如何解决如何使用相同的端口运行python脚本 发件人:收件人:参考:
现在,我正在通过Gmail设置oauth2,以通过python脚本发送邮件。
我正在使用Google提供的快速入门代码来验证授权代码,但是我遇到的情况是,当我运行python脚本时,端口总是在变化。
from __future__ import print_function
import pickle
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
# If modifying these scopes,delete the file token.pickle.
SCOPES = ['https://www.googleapis.com/auth/gmail.readonly']
def main():
"""Shows basic usage of the Gmail API.
Lists the user's Gmail labels.
"""
creds = None
# The file token.pickle stores the user's access and refresh tokens,and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists('token.pickle'):
with open('token.pickle','rb') as token:
creds = pickle.load(token)
# If there are no (valid) credentials available,let the user log in.
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.json',SCOPES)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open('token.pickle','wb') as token:
pickle.dump(creds,token)
service = build('gmail','v1',credentials=creds)
# Call the Gmail API
results = service.users().labels().list(userId='me').execute()
labels = results.get('labels',[])
if not labels:
print('No labels found.')
else:
print('Labels:')
for label in labels:
print(label['name'])
if __name__ == '__main__':
main()
此代码的问题是,每当我运行脚本时,它总是会更改端口,并且无法在Google控制台中设置重定向的URI。
我的问题是,如何在python脚本上设置运行端口?
解决方法
例如,如何进行以下修改?
发件人:
{
title: "View all",isBtn: true
}
收件人:
creds = flow.run_local_server(port=0)
- 在这种情况下,每次使用端口
creds = flow.run_local_server()
。
或
8080
- 在这种情况下,每次使用端口
creds = flow.run_local_server(port=8000)
。
参考:
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。