0

我正在尝试在烧瓶应用程序上访问 Google My Business API,但遇到了问题。我已经使用授权和 oauth-callback 函数设置了 O-Auth 过程。oauth 声称一切正常,因为它完成了 oauth-callback 函数并重定向到位置方法。我在构建函数中添加了一个开发者 api 密钥。当我尝试使用构建功能建立与 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.

    state = flask.session['state']
    flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(
        CLIENT_SECRETS_FILE, scopes=SCOPES, state=state)
    flow.redirect_uri = flask.url_for('google_questions.oauth2callback', _external=True)
    flow.code_verifier = flask.session['code_verifier']
    # Use the authorization server's response to fetch the OAuth 2.0 tokens.
    authorization_response = flask.request.url
    flow.fetch_token(authorization_response=authorization_response)

    # Store credentials in the session.
    # ACTION ITEM: In a production app, you likely want to save these
    #              credentials in a persistent database instead.
    credentials = flow.credentials
    flask.session['credentials'] = credentials_to_dict(credentials)

    return flask.redirect(flask.url_for('google_questions.locations'))

这是 creds_to_dict 方法:

def credentials_to_dict(credentials):
    return {'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}

这是它在位置方法中窒息的地方:

@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 请求。

4

1 回答 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')
于 2019-09-09T19:53:51.780 回答