Search code examples
pythonflaskoauth-2.0google-my-business-api

Issue with accessing Google My Business API on Flask App Using O-Auth


I am trying to access the Google My Business API on a flask app, and having trouble. I have set up the O-Auth procedure with an authorize and oauth-callback functions. The oauth claims to have gone through fine, becuase it finishes the oauth-callback function and redirects to the locations method. I added a developer api key to the build function. When I try and build the connection to the api using the build function i get this: googleapiclient.errors.UnknownApiNameOrVersion: name: mybusiness version: v4. Im pretty sure are the right api details, because in a command line version without oauth that api name and version number works. Im stuck and I think the error message could be a little misleading, maybe something is wrong with my oauth proccess. I am doing is incorrect?

I have tried the google drive api using the same procedure and it worked. I have also made sure the google my business api is enabled in the google developers console.

Here is the authorize function:

@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)

Here is the oauth-callback function:

@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'))

Here is the creds_to_dict method:

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}

Here is where it chokes in the locations method:

@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)

These are the scopes and api details defined globally:

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

I expect the api to connect and allow for api requests.


Solution

  • The google-api-python-client looks for a discovery document thats default does not have the mybusiness v4 included. You can find the code here https://github.com/googleapis/google-api-python-client/blob/master/googleapiclient/discovery.py You can specify a discovery document with the discoveryServiceUrl parameter. set it to this:

    discoveryServiceUrl='https://developers.google.com/my-business/samples/mybusiness_google_rest_v4p5.json'
    

    your full build should look like this:

    business = googleapiclient.discovery.build(
            API_SERVICE_NAME, API_VERSION, credentials=credentials, discoveryServiceUrl='https://developers.google.com/my-business/samples/mybusiness_google_rest_v4p5.json')