我创建了一些可以使用gmail的应用程序。但我使用文档和gmail中的授权代码,为了完成它,我需要在浏览器中打开一个链接来完成身份验证,但在原则上不可能的情况下,我如何在云功能中做到这一点?
"""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(
'gmail-sms-e30e484e728e.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)
request = {
'labelIds': ['INBOX'],
'topicName': 'projects/sms-service-1606905645083/topics/sms'
}
results = service.users().messages().list(userId=user_id).execute()
messages = results.get('messages', [])
last_message_id = messages[0].get('id')
notification_txt = show_chatty_threads(service, last_message_id)
plivo_send_message(notification_txt)
我需要在浏览器中打开一个链接,该链接将完成身份验证,但我如何在云功能中做到这一点原则上是不可能的吗?
您不能在云函数中执行此操作。谷歌要求用户与网络浏览器交互以授权其帐户。
对于云功能等服务,您要么需要传递给您的凭据(访问或身份令牌(,要么需要使用服务帐户。