2017-06-13 121 views
0

我正在致力于存储oauth2凭据,以便以后可以创建服务。oAuth2凭据存储为unicode

目前我的模式是这样的:

from django.db import models 
from oauth2client.contrib.django_util.models import CredentialsField 
from south.modelsinspector import add_introspection_rules 

class oAuth(models.Model): 
    siteid = models.CharField(max_length=100L, primary_key=True) 
    credential = CredentialsField() 

    add_introspection_rules([],["^oauth2client\.contrib\.django_util\.models\.CredentialsField"]) 

,当我尝试保存我的凭据:

credential = flow.step2_exchange(code) 
storage = DjangoORMStorage(oAuth, 'siteid', site_id, 'credential') 
storage.put(credential) 

在我的数据库我有被存储在一个Unicode字符串,然后不能转换到一个oauth对象使用:

storage = DjangoORMStorage(oAuth, 'siteid', site_id, 'credential') 
credential = storage.get() 
return credential 

我得到的凭据是先前存储的unicode,我只是不能执行.authorize方法,这是我需要做的,

我得到了困惑的地方?

回答

0

确保您的数据库设置为存储utf-8。 Django需要这个。

https://docs.djangoproject.com/en/1.11/ref/unicode/#creating-the-database

+0

我的问题是,当我调用storage.get()时,我得到的unicode不是原始的oAuth对象,所以我无法调用'.authorize()',我不确定是否需要使用unicode并构建一个oAuth对象,但我无法找到任何关于该对象的说明。 我检查了编码,它是cp1252但是我所有的其他表都是相同的编码,其他所有的东西似乎都正常工作 –

+0

忽略我以前的评论。我现在看到'DjangoORMStorage'来自lib本身。我没有想法。 :-) –

+0

寻找[客户端存储](https://github.com/google/oauth2client/blob/master/oauth2client/client.py)它似乎像.get()存储实际上必须返回一个oauth对象这就是为什么我不明白它是如何返回unicode的 –

0

这已经有一段时间找(我一直在这个挣扎了一天左右问堆栈溢出之前),但..需要进行回一个

返回的Unicode使用OAuth的我通过对象的实际通话进去后发现this

解决办法:

storage = DjangoORMStorage(oAuth, 'siteid', site_id, 'credential') 
credential = storage.get() 
credential = CredentialsField().to_python(credentials) 
return credential 

credential现在可以作为一个普通的oauth对象调用