将客户端oauth2凭据存储在Google App-Engine数据存储区中

时间:2013-06-28 02:10:21

标签: python-2.7 google-app-engine google-oauth google-cloud-datastore google-api-python-client

我正在编写一个代表注册用户离线访问Google API的应用程序。注册过程收集基本帐户信息,如商家名称,联系人Eamil等。然后让用户完成OAuth2流程,最终获得用户的OAuth2凭据。我想在同一个数据存储区实体中保存所有这些数据,商家名称,联系人电子邮件和OAuth2凭据。但是,当我尝试调用Model.put();

时,我收到了这个奇怪的错误
  

BadValueError:属性凭据必须可转换为Credentials实例(< oauth2client.client.OAuth2Credentials对象,位于0xb595facc>)

我的Oauth回调功能如下所示。您可以看到我尝试使用StorageByKeyName()但它给出了相同的错误。

注意我不确定这是否重要,但在我调用ua.put()时,尚未创建实体。我已经看到了在分配凭证属性时实体已经存在的其他非常相似的代码。

class Oauth2callback(BaseHandler):
    def get(self):
        user    = users.get_current_user()
        flow    = pickle.loads(memcache.get( user.user_id() ))
        if flow:
            # Store UserAccount
            credentials = flow.step2_exchange(self.request.params)
            pa = models.ParentAccount.get_account_by_token('XXXXXXXXXX')
            ua = models.UserAccount(
                key_name        = user.user_id(),
                account_email   = user.email(),
                parent_account  = pa,
                credentials     = credentials,
            )
            ua.put()

            # Try Storing Credentials Separately
            # credentials = flow.step2_exchange(self.request.params)
            # storage = StorageByKeyName(models.UserAccountCredentials, user.user_id(), 'credentials')
            # storage.put(credentials)

由于

这是模型定义。

class UserAccount(db.Model):
    status_states       = ["IN-ACTIVE","ACTIVE", "PAUSED", "DELETED"]

    account_email       = db.StringProperty(required=False)
    contact_email       = db.StringProperty(required=False)
    business_name       = db.StringProperty(required=False)
    create_date         = db.DateTimeProperty(auto_now_add=True)
    account_settings    = db.StringProperty(required=False) # JSON config data MAX 500 characters. Change type: 'Text' for larger values and to avoid indexing.
    parent_account      = db.ReferenceProperty(ParentAccount, collection_name = "children", required=False)
    status              = db.StringProperty(required=True, default = "ACTIVE", choices=set(status_states))
    original_status     = db.StringProperty(required=True, default = "ACTIVE", choices=set(status_states))
    credentials         = CredentialsProperty(required=False)

1 个答案:

答案 0 :(得分:0)

我认为序列化凭据可能存在问题。我正在寻找适当的腌制OAuth2Credentials对象来存储它。 如果我找到解决方案,我会更新此信息。

更新使用OAuth2Credentials.to_json()获取from_json()对象的可序列化版本以将其转换回来。存储纯文本可能会导致安全问题。有关更多信息,请参阅有关会话的Flask文档,以防您需要开头。