使用 OAuth 在 Flask 应用程序上访问 Google 我的业务 API 时出现问题
Issue with accessing Google My Business API on Flask App Using O-Auth
我正在尝试在 Flask 应用程序上访问 Google 我的业务 API,但遇到了问题。我已经使用授权和 oauth 回调函数设置了 O-Auth 过程。 oauth 声称一切顺利,因为它完成了 oauth-callback 函数并重定向到 locations 方法。我在构建函数中添加了一个开发者 api 键。当我尝试使用构建函数建立与 api 的连接时,我得到了这个:googleapiclient.errors.UnknownApiNameOrVersion: name: mybusiness version:
v4.我很确定 api 细节是正确的,因为在没有 oauth 的命令行版本中 api 名称和版本号有效。我卡住了,我认为错误消息可能有点误导,也许我的 oauth 过程有问题。我这样做不对吗?
我已经用相同的步骤尝试了 google 驱动器 api,并且成功了。我还确保 google 我的业务 api 在 google 开发人员控制台中启用。
这里是授权函数:
@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 请求。
google-api-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'
您的完整构建应如下所示:
business = googleapiclient.discovery.build(
API_SERVICE_NAME, API_VERSION, credentials=credentials, discoveryServiceUrl='https://developers.google.com/my-business/samples/mybusiness_google_rest_v4p5.json')
我正在尝试在 Flask 应用程序上访问 Google 我的业务 API,但遇到了问题。我已经使用授权和 oauth 回调函数设置了 O-Auth 过程。 oauth 声称一切顺利,因为它完成了 oauth-callback 函数并重定向到 locations 方法。我在构建函数中添加了一个开发者 api 键。当我尝试使用构建函数建立与 api 的连接时,我得到了这个:googleapiclient.errors.UnknownApiNameOrVersion: name: mybusiness version: v4.我很确定 api 细节是正确的,因为在没有 oauth 的命令行版本中 api 名称和版本号有效。我卡住了,我认为错误消息可能有点误导,也许我的 oauth 过程有问题。我这样做不对吗?
我已经用相同的步骤尝试了 google 驱动器 api,并且成功了。我还确保 google 我的业务 api 在 google 开发人员控制台中启用。
这里是授权函数:
@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 请求。
google-api-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'
您的完整构建应如下所示:
business = googleapiclient.discovery.build(
API_SERVICE_NAME, API_VERSION, credentials=credentials, discoveryServiceUrl='https://developers.google.com/my-business/samples/mybusiness_google_rest_v4p5.json')