403“请求的认证范围不足".使用Python在Google API中创建草稿时 [英] 403 "Request had insufficient authentication scopes" when creating drafts in Google API with Python
问题描述
我是第一次使用google api,并且是一般编码的新手,并且我有一个很确定的问题,但我找不到答案.当我运行以下代码-
I am working with the google api for the first time, and am new to coding in general, and have a question that I'm sure is very simple, but I cannot find the answer to. When I run the following code -
credentials = get_credentials()
http = credentials.authorize(httplib2.Http())
service = discovery.build('gmail', 'v1', http=http)
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'])
message_body = "This is the message"
message = {'message': message_body}
draft = service.users().drafts().create(userId='me', body=message).execute()
我可以成功列出我的gmail帐户中的标签,但是创建草稿的请求会弹出一条错误消息请求的身份验证请求不足".我发现创建草稿需要以下范围之一:
I am successfully able to list the labels in my gmail account, but the request to create a draft kicks back an error message "Request had insufficient authentication request". I found that creating drafts requires one of the following scopes:
- https://mail.google.com/
- https://www.googleapis.com/auth/gmail.modify
- https://www.googleapis.com/auth/gmail.compose
- https://mail.google.com/
- https://www.googleapis.com/auth/gmail.modify
- https://www.googleapis.com/auth/gmail.compose
但是对于我的一生,我无法弄清楚这到底意味着什么或如何实现,尽管我已尽力在某个地方找到了它.
but for the life of me, I cannot figure out what exactly that means or how to make that happen, though I've done my best to find this somewhere.
推荐答案
如果您遵循快速入门,则您的代码中可能会有一个全局变量SCOPES
,其值为https://www.googleapis.com/auth/gmail.readonly
.当您的用户重定向到Google时,会使用这些作用域,以便为您提供access_token
,以便您可以阅读用户的gmail帐户的内容.
You probably have a global variable SCOPES
in you code with the value of https://www.googleapis.com/auth/gmail.readonly
if you are following the Quickstart. These scopes are used when your users are redirected to Google in order to give you an access_token
so you can read the contents of your user's gmail account.
要发送草稿等,您需要提到的范围之一,例如https://mail.google.com/
.将您的SCOPES
值更改为此字符串,并删除~/.credentials/gmail-python-quickstart.json的凭据"noreferrer>快速入门,以获取新的凭据,这些凭据不仅可以读取内容,还可以做更多的事情.
In order to send drafts etc. you need one of the scopes you mentioned, e.g. https://mail.google.com/
. Change your value of SCOPES
to this string, and remove your credentials located at ~/.credentials/gmail-python-quickstart.json
as outlined by the Quickstart in order to get new credentials that can do more than just read content.
这篇关于403“请求的认证范围不足".使用Python在Google API中创建草稿时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!