Created
January 21, 2024 07:35
-
-
Save Isabellae4567/ba5682236efc31c4d80f7bbb518527cb to your computer and use it in GitHub Desktop.
To add the location of a service through Google My Business (GMB) in your custom service-based website
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| To add the location of a service through Google My Business (GMB) in your custom service-based website, you can use the Google My Business API. Below is a simplified example using Python and the Google API client library. Before you proceed, make sure you have set up a project in the Google Cloud Console, enabled the Google My Business API, and obtained the necessary credentials. | |
| import google.auth | |
| from google.auth.transport.requests import Request | |
| from google.oauth2.credentials import Credentials | |
| from googleapiclient.discovery import build | |
| # Load credentials from the file generated after setting up API access | |
| creds = None | |
| token_file = 'path/to/token.json' # Update with your token file path | |
| if os.path.exists(token_file): | |
| creds = Credentials.from_authorized_user_file(token_file) | |
| # If there are no (valid) credentials available, let the user log in. | |
| if not creds or not creds.valid: | |
| if creds and creds.expired and creds.refresh_token: | |
| creds.refresh(Request()) | |
| else: | |
| # Authenticate the user | |
| flow = InstalledAppFlow.from_client_secrets_file( | |
| 'path/to/client_secret.json', # Update with your client secret file path | |
| ['https://www.googleapis.com/auth/business.manage'], | |
| ) | |
| creds = flow.run_local_server(port=0) | |
| # Save the credentials for the next run | |
| with open(token_file, 'w') as token: | |
| token.write(creds.to_json()) | |
| # Build the GMB service | |
| gmb_service = build('mybusiness', 'v4', credentials=creds) | |
| # Define the location information | |
| location_data = { | |
| "locationName": "Your Location Name", | |
| "primaryCategory": {"displayName": "Your Service Category"}, | |
| "websiteUrl": "https://www.yourwebsite.com", | |
| "address": { | |
| "regionCode": "US", | |
| "postalCode": "Your Postal Code", | |
| "administrativeArea": "Your State", | |
| "locality": "Your City", | |
| "addressLines": ["Street Address Line 1", "Street Address Line 2"], | |
| }, | |
| } | |
| # Create a new location | |
| response = gmb_service.accounts().locations().create( | |
| parent="accounts/YOUR_ACCOUNT_ID", # Update with your GMB account ID | |
| body={"location": location_data}, | |
| ).execute() | |
| # Print the newly created location ID | |
| print("Location created. Location ID:", response["name"]) | |
| Make sure to replace placeholders like 'path/to/token.json', 'path/to/client_secret.json', and 'YOUR_ACCOUNT_ID' with the actual paths and values specific to your setup. | |
| Additionally, ensure that you have the necessary API access and permissions for the specified account. For more details on the Google My Business API, refer to the official documentation: https://developers.google.com/my-business/reference/rest/v4/accounts.locations/create |
Author
I just added this code to my site that’s all about the 7 Brew menu. You should definitely check it out — it’s a quick example of how it looks in action.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I have added this code in my website related to Limo service toronro. You must visit this to see a sample.