Gmail API Service account


15 de Septiembre del 2016

I want to read my gmail inbox using Gmail API. I need to use a service account due my application haven't user interaction. I get a following error on request:

"InnerException = {"Error:"unauthorized_client", Description:"Unauthorized client or scope in request.", Uri:"""} "

This is my code:

string applicationName = "Gmail API .NET";
string[] scopes = { GmailService.Scope.GmailReadonly };

string certPath = "./XXXXXXXXXX.p12";
string userEmail = "[email protected]";
string serviceAccountEmail = "MYSERVICEACCOUNT...am.gserviceaccount.com";

//Carga el certificado obtenido de 
var certificate = new X509Certificate2(certPath, "notasecret", X509KeyStorageFlags.Exportable);

ServiceAccountCredential credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(serviceAccountEmail)
{
User = userEmail,
Scopes = scopes
}.FromCertificate(certificate)
);

if (credential.RequestAccessTokenAsync(CancellationToken.None).Result) 


What am I doing wrong? Can anybody help me?

Regards