我在GCP云功能上使用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
如the documentation中所述,临时文件夹仅用于创建临时文件,这些临时文件将存储在RAM存储器中,并且仅可用于当前正在执行您的代码的实例,因此不能保证调用之间的持久性。
[您应该检查this tutorial,因为它说明了如何从Cloud Functions验证到Gmail(您将需要多个功能)。