My Business
5.2K members online now
5.2K members online now
For developers who are using the Google My Business API to manage locations
Guide Me
star_border
Reply

Scope for GoogleMyBusiness?

[ Edited ]
Visitor ✭ ✭ ✭
# 1
Visitor ✭ ✭ ✭

Hello,

 

I am developing an application that will push data from the Sitecore CMS to Google My Business using the API. I would like to use the C# classes to do this.

 

My code so far looks like this:

 

// Create credentials
ServiceAccountCredential credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(clientIdEmail)
{
Scopes = new[] {
//Need Scope
},
}.FromCertificate(certificate));


// Create the service
BaseClientService.Initializer initializer = new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "My Application",
GZipEnabled = true,
};

MybusinessService service = new MybusinessService(initializer);

 

AccountsResource.ListRequest listAccounts = service.Accounts.List();
ListAccountsResponse response = listAccounts.Execute();
List<Account> accounts = response.Accounts.ToList();
Account account = accounts[0];

Response.Write(account);

 

The problem I am running into is that I don't know if this code is written correctly and I don't know what scope to use for the GMB API.

 

Any help is appreciated. Thank you.

 

1 Expert replyverified_user
1 ACCEPTED SOLUTION

Accepted Solutions
Marked as Best Answer.
Solution
Accepted by topic author Alec L
May 2016

Re: Scope for GoogleMyBusiness?

[ Edited ]
Google Employee
# 2
Google Employee

Hi @Alec L,

 

The Google My Business API requires the following OAuth 2.0 scope:

 

Judging from the source code you provided, you tried to authenticate using a service account's credentials with the C# client library.

 

Due to the complexities involved in using service accounts, we strongly recommend using the OAuth 2.0 installed application flow and persisting the refresh token. This way, your application will always be able to request a new access token when necessary. This process requires a user to manually authorize the application during the OAuth 2.0 installed applications flow only once.

 

For a service account, your application uses the client ID and one private key to create a signed token and sends it in a request to the Google OAuth 2.0 Authorization Server for an access token to access a Google API. When the token expires, the application repeats the process. Please note that a one-time user consent is required for a service account to be able to access the Google My Business account and location data on behalf of a particular end user. The end users will have to log in with their Google Accounts to manually authorize the application using OAuth 2.0 Authorization. They can perform this process during the OAuth 2.0 installed applications flow or using OAuth 2.0 Playground. This process needs to be done only once. When you prepare to make authorized API calls using the service account, you specify the user to impersonate by specifying the email address of the user account for access to their Google My Business account and location data. If the end users later want to revoke the application’s access to their Google My Business account and location data, they can remove the authorized app from the Apps connected to your account page of their accounts.

 

The following is a code snippet for using a service account with the Google API Client Library for .NET:

           // The scope of Google My Business API

           string MybusinessServiceScope = "https://www.googleapis.com/auth/plus.business.manage";

           String serviceAccountEmail = "123456789000-abc123def456@developer.gserviceaccount.com";

 

           var certificate = new X509Certificate2(@"MyProject.p12", "notasecret", X509KeyStorageFlags.Exportable);

 

           ServiceAccountCredential credential = new ServiceAccountCredential(

              new ServiceAccountCredential.Initializer(serviceAccountEmail)

              {

                  Scopes = new[] { MybusinessServiceScope },

                  User = "user@example.com"

              }.FromCertificate(certificate));

 

           // Creates the service.

           var service = new MybusinessService(new BaseClientService.Initializer()

           {

               HttpClientInitializer = credential,

               ApplicationName = "Google My Business API Sample",

           });

 

           // Gets all accounts for the authenticated user.

           var accountsListRequest = service.Accounts.List();

           ListAccountsResponse accountsResult = accountsListRequest.Execute();

 

I hope this helps solve your problem.

 

Thanks, 

Terry

View solution in original post

Marked as Best Answer.
Solution
Accepted by topic author Alec L
May 2016

Re: Scope for GoogleMyBusiness?

[ Edited ]
Google Employee
# 2
Google Employee

Hi @Alec L,

 

The Google My Business API requires the following OAuth 2.0 scope:

 

Judging from the source code you provided, you tried to authenticate using a service account's credentials with the C# client library.

 

Due to the complexities involved in using service accounts, we strongly recommend using the OAuth 2.0 installed application flow and persisting the refresh token. This way, your application will always be able to request a new access token when necessary. This process requires a user to manually authorize the application during the OAuth 2.0 installed applications flow only once.

 

