如何获取python-social-auth的凭据以获取Google Drive sdk的凭据? [英] How do I get credentials of python-social-auth for google drive sdk?

查看:143
本文介绍了如何获取python-social-auth的凭据以获取Google Drive sdk的凭据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为我的web框架使用django,

我找到 python-social -auth 是一个很好的OAuth包。

我也想从谷歌OAuth2访问谷歌驱动器。

我看到一些示例代码 Google Drive SDK

  from apiclient.discovery import build 
$ b $ def build_service(凭证):

http = httplib2.Http()
http =凭证。授权(http)
return build('drive','v2',http = http)



<因此,看起来我需要获取证书来实例化服务器对象。

我该怎么做才能获得证书?

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

解决方案

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

 从django.conf导入日期时间
从oauth2client导入设置
导入GOOGLE_REVOKE_URI,GOOGLE_TOKEN_URI $来自oauth2client.client的b $ b从oauth2client.django_orm导入OAuth2Credentials,_extract_id_token
导入从website.models导入存储
CredentialsModel


def set_google_credentials(策略,详细信息,如果用户和kwargs.get('is_new'):
token_expiry = datetime.datetime.utcnow()+ datetime.timedelta(seconds ='response',user = None,* args,** kwargs):
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 =无,
revoke_uri = GOOGLE_REVOKE_URI,
id_token = id_token,
token_response =响应)
storage =存储(CredentialsModel,'id',用户,'凭证')
storage.put(凭证)

然后在我的设置中:

  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'
#更多管线

然后当我需要用户的凭证时:
$ b 更新 @ bcoover 注意到从存储需要解码和取消的结果。

  user =#从请求获取用户实例
storage = Storage(CredentialsModel,'id',user,'credential')
导入pickle,base64
凭证=(pickle.loads(base64.b64decode(storage.get())))
#做任何你想要的凭证

这种方法目前适用于我,但如果有人有更好的方式去做,我会很感激。 Storage和CredentialsModel来自 https://developers.google.com/ api-client-library / python / guide / django


I use django for my web framework,
I found python-social-auth is a good package for OAuth.
And I also want to get access google drive from google OAuth2.
I saw some sample code on 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)

So, Looks like I need to get credentials to instantiate server objects.
How do I do so that can get credentials?
Or what do I do can get use google drive?

解决方案

I've run into the same case, and I've been seeing how oauth2client works, so I've generated the credential from the data obtained by the authentication with python-social-auth, I put this logic inside a pipeline when a user logged in the website for the first time. As follows:

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)

then in my settings:

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
)

then when I need credentials for a user:

UPDATE: As @bcoover noted the results got from Storage needed to be decoded and depickled.

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

this approach works for me at the moment, but if someone has a better way to do, I'll be grateful. The Storage and CredentialsModel are from https://developers.google.com/api-client-library/python/guide/django

这篇关于如何获取python-social-auth的凭据以获取Google Drive sdk的凭据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