【问题标题】:Not authorized to request the scopes - Google OAuth2 for devices无权请求范围 - 适用于设备的 Google OAuth2
【发布时间】: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


    【解决方案1】:

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

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

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-13
    • 2021-05-01
    • 2017-06-26
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    相关资源
    最近更新 更多