使用 O-Auth 在 Flask 应用程序上访问 Google My Business

我正在尝试在烧瓶应用程序上访问 Google My Business API,但遇到了问题。我已经使用授权和 oauth-callback 函数设置了 O-Auth 过程。oauth 声称一切正常,因为它完成了 oauth-callback 函数并重定向到位置方法。我在构建函数中添加了一个开发者 api 密钥。当我尝试使用 build 函数建立与 api 的连接时,我得到了这个:googleapiclient.errors.UnknownApiNameOrVersion: name: mybusiness version: v4. 我很确定是正确的 api 详细信息,因为在没有 oauth 的命令行版本中,api 名称和版本号有效。我卡住了,我认为错误消息可能有点误导,也许我的 oauth 过程有问题。我做的不对?


我已经使用相同的程序尝试了 google drive api 并且它有效。我还确保在 google 开发者控制台中启用了 google my business api。


这是授权功能:

@bp.route('/authorize')

def authorize():


    # Create flow instance to manage the OAuth 2.0 Authorization Grant Flow steps.

    flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(

        CLIENT_SECRETS_FILE, scopes=SCOPES)


    # The URI created here must exactly match one of the authorized redirect URIs

    # for the OAuth 2.0 client, which you configured in the API Console. If this

    # value doesn't match an authorized URI, you will get a 'redirect_uri_mismatch'

    # error.

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

    code_verifier = generate_code_verifier()

    flow.code_verifier = str(code_verifier)

    flask.session['code_verifier'] = str(code_verifier)

    authorization_url, state = flow.authorization_url(

        # Enable offline access so that you can refresh an access token without

        # re-prompting the user for permission. Recommended for web server apps.

        access_type='offline',

        # Enable incremental authorization. Recommended as a best practice.

        include_granted_scopes='true')


    # Store the state so the callback can verify the auth server response.

    flask.session['state'] = state

    apobj.notify(title='Auth url',

        body=authorization_url)


    return redirect(authorization_url)

这是 oauth 回调函数:

@bp.route('/oauth2callback')

def oauth2callback():

  # Specify the state when creating the flow in the callback so that it can

  # verified in the authorization server response.



GCT1015
浏览 138回答 1
1回答

波斯汪

google-api-python-client 查找默认不包含 mybusiness v4 的发现文档。您可以在此处找到代码https://github.com/googleapis/google-api-python-client/blob/master/googleapiclient/discovery.py您可以使用 discoveryServiceUrl 参数指定发现文档。将其设置为:discoveryServiceUrl='https://developers.google.com/my-business/samples/mybusiness_google_rest_v4p5.json'您的完整版本应如下所示:business = googleapiclient.discovery.build(         API_SERVICE_NAME, API_VERSION, credentials=credentials, discoveryServiceUrl='https://developers.google.com/my-business/samples/mybusiness_google_rest_v4p5.json')
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python