0

저는 최근에 Google Apis를 사용해 보았습니다. 나는 지난 2 시간 동안 API 액세스를 허용하는 전자 메일을받는 방법과 토큰을 자동으로 새로 고치는 방법을 검색하는 데 썼습니다.C# Google OAuth2는 현재 로그인 한 사용자의 이메일을받습니다.

내 코드는 튜토리얼을 검색 한 후

class GoogleAPI 
    { 
     // If modifying these scopes, delete your previously saved credentials 
     // at ~/.credentials/sheets.googleapis.com-dotnet-quickstart.json 
     static string[] Scopes = { SheetsService.Scope.Spreadsheets }; 
     static string ApplicationName = "Google Sheets API .NET Quickstart"; 

     public GoogleAPI() 
     { 
      UserCredential credential; 

      using (var stream = 
       new FileStream("Secret.json", FileMode.Open, FileAccess.Read)) 
      { 
       string credPath = System.Environment.GetFolderPath(
        System.Environment.SpecialFolder.Personal); 
       credPath = Path.Combine(credPath, ".credentials/Secret_Credentials.json"); 

       credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
        GoogleClientSecrets.Load(stream).Secrets, 
        Scopes, 
        "user", 
        CancellationToken.None, 
        new FileDataStore(credPath, true)).Result; 
       Console.WriteLine("Credential file saved to: " + credPath); 
      } 

      // Create Google Sheets API service. 
      var service = new SheetsService(new BaseClientService.Initializer() 
       { 
        HttpClientInitializer = credential, 
        ApplicationName = ApplicationName, 
       }); 

      // Define request parameters. 
      String spreadsheetId = "1Jd9NgmnfvJzPtjXw_jNzSUAlOO532mF6ebYyIIle_H8"; 
      String range = "Sheet1!A:L"; 
      SpreadsheetsResource.ValuesResource.GetRequest request = 
        service.Spreadsheets.Values.Get(spreadsheetId, range); 

      ValueRange response = request.Execute(); 

      var a = response; 
      System.Windows.Forms.MessageBox.Show("success"); 

     } 
    } 

같은 꽤 많이 있습니다, 난 내 토큰이 자동으로 새로 고쳐 만료 때 가정, 그래서 내가 한 문제가 남아 있어요.

문제 : 자격 증명에 저장된 사용자의 전자 메일을받는 방법.

정말 신기하니 정말 자세히 설명해주세요. @DaImTo


덕분에 나는 마지막으로 추가하는 방법을 이해합니다. 그의 자습서 ->http://www.daimto.com/find-users-email-with-google-api/

코드가 작동하도록하십시오.

먼저 "이메일"을 범위로 추가하고 이전 자격 증명을 삭제해야합니다.

static string[] Scopes = { SheetsService.Scope.Spreadsheets, "email" }; 

둘째, Google 플러스 서비스를 만드십시오. 정확하게 이메일을 원하지 않는 이유

// Create Google Plus API service. 
var plusService = new PlusService(new BaseClientService.Initializer() 
{ 
    HttpClientInitializer = credential, 
    ApplicationName = ApplicationName, 
}); 

그리고 마지막으로, 이메일

var me = plusService.People.Get("me").Execute(); 
var useremail = me.Emails.FirstOrDefault().Value; 

답변

0

를 얻을?

액세스 토큰을 사용하면 API에 액세스 할 수 있습니다. 새로 고침 토큰은 FileDataStore가 수행하는 만료시 (1 시간) 액세스 토큰을 자동으로 업데이트합니다.

credPath 경로를 올바르게 읽으면 파일 데이터 저장소가 % appData %에 사용자의 자격 증명을 이미 저장했습니다.

현재 인증 된 사용자의 이메일을받는 유일한 방법은 Google+ api People.get이 나에게 보내는 것입니다. 요청에 다른 범위를 추가해야합니다.

사용자 ID 값 "나"를 사용하면,이 방법은 범위의 OAuth https://www.googleapis.com/auth/plus.login 또는 https://www.googleapis.com/auth/plus.me 부여 된 토큰을 사용하여 인증을 요구하는 경우 . OAuth에 대해 더 자세히 읽어보십시오.

코드 :

Nuget 패키지 :

PM> Install-Package Google.Apis.Plus.v1 

코드

var me = plusService.People.Get("me").Execute(); 
var useremail = me.Emails.FirstOrDefault().Value; 
+0

애플 리케이션 여러 사람이 사용하고, 내가에 데이터를 삽입 한 누가 알고 있었다됩니다 스프레드 시트. 또한 당신은 새로 고침 토큰이 자동으로 액세스 토큰을 업데이 트 것이라고 말했다, 그래서 사용자가 다시 동의/부탁을 요청 못해? – Neolitz

+0

사용자 로그인 이름을 사용하여 "user"로 보냅니다. 각 사용자 데이터는 그에 의해 저장됩니다. – DaImTo

+0

도움이 될 것입니다. http://www.daimto.com/google-net-filedatastore-demystified/ 가능한 한 통증이 있다면 이메일을 사용하지 않는 것이 좋습니다. – DaImTo