无权请求范围 - Google OAuth2 for devices

Not authorized to request the scopes - Google OAuth2 for devices

我最近一直致力于从 python 脚本访问 gmail API。

由于脚本需要在服务器上 运行,没有任何形式的 Web 浏览器,我不得不实施 OAuth for Devices 流程,详见 https://developers.google.com/accounts/docs/OAuth2ForDevices

一切顺利,因为我一直在使用 "profile" 范围进行测试,我一直在按预期获得访问令牌。

但是,为了切换到我的脚本需要的实际令牌,因为我需要被授权写入用户的电子邮件,我已经尝试 运行 将我的脚本与范围 www.googleapis.com/auth/gmail.compose,其中 returns 以下内容:

{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。

如果有人知道为什么会发生这种情况,我很高兴收到您的来信!

注意:我不得不编辑范围,因为它们算作链接,而且我的声誉还不够高,无法 post 全部。我试过的示波器实际上是正确的。

设备流的 Oauth2 不太安全,并且 fine/allowed 对于一些不太敏感的 API 但不是全部。 Gmail API 不允许使用它。

您可以让您的服务器实现 Web 服务器流程(如果您可以 运行 在其上安装一个 http 服务器),然后您的客户端将使用您可以检索+存储的适当令牌重定向回那里: https://developers.google.com/accounts/docs/OAuth2WebServer