Oauth2 使用 Python 3.4 和 Django 1.7
Oauth2 using Python 3.4 and Django 1.7
我正在尝试使用 oauth2 授权我的 django 应用程序,以便与 google 内容进行交互以进行购物 API。但是,我在使用 oauth2 的过程中遇到了问题。
我安装了 oauth2client 和 google-api-python-client。我的看法如下:
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' 函数,该函数不起作用,因此 return 返回了 Base64 数据。
要解决此问题,您必须编辑 oauth2client/django_orm 来源定义:
class CredentialsField(models.Field):
至:
from django.utils.six import with_metaclass
class CredentialsField(with_metaclass(models.SubfieldBase, models.Field)):
这将允许它 return Python2 和 Python3 的 Credentials 对象。
确保先删除当前存储的凭据对象,因为它是字符串对象而不是凭据对象。
我正在尝试使用 oauth2 授权我的 django 应用程序,以便与 google 内容进行交互以进行购物 API。但是,我在使用 oauth2 的过程中遇到了问题。
我安装了 oauth2client 和 google-api-python-client。我的看法如下:
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' 函数,该函数不起作用,因此 return 返回了 Base64 数据。
要解决此问题,您必须编辑 oauth2client/django_orm 来源定义:
class CredentialsField(models.Field):
至:
from django.utils.six import with_metaclass
class CredentialsField(with_metaclass(models.SubfieldBase, models.Field)):
这将允许它 return Python2 和 Python3 的 Credentials 对象。
确保先删除当前存储的凭据对象,因为它是字符串对象而不是凭据对象。