python - appengine 的 OAuth2 身份验证停止工作

标签 python google-app-engine oauth-2.0

我有一项定期工作,使用远程 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. That’s an error. Error: invalid_client The OAuth client was not found. Request Details access_type=offline scope=https://www.googleapis.com/auth/appengine.apis https://www.googleapis.com/auth/userinfo.email response_type=code redirect_uri=urn:ietf:wg:oauth:2.0:oob client_id=None That’s all we know.

帮忙?

---- 更新 (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 的开发者控制台在“凭据/服务帐户 key /管理服务帐户”下生成的

最佳答案

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

ConfigureRemoteApiForOAuth 接受使用带有 key 文件的服务帐号,以 appengine_rpc_httplib2.HttpRpcServerOAuth2.OAuth2Parameters 对象的形式提供 oauth2 参数,您可以在其中指定客户端 ID secret 范围OAuth2Parameters Doc

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

gcloud auth login

关于python - appengine 的 OAuth2 身份验证停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37148733/

相关文章:

javascript - Nodejs 无法使用 axios 在 get 请求中发送 oauth v1 参数

python - 了解 scipy.ndimage.map_coordinates 上的输入和输出

python - 将输入从 html 传递到 python 并返回

php - App Engine - PHP Symfony - 如何要求使用 app.yaml 登录

java - 关于开发基于AppEngine的ERP软件

php - 如何处理 Laravel 护照中的 "unsupported_grant_type"

python - 意外的可变打印

python - 无法从 cron 作业中找到 dotenv 环境变量

python - 使用 python gdata.contacts.client 检索一个联系人

java - 我可以在一个应用程序中同时实现 SAML 和基本的 Spring Security 吗?