0

我在使用带有烧瓶、python3 和 google oauth 的 rauth 库时遇到了一些问题,python2 可以正常工作。

它说 TypeError: JSON object must be str, not 'bytes'

这是日志错误:

在此处输入图像描述

我在这里发现了这个问题并尝试将字节转换为字符串

decoder=lambda b: json.loads(str(b))

但没有成功。

这是我的实现

class GoogleSignIn(OAuthSignIn):
def __init__(self):
    super(GoogleSignIn, self).__init__('google')
    self.service = OAuth2Service(
        name='google',
        client_id=self.consumer_id,
        client_secret=self.consumer_secret,
        authorize_url='https://accounts.google.com/o/oauth2/auth',
        access_token_url='https://accounts.google.com/o/oauth2/token',
        base_url='https://www.googleapis.com/plus/v1/people/'
    )

def authorize(self):
    return redirect(self.service.get_authorize_url(
        scope='email',
        response_type='code',
        redirect_uri=self.get_callback_url())
    )

def callback(self):
    if 'code' not in request.args:
        return None, None, None
    oauth_session = self.service.get_auth_session(
        data={'code': request.args['code'],
              'grant_type': 'authorization_code',
              'redirect_uri': self.get_callback_url()},
        decoder=json.loads
    )
    me = oauth_session.get('me').json()
    me_email = None
    for e in me['emails']:
        if e['type'] == 'account':
            me_email = e['value']
    return (
        me.get('id'),
        me.get('displayName'),
        me_email)

提到了另一种使用自己的解码器的方法,但不知道该怎么做,请帮助我。

4

1 回答 1

0

我想出了一些使用simplejson模块的肮脏解决方法(也有一个加载方法,所以这里没有什么可以改变的)

将其导入为

import simplejson as json

在 Google 课程让身份验证按预期工作之前

于 2015-05-18T09:49:20.440 回答