【问题标题】:How do I get credentials of python-social-auth for google drive sdk?如何获取 google drive sdk 的 python-social-auth 凭据?
【发布时间】:2014-01-15 01:51:16
【问题描述】:

我使用 django 作为我的 web 框架,
我发现python-social-auth 是一个很好的 OAuth 包。
而且我还想从谷歌 OAuth2 访问谷歌驱动器。
我在Google Drive SDK看到了一些示例代码:

from apiclient.discovery import build

def build_service(credentials):

  http = httplib2.Http()
  http = credentials.authorize(http)
  return build('drive', 'v2', http=http)

所以,看来我需要获取凭据来实例化服务器对象。
我该怎么做才能获得凭据?
或者我该怎么做才能使用谷歌驱动器?

【问题讨论】:

    标签: python django oauth-2.0 google-drive-api google-oauth


    【解决方案1】:

    这非常有帮助,感谢@eyscode! 我遇到了和@areyoueye一样的问题,看了谷歌代码后发现,需要对结果进行解码和depickled,然后效果很好! 这是修改后的代码:

    import pickle
    ...
    storage = Storage(GoogleCredential, 'id', user, 'credential')
    credentials = (pickle.loads(base64.b64decode(storage.get())))
    ...
    

    然后执行 print(),你应该得到一个对象作为输出,如下所示:

    ...
    print(credentials)
    <oauth2client.client.OAuth2Credentials object at 0x7fa2837af630>
    

    【讨论】:

      【解决方案2】:

      我遇到过同样的情况,我一直在了解 oauth2client 是如何工作的,所以我根据使用 python-social-auth 进行身份验证获得的数据生成了凭据,我将这个逻辑放在了管道中当用户第一次登录网站时。如下:

      import datetime
      from django.conf import settings
      from oauth2client import GOOGLE_REVOKE_URI, GOOGLE_TOKEN_URI
      from oauth2client.client import OAuth2Credentials, _extract_id_token
      from oauth2client.django_orm import Storage
      from website.models import CredentialsModel
      
      
      def set_google_credentials(strategy, details, response, user=None, *args, **kwargs):
          if user and kwargs.get('is_new'):
              token_expiry = datetime.datetime.utcnow() + datetime.timedelta(seconds=int(response.get('expires_in')))
              id_token = _extract_id_token(response.get('id_token'))
              credential = OAuth2Credentials(
                  access_token=response.get('access_token'),
                  client_id=settings.SOCIAL_AUTH_GOOGLE_PLUS_KEY,
                  client_secret=settings.SOCIAL_AUTH_GOOGLE_PLUS_SECRET,
                  refresh_token=response.get('refresh_token'),
                  token_expiry=token_expiry,
                  token_uri=GOOGLE_TOKEN_URI,
                  user_agent=None,
                  revoke_uri=GOOGLE_REVOKE_URI,
                  id_token=id_token,
                  token_response=response)
              storage = Storage(CredentialsModel, 'id', user, 'credential')
              storage.put(credential)
      

      然后在我的设置中:

      SOCIAL_AUTH_PIPELINE = (
          'social.pipeline.social_auth.social_details',
          'social.pipeline.social_auth.social_uid',
          'social.pipeline.social_auth.social_user',
          'social.pipeline.user.get_username',
          'social.pipeline.user.create_user',
          'myapp.pipeline.set_google_credentials'    
          # more pipelines
      )
      

      然后当我需要用户凭据时:

      更新:正如@bcoover 所指出的,从Storage 获得的结果需要被解码和解酸。

      user = # get a user instance, from the request for example
      storage = Storage(CredentialsModel, 'id', user, 'credential')
      import pickle, base64
      credentials = (pickle.loads(base64.b64decode(storage.get())))
      # do what ever you want with the credentials
      

      目前这种方法对我有用,但如果有人有更好的方法,我将不胜感激。 Storage 和 CredentialsModel 来自https://developers.google.com/api-client-library/python/guide/django

      【讨论】:

      • 我试过了,但 storage.get() 返回一个巨大的以 '\\x67414e' 开头的十六进制字符串 这是我的导入: from oauth2client.django_orm import Storage from socrademy.apps.user.models import CredentialsModel from social.apps.django_app.utils import load_strategy from googleapiclient.discovery import build
      猜你喜欢
      • 2014-09-20
      • 1970-01-01
      • 2013-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多