For a service account, your application uses the client ID and one private key to create a signed token and sends it in a request to the Google OAuth 2.0 Authorization Server for an access token to access a Google API. When the token expires, the application repeats the process. Please note that a one-time user consent is required for a service account to be able to access the Google My Business account and location data on behalf of a particular end user. The end users will have to log in with their Google Accounts to manually authorize the application using OAuth 2.0 Authorization. They can perform this process during the OAuth 2.0 installed applications flow or using OAuth 2.0 Playground. This process needs to be done only once. When you prepare to make authorized API calls using the service account, you specify the user to impersonate by specifying the email address of the user account for access to their Google My Business account and location data. If the end users later want to revoke the application’s access to their Google My Business account and location data, they can remove the authorized app from the Apps connected to your account page of their accounts.

 

The following is a code snippet for using a service account with the Google API Client Library for .NET:

           // The scope of Google My Business API

           string MybusinessServiceScope = "https://www.googleapis.com/auth/plus.business.manage";

           String serviceAccountEmail = "123456789000-abc123def456@developer.gserviceaccount.com";

 

           var certificate = new X509Certificate2(@"MyProject.p12", "notasecret", X509KeyStorageFlags.Exportable);

 

           ServiceAccountCredential credential = new ServiceAccountCredential(

              new ServiceAccountCredential.Initializer(serviceAccountEmail)

              {

                  Scopes = new[] { MybusinessServiceScope },

                  User = "user@example.com"

              }.FromCertificate(certificate));

 

           // Creates the service.

           var service = new MybusinessService(new BaseClientService.Initializer()

           {

               HttpClientInitializer = credential,

               ApplicationName = "Google My Business API Sample",

           });

 

           // Gets all accounts for the authenticated user.

           var accountsListRequest = service.Accounts.List();

           ListAccountsResponse accountsResult = accountsListRequest.Execute();

 

I hope this helps solve your problem.

 

Thanks, 

Terry

Re: Scope for GoogleMyBusiness?

[ Edited ]
Visitor ✭ ✭ ✭
# 3
Visitor ✭ ✭ ✭

Hi @Terry W,

Thank you for your response. It was exaclty what I was looking for and my code is working sucessfully now. I have a few other questions for you about the API if you could please answer them.

1. If we have multiple google accounts that contain Google My Business locations, is there a way to query both accounts at the same time or combine the info? Or would we have to query each account seperately?

2. Can we mark locations as closed through the API and re-open them through the API. Our locations are child care centers and their open/closed status can be a bit volatile.

3. Do Google My Business Locations support events?

4. If we push data from the API to a location, will it be automatically published, or do we have to manually publish?

5. Is there a description getter/setter in the C# API, that's the one attribute I could not find.

Thank you in advance!!

Re: Scope for GoogleMyBusiness?

[ Edited ]
Google Employee
# 4
Google Employee

Hi @Alec L,

 

1. There are two ways to manage multiple Google Accounts that contain Google My Business locations. You can either use business accounts to share access to a set of locations within a single Google Account or generate separate OAuth 2.0 tokens for each Google Account.

 

You don’t have to query each account separately if you use business accounts to share access to a large set of locations with a group of people within a single Google Account. Please check out the the best practices for using Business Accounts with Google My Business locations in the FAQ. The locations that you share in a business account will be available across multiple Google Accounts so that you should be able to access different sets of locations in a single Google Account using a single access token via the API. Keep in mind that authorized representatives must, whenever possible, encourage the business owner to create an account, own the listing, and add authorized representatives as managers. Learn more

 

For generating separate OAuth 2.0 tokens for each Google Account, when you use your whitelisted project’s credentials to obtain an OAuth 2.0 access token for authentication via the Google My Business API, you should be logging in with your specific Google Account. When this token is provided and you are making API calls, you will be able to view and manage all the existing locations within that specific account. You should do this with a single set of OAuth 2.0 credentials for your separate Google Accounts to obtain a token for each Google Account. In addition, when using the OAuth 2.0 client ID for authorization, you should store the refresh token for future use and use the access token to access the API. Once the access token expires, the application should use the refresh token to obtain a new one. This way, your application will always be able to request a new access token when necessary. This process requires a user to manually authorize the application only once.

 

