Search code examples
c#oauth-2.0google-apigoogle-my-business-apioauth2-playground

Google My Business API - Get Reviews PERMISSION_DENIED Error


I'm trying to work with Google My Business API. I was able to successfully setup the OAuth 2.0 Playground to work and some simple C# code using the Google.Apis.MyBusinessAccountManagement.v1 library. Now that I have those 2 things working I'm trying to move on to my goal which is get a list of reviews for my business. For the C# library, the MyBusinessAccountManagementService object doesn't have any methods for reviews. So I researched a bunch and found an API call to a different endpoint https://mybusiness.googleapis.com/v4/accounts/{accountId}/locations/{locationId}/reviews and decided to try with the Oauth2.0 Playground using both the https://www.googleapis.com/auth/business.manage and https://www.googleapis.com/auth/plus.business.manage scopes; but for some reason on this endpoint I get the PERMISSION_DENIED error (see below). I already went over the process to fill out the form to get my project associated with the Business and the API is set as Enabled (see image)

{
  "error": {
    "status": "PERMISSION_DENIED", 
    "message": "Google My Business API has not been used in project {projectId} before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/mybusiness.googleapis.com/overview?project={projectId} then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.", 
    "code": 403, 
    "details": [
      {
        "@type": "type.googleapis.com/google.rpc.Help", 
        "links": [
          {
            "url": "https://console.developers.google.com/apis/api/mybusiness.googleapis.com/overview?project={projectId}", 
            "description": "Google developers console API activation"
          }
        ]
      }, 
      {
        "reason": "SERVICE_DISABLED", 
        "@type": "type.googleapis.com/google.rpc.ErrorInfo", 
        "domain": "googleapis.com", 
        "metadata": {
          "consumer": "projects/{projectId}", 
          "service": "mybusiness.googleapis.com"
        }
      }
    ]
  }
}

My Business API Enabled

All APIs Enabled


Solution

  • I found out what I was doing wrong. In the OAuth 2.0 Playground, there's a settings button where you need to enter your application's Client ID and Secret; I did that the first time forgot every time after that; so I was using some default Application for testing instead of my own. After adding those two I kept working and found out that I had the wrong location ID. The correct way to get your location ID is once you are a manager of a Business Location you need to go to the Location Manager page on Google and find the Business Profile ID under Advanced Settings. Don't use the Google Maps tool to get the location ID for Google Maps those are different IDs!

    enter image description here


    EDIT 01/19/2023

    I was able to get the Google API to work with my request in C# code!

    using Google.Apis.Auth.OAuth2;
    using Google.Apis.MyBusinessAccountManagement.v1;
    using Google.Apis.Services;
    using Microsoft.Extensions.Configuration;
    using Microsoft.Extensions.Hosting;
    
    HttpClient client = new HttpClient();
    
    using IHost host = Host.CreateDefaultBuilder(args).Build();
    var configuration = new ConfigurationBuilder().AddJsonFile($"appsettings.json", optional: false, reloadOnChange: true);
    var config = configuration.Build();
    
    Console.WriteLine($"Hello, World! You are in the {config.GetValue<string>("General:Environment")} Environment");
    
    UserCredential credential;
    using (var stream = new FileStream("client_secrets.json", FileMode.Open, FileAccess.Read))
    {
        credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
            GoogleClientSecrets.FromStream(stream).Secrets,
            new[] { "https://www.googleapis.com/auth/business.manage" },
            "user", CancellationToken.None);
    }
    
    var service = new MyBusinessAccountManagementService(new BaseClientService.Initializer()
    {
        HttpClientInitializer = credential,
        ApplicationName = "{ProjectName}",
        ApiKey = "{API Key}"
    });
    
    HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "https://mybusiness.googleapis.com/v4/accounts/{accountid}/locations/{locationid}/reviews");
    var response = await service.HttpClient.SendAsync(request);
    var json = await response.Content.ReadAsStringAsync();
    Console.WriteLine(json);
    
    // API call to get Accounts
    // var accounts = await service.Accounts.List().ExecuteAsync();
    //Console.WriteLine(accounts.ToString());
    
    Console.ReadLine();
    
    await host.RunAsync();