第二步,谷歌+;来自配置文件图片json的配置文件图片url

第二步,谷歌+;来自配置文件图片json的配置文件图片url,json,api,flask,google-plus,oauth2,Json,Api,Flask,Google Plus,Oauth2,我在从个人资料图片json获取Google+个人资料图片url时遇到了一个问题。我是一个初学者,这是我的第一个网络应用,所以请考虑这一点 我的回调函数: def callback(self): self.validate_oauth2callback() oauth_session = self.service.get_auth_session( data={'code': request.args['code'], '

我在从个人资料图片json获取Google+个人资料图片url时遇到了一个问题。我是一个初学者,这是我的第一个网络应用,所以请考虑这一点

我的回调函数:

def callback(self):
    self.validate_oauth2callback()
    oauth_session = self.service.get_auth_session(
            data={'code': request.args['code'],
                  'grant_type': 'authorization_code',
                  'redirect_uri': self.get_callback_url()
                 },
            decoder=jsondecoder
    )
    me = oauth_session.get('').json()
    social_id = 'google$' + me['sub']
    username = me.get('name', None) if me.get('name', None) else me['email'].split('@')[0]
    email = me['email'] if me['email_verified'] == True else None
    url = me.get('profile', None)
    image_json = 'https://www.googleapis.com/plus/v1/people/' + me['sub'] + '?fields=image&key=AIz..yAl..juCqj..sjj9y..PuM..R..9..F8p..mo'
    image = image_json['image'] # <-- THIS DOESN'T WORK
    return social_id, username, email, url, image, me
我必须从中提取个人资料图片url。子字符串方法不起作用,因为为了获得代码,我必须“运行”保存在
image\u json
中的url。 我需要像
image=image\u json['image']
这样的东西来检索字符串:

”https://lh6.googleusercontent.com/-f..i0..dl..Gc/AAAAAAAAAAI/AAAAAAAAABQ/iNw-IEz...o/photo.jpg?sz=50“

我想把尺寸改为256而不是50。 我关注了很多帖子,但是我没有找到解决这个问题的方法

将调用此函数的视图如下所示:

{
 "image": {
  "url": "https://lh6.googleusercontent.com/-f..i0..dl..Gc/AAAAAAAAAAI/AAAAAAAAABQ/iNw-IEz...o/photo.jpg?sz=50",
  "isDefault": false
 }
}
@auth.route('/callback/<provider>')
def oauth_callback(provider):
    oauth = OAuthSignIn.get_provider(provider)
    social_id, username, email, url, image, jsonme = oauth.callback()

    if social_id is None:  
        flash('Authentication failed! Access to ' + provider + ' denied.')
        return redirect(url_for('main.home'))

    user = User.query.filter_by(email=email).first()

    if not user:  
        user = User(social_id=social_id, username=username, email=email, social_page=url, social_image=image)
        db.session.add(user)
        db.session.commit()

    else:  

       ...

    login_user(user, True)
    return redirect(url_for('main.home'))
@auth.route(“/callback/”)
def oauth_回调(提供程序):
oauth=OAuthSignIn.get\u提供程序(提供程序)
社交id、用户名、电子邮件、url、图像、jsonme=oauth.callback()
如果social_id为None:
闪存('身份验证失败!拒绝访问'+provider+'。)
返回重定向(url_for('main.home'))
user=user.query.filter_by(email=email).first()
如果不是用户:
用户=用户(社交id=社交id,用户名=用户名,电子邮件=电子邮件,社交页面=url,社交图片=图片)
db.session.add(用户)
db.session.commit()
其他:
...
登录用户(用户,True)
返回重定向(url_for('main.home'))

也许我误解了你,但你似乎离你需要的东西很近了。为什么不使用
image=image\u json['image']['url']
?它不起作用,我得到了这个错误:image=image\u json['image']['url']TypeError:字符串索引必须是整数实际上它不起作用,因为变量
image\u json
不包含字典,只包含字符串
https://www.googleapis.com/plus/v1/people/111111111111111?fields=image&key=AIz..yAl..juCqj..sjj9y..PuM..R..9..F8p..mo“
因此,我需要类似“解码器”的东西它可以得到页面的主体,因为字典在主体中,而不是在urlOk中,我现在得到了那部分。您如何从该URL转换到问题中显示的JSON对象?根据您的代码片段,我只看到您将URL作为字符串存储在
image\u json
中。感谢您的帮助,我已经简单地使用
image=me['picture']
解决了问题,变量
image\u json
不再需要。