Search code examples
pythongoogle-cloud-platformoauthgoogle-my-business-api

UnknownApiNameOrVersion: name: mybusiness version: v4


I'm trying to access accounts and locations from my Google MyBusiness API. I defined an OAuth2 key and authorized APIs in GCP console, once I run my python code run.py I get this error :

googleapiclient.errors.UnknownApiNameOrVersion: name: mybusiness version: v1

run.py

from google.cloud import bigquery
from pathlib import Path

from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from libs.gcp import gcp
import google.auth
from googleapiclient.discovery import build

 def run():

    SCOPES = ["https://www.googleapis.com/auth/business.manage"]

    creds = None
    if Path("token.json").exists():
        creds = Credentials.from_authorized_user_file("token.json", SCOPES)
        print("m")
        print(creds)
        # Build the API client
        service = build("mybusiness", "v1", credentials=creds)

        # Get the list of locations for the authenticated account
        locations = service.accounts().locations().list(
            parent='accounts/112827745/locations'.format(account_id='myaccount')).execute()

        # Print the list of location names
        for location in locations.get('locations', []):
            print(location.get('name'))
    if not creds or not creds.valid:
        if creds and creds.expired and creds.refresh_token:
            creds.refresh(Request())
        else:
            flow = InstalledAppFlow.from_client_secrets_file("credentials.json", SCOPES)
            creds = flow.run_local_server(port=0)
            print(creds)
        with open("token.json", "w") as token:
            token.write(creds.to_json())

I tried to run with service = build("mybusiness", "v4", credentials=creds) and service = build("mybusiness", "v4.0", credentials=creds) but got the same error.

My API is well configured because I am able to get data from postman so it's not a token or API problem.

Does anyone see the solution? Ty


Solution

  • For retrieving accounts and locations, try "mybusinessbusinessinformation" as your service name instead of "mybusiness".

    Like this:
    service = build("mybusinessbusinessinformation", "v1", credentials=creds)

    I found that name by looking at the service name on this GBP documentation page