I need to make a windows service which daily exports contacts from a database to my Google Contacts Account.
Since the application is a service and the account is mine, I don't want/need to use browser authentication, but I managed to set up a service account.
I'm testing it but, right now, when I request the access token I get the "Requested client not authorized" error message
First off, the configuration: I have my "destination" account, the one I want the contacts to be imported, which is [email protected]
In the Developer Console, being logged as [email protected], I created new credentials -> Service Account Key as [email protected]; I downloaded the .p12 key and enabled the Domain-wide Delegation
Then, I went in the Admin panel of the Google Apps Account, and then Security -> Show More -> Advanced settings -> Manage API Client Access
I entered the service accont client id and "https://www.google.com/m8/feeds/" as scope
Finally, the code:
var _uName = "[email protected]";
var _pWord = "password!"; //[email protected]'s password
var _aSrvE = "[email protected]";
var certificate = new X509Certificate2(@"C:\Test\GoogleContactsExporter-123456789.p12", "notasecret", X509KeyStorageFlags.Exportable);
var credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(_aSrvE)
{
Scopes = new string[] { "https://www.google.com/m8/feeds"},
User = _uName
}.FromCertificate(certificate));
var task = credential.RequestAccessTokenAsync(new CancellationToken());
task.Wait();
On task.Wait()
I get this exception:
System.TypeInitializationException: L'inizializzatore di tipo di 'LibraryTest.GoogleContactsHelper' ha generato un'eccezione. ---> System.AggregateException: Si sono verificati uno o più errori. ---> Google.Apis.Auth.OAuth2.Responses.TokenResponseException: Error:"access_denied", Description:"Requested client not authorized.", Uri:"" in Microsoft.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) in Microsoft.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccess(Task task) in Google.Apis.Auth.OAuth2.ServiceAccountCredential.d__19.MoveNext()
I think I've done everything ok, I don't know what to check more: any help would be appreciated.