【问题标题】:Account links successfully but access token shows invalid - Google OAuth帐户链接成功,但访问令牌显示无效 - Google OAuth
【发布时间】:2019-03-27 14:44:06
【问题描述】:

我一直在尝试使用我的 Dialogflow 帐户链接的隐式流程实现 Google OAuth 服务(使用此 documentation

我已根据该文档开发了代码。当用户链接他的帐户并显示“您已成功,将您的帐户与聊天机器人链接”时,它可以正常工作。

但是当我尝试使用我在该文档的步骤 3 中生成的访问令牌时,它说访问令牌无效。看起来它没有注册到 Google 服务!

这是我实现它的代码

import os
import flask
from flask import request, redirect
import string
import random
from random import choice, randint
import google_auth_oauthlib.flow
from AES import AESCipher
import json

CLIENT_SECRETS_FILE = "client_secret.json"

SCOPES = ['https://www.googleapis.com/auth/userinfo.profile', 'https://www.googleapis.com/auth/userinfo.email']
app = flask.Flask(__name__)
app.secret_key = 'SECRET_KEY'

#
# Actions on google call this function with client_id, redirect_uri, state, and response_type
#
@app.route('/auth')
def authorize():
    flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(
        CLIENT_SECRETS_FILE, scopes=SCOPES)

    flow.redirect_uri = flask.url_for('oauth2callback', _external=True)

    print ("%%%%STATE", request.args["state"])
    authorization_url, state = flow.authorization_url(
        access_type='offline',
        include_granted_scopes='true')

    flask.session['state'] = request.args["state"]
    print ("IN AUTH STATE ", state)

    return flask.redirect(authorization_url)


def get_user_id():
    min_char = 8
    max_char = 12
    allchar = string.ascii_letters + string.punctuation + string.digits
    password = "".join(choice(allchar) for x in range(randint(min_char, max_char)))
    return password


@app.route('/oauth2callback')
def oauth2callback():
    print ("### IN CALLBACK ###", request)
    state = flask.session['state']

    user_id = get_user_id()
    client_id = ''.join(random.sample("Google", len("Google")))
    key = ''.join(random.sample("JayPatel", len("JayPatel")))
    bytes_obj = {
        'user_id': user_id,
        'client_id': client_id
    }
    access_token = AESCipher("Jay").encrypt(json.dumps(bytes_obj))
    access_token = access_token.replace('+', 'p')
    access_token = access_token.replace('&', '1')

    # My generated access_token looks like 
    # 6u2PeFcUtNmAblw5N3YAKSn7EC46xQVmgv3Rc8XK9sTW4xcgAxw1doRsvmgE/CC2qGUJ1x7XpNpdYvGes6qJHIEQSwvgEJt8hnYEUbm0qEA=
    print ("###### Access Token ", access_token)
    print ("###### State ", state)

    url = "https://oauth-redirect.googleusercontent.com/r/test-happierbot-4c514#access_token=" + access_token + "&token_type=bearer&state=" + state

    return redirect(url)


if __name__ == '__main__':
    os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = '1'

    app.run('localhost', 8080, debug=True)

当我尝试通过https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=my_generated_token API 使用 access_token 获取信息时,它给出了

{
    "error": "invalid_token",
    "error_description": "Invalid Value"
}

是否有任何其他过程可以激活 access_token 或者我是 缺少任何步骤?

【问题讨论】:

    标签: oauth access-token dialogflow-es actions-on-google google-account


    【解决方案1】:

    如果我正确地遵循代码,您正在生成自己的访问令牌以返回给用户,然后使用谷歌的“tokeninfo”端点来查看令牌是否有效?

    Google 不知道您的令牌是什么或意味着什么,因此它返回它是无效的。助理使用您的服务获取令牌,并在发送来自用户的消息时返回它。否则,它只是将其视为不记名令牌。

    您的服务应根据您想要的任何方式确定其有效性(在表格中查找它,反向解密和验证它的过程,检查有效签名等)。由于 Google 对令牌一无所知,因此无法确定它是否真的有效。

    tokeninfo 服务返回有关 Google 生成的令牌的信息

    Google 不允许您将其令牌端点用于 OAuth 帐户链接。相反,您可以使用Google Sign In for Assistant,这将为您提供一个 id 令牌。您可以通过网站将其与常规 Google 登录一起使用,以获取 访问令牌刷新令牌,您可以使用这些令牌在获得用户许可的情况下访问用户的 Google 资源. (详情请参阅this Stack Overflow answer。)

    【讨论】:

    • 好的!听起来不错,有没有办法从谷歌生成令牌?还有一件事,当我尝试使用 Google OAuth 从 Google 日历 API (github.com/gsuitedevs/python-samples/tree/master/calendar/…) 中获取生成的令牌时,我发现它有 expires_in=3600 参数集。我怎样才能让它终生?
    • @JayPatel 访问令牌的生命周期为一小时,您无法更改。
    • @DaImTo 所以每隔一小时,聊天机器人用户需要再次链接他们的帐户!会烂的!有什么方法可以让他们登录吗?
    • 您只需将用户帐户链接到您的 oauth 服务器一次。之后,将由您的服务器从 Google 的服务器请求新的访问令牌。用户不应该看到这个,所以它不会很糟糕。
    • 答案已更新,其中包含有关其他帐户链接选项的更多信息,这可能会为您提供更多帮助,并提供指向 SO 问题的链接,我将在其中更深入地回答这种情况。
    猜你喜欢
    • 2012-05-27
    • 2012-04-05
    • 2013-05-26
    • 1970-01-01
    • 2019-03-01
    • 2021-11-21
    • 2013-05-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多