Search code examples
c#google-analyticsgoogle-apifirebase-analyticsgoogle-analytics-firebase

Google.Apis.GoogleAnalyticsAdmin.v1alpha raising insufficientPermissions when trying to get google admin analytics account detail in C#?


I wanted to get the account detail, after authenticating I added to the scope section to grant the permission, but when running the code it raised an error insufficient permission, I don't know what step I missed, how would I be able to fix this error?

private static GoogleAnalyticsAdminService _analyticsService;
        

  public static void IntializeAnalytics() {
      GoogleCredential credential = GoogleCredential.GetApplicationDefault();
            
      if (CloudManager.Credential.IsCreateScopedRequired)
      {
         credential = CloudManager.Credential.CreateScoped( 
         GoogleAnalyticsAdminService.Scope.AnalyticsReadonly, GoogleAnalyticsAdminService.Scope.AnalyticsManageUsers,
                GoogleAnalyticsAdminService.Scope.AnalyticsManageUsersReadonly, GoogleAnalyticsAdminService.Scope.AnalyticsEdit);
            }
      _analyticsService = new GoogleAnalyticsAdminService(
       new BaseClientService.Initializer()
       {
                  
           HttpClientInitializer = credential,
           ApplicationName = CloudManager.ApplicationName

       });

      var response =  _analyticsService.Accounts.Get("accounts/Default Account for Firebase").Execute();
            
}

Error message :

   Unhandled exception. The service analyticsadmin has thrown an exception.
HttpStatusCode is Forbidden.
Google.Apis.Requests.RequestError
Request had insufficient authentication scopes. [403]
Errors [
    Message[Insufficient Permission] Location[ - ] Reason[insufficientPermissions] Domain[global]
]

Google.GoogleApiException: The service analyticsadmin has thrown an exception. HttpStatusCode is Forbidden. Request had insufficient authentication scopes.
   at Google.Apis.Requests.ClientServiceRequest`1.ParseResponse(HttpResponseMessage response)
   at Google.Apis.Requests.ClientServiceRequest`1.Execute()
   

Solution

  • Request had insufficient authentication scopes.

    Means that when you authorized your application you did not request the proper scopes. Im not sure where you got that code from Its nothing like the code I normally use.

    using Google.Analytics.Admin.V1Beta;
    
    Console.WriteLine("Hello, World!");
    
    
    // Path to the service account credentials file. 
    const string credentialsJsonPath = @"C:\Development\FreeLance\GoogleSamples\Credentials\ServiceAccountCred.json";
    
    // Property on Google analytics which the service account has been granted access to.
    const string propertyId = "XXXXXX";
    
    var client = new AnalyticsAdminServiceClientBuilder()
    {
        CredentialsPath = credentialsJsonPath
    }.Build();
    
    var response = client.ListAccounts(new ListAccountsRequest());
    
    foreach (var account in response) {
        Console.WriteLine(account.Name);
    }
    
    Console.ReadLine();