oAuth2凭证存储为unicode

时间:2017-06-13 08:00:45

标签: python django oauth double-click

我正在努力存储oauth2凭据,以便我以后可以创建服务。

目前我的模型看起来像这样:

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方法,这是我需要做的,

我在某处感到困惑吗?

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

这花了我一段时间才找到(我在争吵堆栈溢出之前已经挣扎了一天左右)但是......

返回的unicode需要返回到oauth对象,我通过实际调用找到了this

解决方案:

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

credential现在可以被称为常规oauth对象