我有一个网站,下面的代码在我的本地机器上运行得很好,但是在远程服务器上,它没有重定向到Google OAuth来创建token.json,并给了我一个502错误。我能在哪里犯错误呢?
我只需要通过OAuth认证就可以访问我的Gmail帐户并从中检索电子邮件。无法生成token.json。
SCOPES = [
'https://mail.google.com/',
]
def get_gmail_service():
creds = None
config_path = os.path.join(os.path.dirname(__file__), 'config')
credentials_path = os.path.join(config_path, 'creds.json')
token_path = os.path.join(config_path, 'token.json')
if os.path.exists(token_path):
creds = Credentials.from_authorized_user_file(token_path, SCOPES)
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
credentials_path, SCOPES)
creds = flow.run_local_server(port=0)
with open(token_path, 'w') as token:
token.write(creds.to_json())
try:
service = build('gmail', 'v1', credentials=creds)
return service
except HttpError as error:
print(f'An error occurred: {error}')
def get_emails():
service = get_gmail_service()
1条答案
按热度按时间flseospp1#
这是我的web快速入门示例。如果你有任何问题,请告诉我,我会看看的