I am trying to access Google Analytics API V3 from a .Net Console App.
I am calling the List() under Management.Accounts.
Every time I try to invoke his API method , it gives an Insufficient Permission Error 403 as given below:-
Google.Apis.Requests.RequestError
Insufficient Permission [403]
Errors [ Message [Insufficient Permission] Location[-] Reason[InsufficientPermissions] Domain[global]]
I have correctly setup the Project at Google Developer Console with all the required settings.
Also, I am able to access the accounts for the user when I login to the Google Analytics page as I am the Admin
Here is the code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Threading;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Analytics.v3;
using Google.Apis.Analytics.v3.Data;
using Google.Apis.Services;
using Google.Apis.Util.Store;
namespace Books.ListMyLibrary
{
/// <summary>
/// Sample which demonstrates how to use the Books API.
/// <summary>
internal class Program
{
[STAThread]
static void Main(string[] args)
{
Console.WriteLine("================================");
try
{
new Program().Run().Wait();
}
catch (AggregateException ex)
{
foreach (var e in ex.InnerExceptions)
{
Console.WriteLine("ERROR: " + e.Message);
}
}
Console.WriteLine("Press any key to continue...");
Console.ReadKey();
}
private async Task Run()
{
UserCredential credential;
using (var stream = new FileStream("client_secrets.json", FileMode.Open, FileAccess.Read))
{
credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
GoogleClientSecrets.Load(stream).Secrets,
new[] { AnalyticsService.Scope.Analytics},
"user", CancellationToken.None, new FileDataStore("Books.ListMyLibrary"));
}
// Create the service.
var service = new AnalyticsService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "GA-AUL-Console-Analytics-1", // Name of the project created in Google Developer Console
});
Accounts res1 = await service.Management.Accounts.List().ExecuteAsync(); //This is where it gives the error in browser
int totalResults = (int)res1.TotalResults;
}
}
}
Few Important Notes:
1) I am able to get the response from the Books API on using the same code
2) I am getting the same insufficient permission error even when I try to use the sample code available for using the ServiceAccountCredential instead of using the UserCredential stuff.
Any help on this please?
Thanks,
Regards,
Sumeet.