如何交换令牌的授权码? Google的Oauth2

时间:2020-05-20 15:09:37

标签: python authentication google-authentication

我正在使用react和python进行身份验证。因此,从前端开始,我将重定向到后端以验证令牌。至少那是我想发生的事情。目前,我可以获取授权码,但似乎无法将其交换为令牌。我在这里https://developers.google.com/identity/protocols/oauth2/web-server#python_3正确地遵循了文档 但它似乎不起作用。我收到此错误FileNotFoundError:[Errno 2]当我运行代码时,没有这样的文件或目录:'client_secret.json'

这是我的代码

@Auth.route('/login', methods=['GET'])
def login():

    client_secret = "xxxxx"
    client_id = "xxxxxxx"
    redirect_uris = "http://127.0.0.1:5000/auth/logins"

    data = request.data
    state = flask.session['state']
    flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file( 'client_secret.json', scopes=['https://www.googleapis.com/auth/drive.metadata.readonly'], state=state)
    flow.redirect_uri = flask.url_for('oauth2callback', _external=True)
    authorization_response = flask.request.url
    flow.fetch_token(authorization_response=authorization_response)
    credentials = flow.credentials
    flask.session['credentials'] = {
        'token': credentials.token,
        'refresh_token': credentials.refresh_token,
        'token_uri': credentials.token_uri,
        'client_id': credentials.client_id,
        'client_secret': credentials.client_secret,
        'scopes': credentials.scopes}

0 个答案:

没有答案