Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
帐户链接成功,但访问令牌显示无效-Google OAuth_Oauth_Access Token_Dialogflow Es_Actions On Google_Google Account - Fatal编程技术网

帐户链接成功,但访问令牌显示无效-Google OAuth

帐户链接成功,但访问令牌显示无效-Google OAuth,oauth,access-token,dialogflow-es,actions-on-google,google-account,Oauth,Access Token,Dialogflow Es,Actions On Google,Google Account,我一直在尝试为我的Dialogflow帐户链接实现带有隐式流的GoogleOAuth服务(使用这个) 我已经根据那个文档开发了代码。当用户链接他的帐户并显示“您已成功地将您的帐户链接到聊天机器人”时,它可以正常工作 但是,当我尝试使用在该文档的步骤3中生成的访问令牌时,它表示访问令牌无效。看起来它没有在谷歌服务上注册 这是我的代码,我用它实现了它 import os import flask from flask import request, redirect import string im

我一直在尝试为我的Dialogflow帐户链接实现带有隐式流的GoogleOAuth服务(使用这个)

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

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

这是我的代码,我用它实现了它

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,它提供

{
    "error": "invalid_token",
    "error_description": "Invalid Value"
}
是否有任何其他过程来激活访问令牌或I 缺少任何步骤?


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

谷歌不知道你的代币是什么或意味着什么,所以它返回它是无效的。助手使用您的服务获取令牌,并在从用户发送消息时返回令牌。否则,它只将其视为不记名令牌

您的服务需要根据您想要的任何方式来确定其有效性(在表中查找、反向解密和验证、检查有效签名等)。由于谷歌对该代币一无所知,因此无法确定它是否真的有效

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


Google不允许您将其令牌端点用于OAuth帐户链接。相反,您可以使用,这将为您提供一个id令牌。您可以将其与通过网站的常规Google登录一起使用,以获得访问令牌和刷新令牌,您可以使用该令牌在用户的许可下访问其Google资源。(有关详细信息,请参阅。)

好的!听起来不错,有没有办法从谷歌生成代币?还有一件事,当我尝试使用GoogleOAuth在CalendarAPI()中从Google获取生成的令牌时,我发现它有
expires\u in=3600
参数集。我怎样才能生存?@JayPatel访问令牌的生存时间为一小时,你不能改变它。@DaImTo所以每一小时后,聊天机器人用户需要再次链接他们的帐户!!这会很糟糕!有没有办法让他们登录?您只需将用户帐户链接到oauth服务器一次。之后,将由您的服务器向Google服务器请求新的访问令牌。用户不应该看到这一点,所以它不会吸。答案更新了更多关于其他帐户链接选项的信息,这可能会帮助你更多,并链接到一个so问题,我在这里更深入地回答这个问题。