GoogleMyBusiness Module
Allows you to seamlessly synchronize locations from the website with GoogleMyBusiness. Changes to a location (for example: opening times, holidays etc.) are automatically synchronized with GoogleMyBusiness.
Implementation
- We use the client library for .NET. for MyBusinessBusinessInformationAPI NuGet / Docs
- We use the client library for .NET. for AccountManagementAPI NuGet / Docs
- We use the client library for .NET. for OAuth2.0 NuGet / More info
Installation
Add the Module to your Project
You can install the Analytics module from NuGet with
dotnet add package Confinity.GoogleMyBusiness
In your Startup.cs you can now register the module with the module builder by calling the AddModule method with GoogleMyBusinessModule. See Modules documentation for more information.
Add the service via Dependency Injection
public class MyClass
{
private readonly IGoogleMyBusinessService _myBusinessService;
public MyClass(IGoogleMyBusinessService myBusinessService)
{
_myBusinessService = myBusinessService;
}
}
Authentication
OAuth 2.0
We use the OAuth 2.0 protocol for authentication. The OAuth 2.0 protocol allows a user to grant a third-party web site or application access to the user's protected resources, without necessarily revealing their long-term credentials or even their identity. Via a consent screen, the user is asked to authorize the application to access resources owned by the user and shared by the resource server.
OAuth 2.0 Setup
- Create Oauth2.0 Client credentials for the API in Google Cloud Platform(GCP) for the project.
- Configure the ApplicationType, redirectUri and originUri in the create Oauth2.0 credentials page in GCP.
- Create a consent screen in GCP(type = external).
- The consent screen will automatically start in test mode and only users added to the test users list can access.
- You need to add test users to the consent screen when in test mode.
Important
Once you are ready to go live you need to submit the consent screen for review and it will be available for all users.
Configure GoogleMyBusiness in the Settings App
- You need to add the Oauth2.0 credentials(Client secrets) and the redirectUri to the GoogleMyBusiness settings in the Settings app in the Admin Panel.
- You can get the Client secrets(json) and the redirectUri from the Oauth2.0 credentials page in GCP.
- An AuthorizationUrl will be generated and displayed in the GoogleMyBusiness settings in the Settings app.
- This will start the authorization flow and the user will be redirected to the consent screen.
- After allowing access to the application, The user will be redirected to the redirectUri. Google will send a string representing the RefreshToken to the redirectUri.
- The user needs to copy and paste the RefreshToken into the GoogleMyBusiness settings in the Settings app.
Important
Only once both, the Client Secrets and the RedirectUri are added to the GoogleMyBusiness settings in the Settings app, an authorization url will be generated and displayed in the GoogleMyBusiness settings in the Settings app.
Create a Controller for the redirectUri
Example for a controller that handles the redirectUri https://example.com/api/v1.0/gmb/auth:
[Route("/api/v1.0/gmb/")]
[AllowAnonymous]
public class GoogleMyBusinessController : Controller
{
private readonly IGoogleMyBusinessService _googleMyBusinessService;
public GoogleMyBusinessController(IGoogleMyBusinessService googleMyBusinessService)
{
_googleMyBusinessService = googleMyBusinessService;
}
[HttpGet("auth")]
public async Task<IActionResult> AuthCallback([FromQuery] string code)
{
var token = await _googleMyBusinessService.GenerateTokenFromAuthCodeAsync(code);
if (string.IsNullOrWhiteSpace(token)){
var htmlContentError = "<html><body><h2>RefreshToken:</h2><p>Error generating RefreshToken</p></body></html>";
return Content(htmlContentError, "text/html");
}
var htmlContent = $"<html><body><h2>RefreshToken:</h2><p>{token}</p> <p><small>Copy the RefreshToken and save it in GoogleMyBusiness settings App <br />You can close this window!</small></p></body></html>";
// Return the HTML response with the token for the user
return Content(htmlContent, "text/html");
}
}
OAuth 2.0 Authorization Flow
We use the OAuth 2.0 Authorization Code Flow. This flow is suitable for long-running applications in which the user grants permission only once. It provides an access token that can be refreshed. We use RefreshTokens to keep creating new AccessTokens so we can use the API without the need to reauthorize a user.
Important
- RefreshTokens should NOT expire when consent screen is in PRODUCTION mode.
- RefreshTokens expire after 7 Days when consent screen is in TEST mode.
- RefreshTokens expire if the authorized user changes his google password
- RefreshTokens expire if the authorized user revokes access to the application manually
- RefreshTokens expire if they have not been used for 6 months
BusinessProfile APIs
MyBusinessBusinessInformationAPI
- Add My Business Business Information API to the project and active it in the GCP project.
Important
- If the Quota is 0, you need to request an increase for the quota.
- You can check the quota in the Google Cloud Platform under APIs & Services -> Dashboard.
- You can request an increase in the quota panel of the API.
My Business Account Management API
- Add My Business Account Management API to the project and active it in the GCP project.
Important
- If the Quota is 0, you need to request an increase for the quota.
- You can check the quota in the Google Cloud Platform under APIs & Services -> Dashboard.
- You can request an increase in the quota panel of the API.
How to use
Methods
Generate authorization url:
var authorizationUrl = await _myBusinessService.GetAuthorizationUrlAsync();
Generate token from authorization code:
var token = await _myBusinessService.GenerateTokenFromAuthCodeAsync(code);
Get all locations:
var allLocations = await _myBusinessService.GetLocationsAsync();
Update locations:LocationUpdateMaskEnum holds all the fields that you want to update.
var locationsToUpdateDtos = new List<LocationToUpdateDto>() { new(locationToBeUpdated, new List<LocationUpdateMaskEnum>()
{
LocationUpdateMaskEnum.regularHours,
LocationUpdateMaskEnum.storeCode,
LocationUpdateMaskEnum.title,
}) };
await _myBusinessService.UpdateLocationsAsync(locationsToUpdateDtos);