I’m trying to fire a request to “https://mybusiness.googleapis.com/v4/accounts/XXXXXXXXX/locations/YYYYYYYY” to get my business reviews out. I have access to the google business API and by using OAuth2 login method, to get a code out that I can then request a token. However, I want to make this a automated process, so need to use the Service account.
I believe I’ve logged in using my service account credentials, set up as a viewer in the API manager, but when I make the above call using the service I get a response: “Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.”
I’ll post my code below. Just don’t understand what I’m doing wrong and how to get it working as anything I try I get this error.
string path = HttpContext.Current.Server.MapPath(@"App_Datagoogle_credential.json");
var credJson = File.ReadAllText(path);
var cr = JsonConvert.DeserializeObject<PersonalServiceAccountCred>(credJson); // "personal" service account credential
// Create an explicit ServiceAccountCredential credential
var xCred = new ServiceAccountCredential(new ServiceAccountCredential.Initializer(cr.client_email)
{
Scopes = new[] {
"https://www.googleapis.com/auth/business.manage","https://www.googleapis.com/auth/plus.business.manage", "https://accounts.google.com/o/oauth2/v2/auth"
}
}.FromPrivateKey(cr.private_key));
// Create the service
Google.Apis.MyBusinessAccountManagement.v1.MyBusinessAccountManagementService theService = new Google.Apis.MyBusinessAccountManagement.v1.MyBusinessAccountManagementService(
new BaseClientService.Initializer()
{
HttpClientInitializer = xCred,
}
);
string accountId23 = "XXXXXXXX"; //Accountid
string location23 = "YYYYYYYY"; //locationid
string accountPath23 = "accounts/" + accountId23;
string locationPath23 = "locations/" + location23;
HttpRequestMessage request23;
HttpResponseMessage response23;
request23 = new HttpRequestMessage(HttpMethod.Get, "https://mybusiness.googleapis.com/v4/" + accountPath23 + "/" + locationPath23 + "/reviews");
response23 = await theService.HttpClient.SendAsync(request23);
string result = await response23.Content.ReadAsStringAsync();
It may be a set up thing too, but I just can’t seem to find out what it might be.