如何获取用于google drive sdk的python social auth的凭据?

如何获取用于google drive sdk的python social auth的凭据?,python,django,oauth-2.0,google-drive-api,google-oauth,Python,Django,Oauth 2.0,Google Drive Api,Google Oauth,我将django用于我的web框架, 我发现这是一个很好的OAuth软件包。 我还想从google OAuth2访问google drive。 我在上看到了一些示例代码: 因此,看起来我需要获取凭据来实例化服务器对象。 如何才能获得凭据? 或者我该怎么做才能使用google drive?我遇到过同样的情况,我一直在了解oauth2client是如何工作的,所以我用python social auth通过身份验证获得的数据生成了凭据,当用户第一次登录网站时,我将此逻辑放入管道中。详情如下: imp

我将django用于我的web框架,
我发现这是一个很好的OAuth软件包。
我还想从google OAuth2访问google drive。
我在上看到了一些示例代码:

因此,看起来我需要获取凭据来实例化服务器对象。
如何才能获得凭据?

或者我该怎么做才能使用google drive?

我遇到过同样的情况,我一直在了解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
)
然后,当我需要用户的凭据时:

更新:如前所述,从
存储
中获得的结果需要解码和解密

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

目前这种方法对我很有效,但如果有人有更好的方法,我会很感激。存储和凭证模型来自

这非常有用,谢谢@eyscode! 我遇到了与@areyoueye相同的问题,在查看Google代码后,结果需要解码和解密,然后它工作得很好! 以下是修改后的代码:

import pickle
...
storage = Storage(GoogleCredential, 'id', user, 'credential')
credentials = (pickle.loads(base64.b64decode(storage.get())))
...
然后执行print(),您应该得到一个对象作为输出,如下所示:

...
print(credentials)
<oauth2client.client.OAuth2Credentials object at 0x7fa2837af630>
。。。
打印(凭证)
我尝试了这一点,但storage.get()返回一个以“\\x67414e”开头的巨大十六进制字符串。以下是我的导入:从oauth2client.django\u orm从socrademy.apps.user.models导入存储从social.apps.django\u app.utils导入加载策略从googleapiclient.discovery导入构建
...
print(credentials)
<oauth2client.client.OAuth2Credentials object at 0x7fa2837af630>