Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Python 3.4和Django 1.7的Oauth2_Python_Django_Python 3.x_Google Oauth - Fatal编程技术网

使用Python 3.4和Django 1.7的Oauth2

使用Python 3.4和Django 1.7的Oauth2,python,django,python-3.x,google-oauth,Python,Django,Python 3.x,Google Oauth,我正试图使用oauth2授权我的django应用程序,以便与GoogleContentforShopping API交互。然而,我在使用oauth2的过程中遇到了一些问题 我已经安装了oauth2client和google api python客户端。我的看法如下: CLIENT_SECRETS = os.path.join(os.path.dirname(__file__), 'client_secret.json') FLOW = flow_from_clientsecrets(

我正试图使用oauth2授权我的django应用程序,以便与GoogleContentforShopping API交互。然而,我在使用oauth2的过程中遇到了一些问题

我已经安装了oauth2client和google api python客户端。我的看法如下:

CLIENT_SECRETS = os.path.join(os.path.dirname(__file__), 'client_secret.json')

FLOW = flow_from_clientsecrets(
    CLIENT_SECRETS,
    scope='https://www.googleapis.com/auth/content',
    redirect_uri='https://127.0.0.1:8000/oauth2/oauth2callback')


def get_account_ids(service):
    accounts = service.management().accounts().list().execute()
    ids = []
    if accounts.get('items'):
        for account in accounts['items']:
            ids.append(account['id'])
    return ids


@login_required
def index(request):
    user = request.user
    storage = Storage(CredentialsModel, 'id', user, 'credential')
    credential = storage.get()    
    if credential is None:
        FLOW.params['state'] = xsrfutil.generate_token(
            settings.SECRET_KEY, user)
        authorize_url = FLOW.step1_get_authorize_url()
        f = FlowModel(id=user, flow=FLOW)
        f.save()
        return HttpResponseRedirect(authorize_url)
    else:
        http = httplib2.Http()
        http = credential.authorize(http)
        service = build('content', 'v2', http=http)
        ids = get_account_ids(service)
        return render(
            request, 'index.html', {'ids':ids})


@login_required
def auth_return(request):
    user = request.user
    if not xsrfutil.validate_token(settings.SECRET_KEY, bytes(request.GET['state'], 'utf-8'), user):
        return HttpResponseBadRequest()
    credential = FLOW.step2_exchange(request.GET, http=None)
    storage = Storage(CredentialsModel, 'id', user, 'credential')
    storage.put(credential)
    return HttpResponseRedirect("/oauth2/")
起初,我从auth_return视图中得到一个错误,即request.GET['state']返回的字符串没有编码,因此我更改了以下内容:

if not xsrfutil.validate_token(settings.SECRET_KEY, request.GET['state'], user):
为此:

if not xsrfutil.validate_token(settings.SECRET_KEY, bytes(request.GET['state'], 'utf-8'), user):
        return HttpResponseBadRequest()
错误消失了。但是,我现在得到了一个错误:

'bytes' object has no attribute 'authorize'
从索引视图。导致异常的确切行是:

http = credential.authorize(http)
这似乎是由我之前的改变引起的。我是使用oauth2的新手,我已经花了很多时间尝试调试。谁能给我指一下正确的方向吗


提前谢谢你。

我终于找到了答案

oauth2client中的django_orm模块似乎在CredentialsField定义中使用了“to_python”函数,该函数不起作用,因此返回Base64数据

要解决此问题,必须从以下位置编辑oauth2client/django_orm源定义:

class CredentialsField(models.Field):
致:

这将允许它返回Python2和Python3的凭证对象

确保首先删除当前存储的凭据对象,因为它是字符串对象而不是凭据对象

from django.utils.six import with_metaclass
class CredentialsField(with_metaclass(models.SubfieldBase, models.Field)):