2017-03-14 8 views
4
내가 서비스 계정을 사용하여 Gmail의 API를 인증하려고 할 때 나는 다음과 같은 오류를 얻고있다

를 사용하여 액세스 토큰을 검색 할 권한이없는 것입니다클라이언트가이 방법의 Gmail API의 C에게 #

"클라이언트가이 방법을 사용하여 액세스 토큰을 검색 할 권한이없는 것입니다"

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 = "[email protected]"; 

     ServiceAccountCredential credential = new ServiceAccountCredential(
      new ServiceAccountCredential.Initializer(serviceAccountEmail) 
      { 
       User = "[email protected]", 
       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("[email protected]"); 
     emailListRequest.LabelIds = "INBOX"; 
     emailListRequest.IncludeSpamTrash = true; 
     emailListRequest.Q = "from:[email protected] is:unread"; 



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

난 내 콘솔 occurs.Any 도움 정말 감사하겠습니다 다음과 같은 오류가되는 앱 실행하면 서비스 account.But를 만드는 동안 내가 가진 P12 키를 사용하고 있습니다.

미리 감사드립니다.

+0

을 확인 승인하지 않은 수단? https://developers.google.com/identity/protocols/OAuth2ServiceAccount#delegatingauthority – DaImTo

+0

조직의 관리자 만 할 수 있습니까? 나는 단지 직원 일 뿐이다. – Melvin

+0

네, 관리자 만이 할 수있는 일이라고 생각합니다. – DaImTo

답변

4

서비스 계정에 권한이 부여되어야하거나 도메인의 전자 메일에 액세스 할 수 없습니다.

"클라이언트는이 방법을 사용하여 액세스 토큰을 검색 할 권한이없는 것입니다"

당신이 제대로 당신이 gsuite에 서비스 계정을 추가 했 Delegating domain-wide authority to the service account

+0

고마워요 그 일을 내가 올바른 것으로 표시 – Melvin

+0

일반 사용자가 서비스 계정 인증을 통해 Gmail API에 액세스 할 수 있습니까? – Hariprasath

+2

서비스 계정은 GSuite에서만 작동합니다. 서비스 계정을 사전 인증하고 사용자 계정에 대한 액세스 권한을 부여해야하기 때문입니다. 일반 사용자 Gmail 계정을 사전 인증 할 수있는 방법은 없습니다. 그래서 일반 사용자의 Gmail 계정으로 서비스 계정을 사용할 수 없습니다. – DaImTo