Client is unauthorized to retrieve access tokens using this method Gmail API C#

我们两清 提交于 2019-11-26 09:58:32

问题


I am getting the following error when i tried to authorize gmail api using service account

\"Client is unauthorized to retrieve access tokens using this method\"

static async Task MainAsync()
    {

        sstageEntities db = new sstageEntities();
        //UserCredential credential;
        Dictionary<string, string> dictionary = new Dictionary<string, string>();    
String serviceAccountEmail =
\"xxx.iam.gserviceaccount.com\";

        var certificate = new X509Certificate2(
            AppDomain.CurrentDomain.BaseDirectory +
              \"xxx-8c7a4169631a.p12\",
            \"notasecret\",
            X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.Exportable);

        //string userEmail = \"user@domainhere.com.au\";

        ServiceAccountCredential credential = new ServiceAccountCredential(
            new ServiceAccountCredential.Initializer(serviceAccountEmail)
            {
                User = \"xxx@xxx.com\",
                Scopes = new[] { \"https://mail.google.com/\" }
            }.FromCertificate(certificate)
        );


        // Create Gmail API service.
        var gmailService = new GmailService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName = ApplicationName,
        });

        // Define parameters of request.

        var emailListRequest = gmailService.Users.Messages.List(\"xxx@xxx.com\");
        emailListRequest.LabelIds = \"INBOX\";
        emailListRequest.IncludeSpamTrash = true;
        emailListRequest.Q = \"from:bpm@xxx.co.in is:unread\";



        //Get our emails
        var emailListResponse = await emailListRequest.ExecuteAsync();

I am using the p12 key which i got while creating service account.But when i run my console app the following error occurs.Any help would be really appreciated.

Thanks in advance !


回答1:


The service account needs to be authorized or it cant access the emails for the domain.

"Client is unauthorized to retrieve access tokens using this method"

Means that you have not authorized it properly check Delegating domain-wide authority to the service account



来源:https://stackoverflow.com/questions/42784640/client-is-unauthorized-to-retrieve-access-tokens-using-this-method-gmail-api-c-s

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!