I'm building a web service that needs to read a normal Gmail inbox (not part of a domain).
Code:
String serviceAccountEmail = "1234567890@developer.gserviceaccount.com";
var certificate = new X509Certificate2(@"key.p12", "notasecret", X509KeyStorageFlags.Exportable);
ServiceAccountCredential credentials = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer(serviceAccountEmail)
{
Scopes = new string[] { GmailService.Scope.GmailModify },
User = "user@gmail.com"
}.FromCertificate(certificate));
var service = new GmailService(new BaseClientService.Initializer()
{
HttpClientInitializer = credentials,
ApplicationName = ApplicationName,
});
UsersResource.MessagesResource.ListRequest allMessages = service.Users.Messages.List("me");
IList<Message> messages = allMessages.Execute().Messages;
Error:
An unhandled exception of type 'Google.Apis.Auth.OAuth2.Responses.TokenResponseException' occurred in Google.Apis.dll
Additional information: Error:"unauthorized_client", Description:"Unauthorized client or scope in request.", Uri:""
I can't see any reason why this doesn't work but after reading this, it seems you can't use service account credentials on a personal @gmail account. Does anybody know if this is true or what I'm doing wrong?
Any help is appreciated!
Update
If I change the Scope
to GmailService.Scope.GmailReadonly
, I can view the mail but am unable to modify the labels which is a requirement for me.