Python Google OAuth2令牌中的段数错误

2024-09-27 19:25:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试将我的Android Flutter项目连接到flaskapi服务器,客户端应用程序按预期工作。问题是,即使将OAuth2访问令牌硬编码到服务器中也会给出以下错误消息。我不明白为什么会这样。关于Python后端OAuth2处理的Google官方文档是here。在

错误消息: Wrong number of segments in token: b'ya29.GluNBQsv_8FW2-jjI0w.....

代码: 在

import flask
from flask import jsonify, request, redirect, url_for
from google.oauth2 import id_token
from google.auth.transport import requests

#### SKIPPED INIT AND OTHER CODE ####

@app.route('/', methods=['POST'])
def index():
    token = "ya29.GluNBQsv_8FW2....." # 129 chars in total

    try:
        idinfo = id_token.verify_oauth2_token(token, requests.Request(), None)
        print(idinfo['email'])
    except Exception as ex:
        print(ex)
        data = {
            'status': 403,
            'message': 'Authorization required',
        }
        response = jsonify(data)
        response.status_code = 403
        return response

Tags: infromimport服务器token消息flaskresponse
1条回答
网友
1楼 · 发布于 2024-09-27 19:25:23

您传递的是access_令牌而不是id_令牌。 当你启动一个Google登录,它会给你一个“代码”,然后你把它传递给令牌端点,它给你三件事。访问令牌、id令牌和刷新令牌(根据条件)。 该函数需要的是id帴token。在

相关问题 更多 >

    热门问题