【发布时间】:2015-05-05 11:55:12
【问题描述】:
我最近一直致力于从 python 脚本访问 gmail API。
由于脚本需要在服务器上运行,无需任何形式的 Web 浏览器,我必须实现 OAuth for Devices 流程,详情请参阅 https://developers.google.com/accounts/docs/OAuth2ForDevices
这一切都很好,因为我一直在使用“配置文件”范围进行测试,我已经按预期获得了访问令牌。
但是,为了切换到我的脚本所需的实际令牌,因为我需要被授权写入用户的电子邮件,我尝试使用范围 www.googleapis.com/auth/gmail.compose 运行我的脚本,它返回以下内容:
{u'error_uri': u'code.google.com/apis/accounts/docs/OAuth2.html', u'error_description': u'Not authorized to request the scopes: [www.googleapis.com/auth/gmail.compose]', u'error': u'invalid_scope'}
此时我很好奇,尝试了各种范围,对我来说似乎相当随意,哪些可以,哪些不可用。
工作:
www.googleapis.com/auth/calendar
www.google.com/m8/feeds
不工作:
mail.google.com/
www.googleapis.com/auth/gmail.compose
www.googleapis.com/auth/gmail.readonly
www.googleapis.com/auth/gmail.modify
www.googleapis.com/auth/contacts.readonly
我已将我的代码上传到https://gist.github.com/Hanse00/3a861430b1543599b3ed,供有兴趣查看的人参考。
Gmail API 确实在项目控制台中启用。
如果有人知道为什么会发生这种情况,我很高兴收到您的来信!
注意:我不得不编辑范围,因为它们算作链接,而且我的声誉还不够高,无法将它们全部发布。我尝试的范围实际上是正确的。
【问题讨论】:
标签: python google-oauth gmail-api