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

GMP API connected with my service account using C# but not getting business accounts

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

hi

GMP API connected with my service account using C# but not getting business accounts instead of business account it is showing me some sort of weird account

accounts/accounts/100304373153396672050   i dont know which account is this i can not see this account using playground.

i am using this clientid=553334450401-roik7op4diblciij5i3ucalnqjct065h.apps.googleusercontent.com

here is my code

public void ServiceAccountCredential credentials()
{
string MybusinessServiceScope = "https://www.googleapis.com/auth/plus.business.manage";
var Scopes = new[] {"https://mybusiness.googleapis.com/v3/accounts" };

string keyFile = @"C:\Users\IZaidi\Desktop\Console-GoogleAnalytics-Service\Console-GoogleAnalytics-Service\service\gmbapi-2412154f4fb8.json";
using (var fs = new FileStream(keyFile, FileMode.Open, FileAccess.Read))
{
var credentialParameters =
NewtonsoftJsonSerializer.Instance.Deserialize<JsonCredentialParameters>(fs);
if (credentialParameters.Type != "service_account"
|| string.IsNullOrEmpty(credentialParameters.ClientEmail)
|| string.IsNullOrEmpty(credentialParameters.PrivateKey))
throw new InvalidOperationException("JSON data does not represent a valid service account credential.");
return new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(credentialParameters.ClientEmail)
{
Scopes = new[] { MybusinessServiceScope },
User = credentialParameters.ClientEmail
}.FromPrivateKey(credentialParameters.PrivateKey));
}
}

public TestBusinessAccounts()

{

  var credential = credentials();

var service = new MyBusinessService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential
});

var accountsListRequest = service.Accounts.List();
ListAccountsResponse accountsResult = accountsListRequest.Execute();
var locationsListRequest = service.Accounts.Locations.List(accountsResult.Accounts[0].Name);
ListLocationsResponse locationsResult = locationsListRequest.Execute();

}

1 Expert replyverified_user

Re: GMP API connected with my service account using C# but not getting business accounts

Google Employee
# 2
Google Employee

Hi @eGumball A,

 

You should request user consent for the Google Account that you need to access the Google My Business data from. 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. 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.

 

Thanks,

The Google My Business API team