【问题标题】:OAuth2 authentication for appengine stopped workingappengine 的 OAuth2 身份验证停止工作
【发布时间】:2016-05-12 20:09:37
【问题描述】:

我有一个使用远程 api 从我的 appengine 数据存储中提取数据的周期性工作。 这份工作突然停止工作,因为谷歌决定不再喜欢我的服务器了。 不过,它可以在我的机器上运行。

以下是我机器上的 python shell 中发生的情况:

$ python
Python 2.7.6 (default, Jun 22 2015, 17:58:13) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from google.appengine.ext.remote_api import remote_api_stub
>>> app = 'qmagicobr2.appspot.com'
>>> remote_api_stub.ConfigureRemoteApiForOAuth(app, '/_ah/remote_api')
<google.appengine.tools.appengine_rpc_httplib2.HttpRpcServerOAuth2 object at 0x7f5cdfac61d0>
>>> 

这就是我的服务器上的相同代码会发生什么

(qmbi)dashboard@ip-172-31-32-222:~/qmbi$ python
Python 2.7.6 (default, Jun 22 2015, 17:58:13) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from google.appengine.ext.remote_api import remote_api_stub
>>> app = 'qmagicobr2.appspot.com'
>>> remote_api_stub.ConfigureRemoteApiForOAuth(app, '/_ah/remote_api')
Go to the following link in your browser:

    https://accounts.google.com/o/oauth2/auth?scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fappengine.apis+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&response_type=code&client_id=None&access_type=offline

Enter verification code:

当我转到上面的 URL 时,我得到一个错误页面,上面写着:

  1. 这是一个错误。 错误:invalid_client 未找到 OAuth 客户端。 请求详细信息 access_type=离线 范围=https://www.googleapis.com/auth/appengine.apishttps://www.googleapis.com/auth/userinfo.email response_type=代码 redirect_uri=urn:ietf:wg:oauth:2.0:oob client_id=无 这就是我们所知道的。

帮助?

---- 更新 (2016-05-12) -----

我忘了提及我正在使用的身份验证文件。 要使ConfigureRemoteApiForOAuth 工作,我需要有一个环境变量GOOGLE_APPLICATION_CREDENTIALS 指向凭据文件。 我愿意。 在两种环境中(我的机器和服务器)GOOGLE_APPLICATION_CREDENTIALS 指向一个 json 文件(两种环境中的相同文件),格式如下:

{
  "type": "service_account",
  "project_id": "qmagicobr2",
  "private_key_id": "000000000000000000000000000000000000000",
  "private_key": "-----BEGIN PRIVATE KEY-----\nAAAAAAAAAAAAAAAAAAAAAAA....AAAA\n-----END PRIVATE KEY-----\n",
  "client_email": "dashboard@qmagicobr2.iam.gserviceaccount.com",
  "client_id": "0000000000000000000000",
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  "token_uri": "https://accounts.google.com/o/oauth2/token",
  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/dashboard%40qmagicobr2.iam.gserviceaccount.com"
}

此文件是使用 Appengine 的开发者控制台生成的,位于“凭据/服务帐户密钥/管理服务帐户”下

【问题讨论】:

    标签: python google-app-engine oauth-2.0


    【解决方案1】:

    所以我刚刚测试了这个特殊的用例,客户端 ID 是 none 的事实揭示了很多。

    ConfigureRemoteApiForOAuth 接受使用带有密钥文件的服务帐户,提供 oauth2 参数作为 appengine_rpc_httplib2.HttpRpcServerOAuth2.OAuth2Parameters 对象,您可以在其中指定 client idsecretscope 等。OAuth2Parameters Doc

    但缓解这种情况的最简单方法是使用gcloud tool 正确验证您的服务器:

    gcloud auth login
    

    【讨论】:

    • 我忘记提及我正在使用的身份验证文件,我正在更新问题。
    • 我添加了有关身份验证文件的更多信息,您能看看我的更新吗?
    • 哦,感谢您的回答!我在服务器上安装了 gcloud 并完成了身份验证过程(将浏览器上的链接和代码粘贴回终端)。那行得通。我注意到它创建了~/.config/gcloud/application_default_credentials.json。我将我的GOOGLE_APPLICATION_CREDENTIALS 指向该文件,它起作用了!
    猜你喜欢
    • 2018-09-28
    • 1970-01-01
    • 2017-06-16
    • 1970-01-01
    • 2010-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多