【发布时间】:2020-05-21 16:40:04
【问题描述】:
我在 GCP Cloud Function 上使用 OAuth2.0 时遇到问题。我用来在本地运行此代码。它可以工作,它会打开一个网络浏览器的页面来询问我的 gmail 帐户的访问权限。
我知道InstalledAppFlow 仅用于本地应用程序。
SCOPES = ['https://mail.google.com/']
creds = None
if os.path.exists('token.pickle'):
with open('token.pickle', 'rb') as token:
creds = pickle.load(token)
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) # <-- Oauth2.0 credential
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)
然后我尝试使用 /tmp repesitory 以另一种方式存储令牌,但仍然无法正常工作,我看不出问题出在哪里......你有什么想法吗?非常感谢
SCOPES = ['https://mail.google.com/']
CLIENT_SECRET_FILE = 'credentials.json' #OAuth credentials
APPLICATION_NAME = 'Gmail API Python'
def get_credentials():
store = oauth2client.file.Storage("/tmp/tempcredentials.json")
credentials = store.get()
if not credentials or credentials.invalid:
flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
flow.user_agent = APPLICATION_NAME
credentials = tools.run_flow(flow, store)
return credentials
【问题讨论】:
-
你有错误日志吗?
-
我只在日志中得到状态“崩溃”,但没有足够的信息来知道出了什么问题......
-
这两个代码都可以在本地工作吗?
-
目前有一个known issue,关于当 CF 使用 python37 失败时丢失的痕迹。你应该检查那里的描述,看看是不是你的情况。如果是这样,请应用手动捕获和记录异常的解决方法并在此处分享错误消息。
-
是的对不起@guillaumeblaquiere,这两个代码都在本地工作,这就是我感到困惑的原因..
标签: oauth oauth-2.0 google-cloud-functions google-oauth