【问题标题】:Django nonrel can not retrieve saved Credential via StorageDjango nonrel 无法通过存储检索保存的凭据
【发布时间】:2014-08-28 18:47:34
【问题描述】:

我正在尝试让默认示例应用程序运行以使用 API(与此示例相同):

https://code.google.com/p/google-api-python-client/source/browse/samples/django_sample/plus/views.py

我成功检索了访问令牌,但是我的索引视图处理程序无法检索保存在回调中的凭据。这意味着我的视图处于无休止的重定向循环中,要求 API 访问它已经拥有的整个时间:

INFO:root:"GET / HTTP/1.1" 302 -
INFO:oauth2client.client:Successfully retrieved access token
INFO:root:"GET /oauth2callback/?state=1Rwu-LG-cowGmf9UrW6EuzoxNDA5MTY3M&code=4/yaQK1MVKYh9PeSN7OilnYHzdfSf_.An4_j5za2IcboiIBeO6P2m9ej9cxkAI HTTP/1.1" 302 -
INFO:root:"GET / HTTP/1.1" 302 -
INFO:oauth2client.client:Successfully retrieved access token
INFO:root:"GET /oauth2callback/?state=1Rwu-LG-cowGmf9UrW6EuzoxNDA5MTY3M&code=4/yaQK1MVKYh9PeSN7OilnYHzdfSf_.An4_j5za2IcboiIBeO6P2m9ej9cxkAI HTTP/1.1" 302 -
INFO:root:"GET / HTTP/1.1" 302 -
INFO:oauth2client.client:Successfully retrieved access token
INFO:root:"GET /oauth2callback/?state=1Rwu-LG-cowGmf9UrW6EuzoxNDA5MTY3M&code=4/yaQK1MVKYh9PeSN7OilnYHzdfSf_.An4_j5za2IcboiIBeO6P2m9ej9cxkAI HTTP/1.1" 302 -

我可以看到凭据已成功存储在具有正确用户 ID 的数据存储中。似乎存储似乎无法检索它(credentialNone

PS:类似于this unanswered question

更新

我收到的令牌如下所示:

{'revoke_uri': 'https://accounts.google.com/o/oauth2/revoke',
'access_token': u'ya29.cACFl3-de08MsB0AAADMsAKOc4d06fkI71_YEAfoPWaG84f3PoPiMRUmdSMQw',
'token_uri': u'https://accounts.google.com/o/oauth2/token',
'token_response': {u'access_token': u'ya29.cACFl3-de08MsB0AAADMsAKOc4d06fkI71_YEAfoPWaG84f3PoPiMRUmdSMQw',
                    u'token_type': u'Bearer', u'expires_in': 396,
                    u'id_token': {u'aud': u'1020730626638-fdf06iqdihvr4lbldeg7jd7suuau9rl.apps.googleusercontent.com',
                                  u'cid': u'1020730626638-fdf06iqdihvr4lbldeg7jd7suuau9rl.apps.googleusercontent.com',
                                  u'iss': u'accounts.google.com', u'at_hash': u'n_NmKydTYbZIEzHVzUg0cw',
                                  u'exp': 1409314030,
                                  u'azp': u'1020730626638-fdf06iqdihvr4lbldeg7jd7suuau9rl.apps.googleusercontent.com',
                                  u'iat': 1409310130, u'token_hash': u'n_NmKydTYbZIEzHVzUg0cw',
                                  u'id': u'10694247378316072486', u'sub': u'10694247378316072486'}},
'invalid': False,
'refresh_token': None,
'client_id': u'1020730626638-fdf06iqdihvr4lbldeg7jd7suuau9rl.apps.googleusercontent.com',
'id_token': {u'aud': u'1020730626638-fdf06iqdihvr4lbldeg7jd7suuau9rl.apps.googleusercontent.com',
              u'cid': u'1020730626638-fdf06iqdihvr4lbldeg7jd7suuau9rl.apps.googleusercontent.com',
              u'iss': u'accounts.google.com', u'at_hash': u'n_NmKydTYbZIEzHVzUg0cw', u'exp': 1409314030,
              u'azp': u'1020730626638-fdf06iqdihvr4lbldeg7jd7suuau9rl.apps.googleusercontent.com', u'iat': 1409310130,
              u'token_hash': u'n_NmKydTYbZIEzHVzUg0cw', u'id': u'10694247378316072486',
              u'sub': u'10694247378316072486'},
'client_secret': u'8B9ODH3EliQwcXRhF2f3qicN',
'token_expiry': datetime.datetime(2014, 8, 29, 12, 7, 7, 303770),
'store': None,
'user_agent': None
}

如果我使用以下内容更改回调视图处理程序,则通过添加立即检索:

credential = FLOW.step2_exchange(request.REQUEST)
storage = Storage(CredentialsModel, 'id', request.user, 'credential')
storage.put(credential)
credential = storage.get()

那么最后一个凭证是None

【问题讨论】:

  • 您是否只是想让 OAuth 正常工作以便使用 API?
  • @Bruyere 我认为誓言有效,是 Storage 类无法加载凭据。
  • 如果您愿意,我可以向您展示如何使用处理 OAuth 过程的 Google 系统。它处理存储和检索凭据,以及在需要时进行刷新。它可能会解决您的问题,但需要稍微重写。
  • @Bruyere 我不介意,但 django-nonrel 是必需的。
  • 对不起,我在阅读那部分时隔开了。如果您转到数据存储查看器,是否列出了任何内容?

标签: google-app-engine google-oauth django-nonrel


【解决方案1】:

我在 CredentialsModel 类的 ForeignKey 属性上将 primary_key=True 更改为 unique=True。一旦我进行了此更改,Storage 对象就能够正确获取用户存储的凭据。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-29
    • 2019-01-16
    • 2016-04-19
    • 2013-07-18
    • 1970-01-01
    • 2013-05-31
    • 2022-11-04
    相关资源
    最近更新 更多