在Flask应用程序上使用OAuth访问Google My Business API的问题

2024-04-26 00:12:18 发布

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

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

我用同样的程序尝试了googledriveapi,它成功了。我还确保在google开发者控制台中启用了googlemybusinessapi。在

授权功能如下:

@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回调函数:

^{pr2}$

以下是creds_-to-tu-dict方法:

^{3}$

以下是“位置”方法中的阻塞位置:

@bp.route('/locations', methods=['GET','POST'])
@roles_required(['Admin'])
def locations():
    # Use the discovery doc to build a service that we can use to make
    # MyBusiness API calls, and authenticate the user so we can access their
    # account
    if 'credentials' not in flask.session:
        return flask.redirect('authorize')

  # Load credentials from the session.
    credentials = google.oauth2.credentials.Credentials(
      **flask.session['credentials'], developerKey={api-key})

    business = googleapiclient.discovery.build(
       API_SERVICE_NAME, API_VERSION, credentials=credentials)

以下是全局定义的范围和api详细信息:

SCOPES = ['https://www.googleapis.com/auth/business.manage']
API_SERVICE_NAME = 'mybusiness'
API_VERSION = 'v4'

我希望api能够连接并允许api请求。在


Tags: the函数apiurlflasksessioncodeflow
1条回答
网友
1楼 · 发布于 2024-04-26 00:12:18

googleapi python客户端查找默认情况下不包含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'

您的完整构建应该如下所示:

^{pr2}$

相关问题 更多 >