2. Yes, you can mark a business as open or permanently closed by setting the OpenInfo object to the corresponding OPEN or CLOSED_PERMANENTLY status in the Location object when creating or updating a location via the API. Please note that businesses that were marked as permanently closed as part of a location or name change can't be reopened. You should create or locate the new Google listing and manage your business using that listing instead.

 

3. The Google My Business Locations and Google My Business API do not support events.

 

4. When your locations are verified, updates you make to your local business via the API are eligible to show up across Google, including on Maps, Search, and Google+. Please note that the edits that you make to your verified business information may be subject to review to ensure they follow the quality guidelines for pages as well as the Google+ content policy. Your changes may take up to 3 days to appear on Google Maps and other Google properties. You should also check your listing status to see if your locations are published on Google. You can determine the verified state of a location by checking the isVerified boolean field value of the LocationState object in the Location object through the API.

 

5. The description field is not currently supported through the Google My Business API.

 

Thanks,

The Google My Business API team

Re: Scope for GoogleMyBusiness?

Visitor ✭ ✭ ✭
# 5
Visitor ✭ ✭ ✭
Hi there,

Which parts of the API console need to be enabled a service to connect to google my business API.

I ask because Ive copied and pasted your code and its not working for me.

I also get an email for the service account to be a iam.gserviceaccount.com .. this is different to your domain in your example.

any help would be great...

Re: Scope for GoogleMyBusiness?

Visitor ✭ ✭ ✭
# 6
Visitor ✭ ✭ ✭
I get this error.
An unhandled exception of type 'Google.Apis.Auth.OAuth2.Responses.TokenResponseException' occurred in Google.Apis.dll

Additional information: Error:"invalid_grant", Description:"Invalid JWT Signature.", Uri:""

Re: Scope for GoogleMyBusiness?

Google Employee
# 7
Google Employee

Hi @Jon H,

 

According to your email address domain, your company doesn’t have a whitelisted project to access the Google My Business API, and that’s why the code snippet I posted is not working for you. The service account's email address in my code snippet is just a placeholder and doesn’t reflect the actual OAuth 2.0 service account's credentials you obtain from the Google Developers Console.

 

The Google My Business API is available for developers who represent chains and third parties. For more information about getting access to and using the Google My Business API, please check out the Prerequisites page of our Google Developers site.

 

Thanks,

Terry

Re: Scope for GoogleMyBusiness?

Visitor ✭ ✭ ✭
# 8
Visitor ✭ ✭ ✭

Hi Terry,

 

I am trying to access Google my business API but it is showing an error Request had insufficient authentication scopes. my code is below;

 

string mybusinessServiceScope = "https://www.googleapis.com/auth/plus.business.manage";
ClientSecrets secrets = new ClientSecrets()
{
ClientId = Values.ClientID,
ClientSecret = Values.ClientSecret
};

var tokenResp = new TokenResponse { RefreshToken = token };
var credentials = new UserCredential(new GoogleAuthorizationCodeFlow(
new GoogleAuthorizationCodeFlow.Initializer
{
ClientSecrets = secrets,
Scopes = new[] { mybusinessServiceScope }
}),
"user",
tokenResp);

var service = new MybusinessService(new BaseClientService.Initializer()
{
HttpClientInitializer = credentials,
ApplicationName = "My First Project"
});

// Gets all accounts for the authenticated user.
var accountsListRequest = service.Accounts.List();
ListAccountsResponse accountsResult = accountsListRequest.Execute();
if (accountsResult != null)
{
// For testing purposes, selects the very first account in the accounts array
Account account = accountsResult.Accounts[0];
}

 

 

Please help me out

Re: Scope for GoogleMyBusiness?

Google Employee
# 9
Google Employee

Hi @Irfan R,

 

Judging from the source code you provided, your error seems to be caused by an incorrect implementation of OAuth 2.0 authentication and authorization via the C# client library and the Google API Client Library for .NET because your OAuth 2.0 flow is an erroneous mix of OAuth 2.0 installed application flow and OAuth 2.0 web server application flow.

 

I suggest you follow exactly the code snippets for using an OAuth 2.0 client ID or a service account with the C# client library in this thread.

 

Thanks,

Terry

Re: Scope for GoogleMyBusiness?

Visitor ✭ ✭ ✭
# 10
Visitor ✭ ✭ ✭

Hi @Terry W,

 

Thank you. I have successfully implemented the code and it works fine.

 

Regards,

Irfan Rafiq