2012-07-10 2 views
4

나는 Google Contacts Api을 사용하고 있습니다. 매개 변수로 Auth Token을 보낼 수 있는지 여부는 확실하지 않습니다.Google 연락처 사용 API 가져 오기 연락처 C#

string _token = _google.Token; 
RequestSettings requestSettings = new RequestSettings("AppName",_token); 
ContactsRequest contactsRequest = new ContactsRequest(requestSettings); 

// Get the feed 
Feed<Contact> feed = contactsRequest.GetContacts(); 

는이 코드에 대한 응답으로 401 Unauthorised를 얻을,하지만 난 매개 변수로 사용자 이름과 암호를 보내는 경우, 나는 응답을 얻을 수 있어요.

답변

4

죄송합니다. 처음에는 제대로 이해하지 못했습니다. 실제 응용 프로그램에서이 코드를 사용하고 있습니다. 토큰을 지속적으로 새로 고침하기 때문에 코드에서 약간 다른 작업을 수행합니다. 어떤 경우
, 여기에 적절한 논리입니다 :

 // get this information from Google's API Console after registering your app 
     var parameters = new OAuth2Parameters 
     { 
      ClientId = @"", 
      ClientSecret = @"", 
      RedirectUri = @"", 
      Scope = @"https://www.google.com/m8/feeds/", 
     }; 

     // generate the authorization url 
     string url = OAuthUtil.CreateOAuth2AuthorizationUrl(parameters); 

     // now use the url to authorize the app in the browser and get the access code 
     (...) 

     // get this information from Google's API Console after registering your app 
     parameters.AccessCode = @"<from previous step>"; 

     // get an access token 
     OAuthUtil.GetAccessToken(parameters); 

     // setup connection to contacts service 
     var contacts = new ContactsRequest(new RequestSettings("<appname>", parameters)); 

     // get each contact 
     foreach (var contact in contacts.GetContacts().Entries) 
     { 
      System.Diagnostics.Debug.WriteLine(contact.ContactEntry.Name.FullName); 
     } 

를 참고로, 당신은 당신의 액세스 코드에 대해 GetAccessToken()를 호출 한 후, 당신의 매개 변수 데이터 구조가 AccessTokenRefreshToken 필드가 포함됩니다. 이 두 값을 저장하면 나중에 호출 할 때 매개 변수 구조에 매개 변수 구조를 설정할 수 있으며 (RefreshAccessToken(parameters)) GetAccessToken()을 호출하는 대신 사용자는 언제든지 연락처에 액세스 할 수 있습니다. 이해가 되니?

 // get this information from Google's API Console after registering your app 
     var parameters = new OAuth2Parameters 
     { 
      ClientId = @"", 
      ClientSecret = @"", 
      RedirectUri = @"", 
      Scope = @"https://www.google.com/m8/feeds/", 
      AccessCode = "", 
      AccessToken = "", /* use the value returned from the old call to GetAccessToken here */ 
      RefreshToken = "", /* use the value returned from the old call to GetAccessToken here */ 
     }; 

     // get an access token 
     OAuthUtil.RefreshAccessToken(parameters); 

     // setup connection to contacts service 
     var contacts = new ContactsRequest(new RequestSettings("<appname>", parameters)); 

     // get each contact 
     foreach (var contact in contacts.GetContacts().Entries) 
     { 
      System.Diagnostics.Debug.WriteLine(contact.ContactEntry.Name.FullName); 
     } 

편집 : 여기를 살펴

  // generate the authorization url 
      string url = OAuthUtil.CreateOAuth2AuthorizationUrl(parameters); 

      // now use the url to authorize the app in the browser and get the access code 
      (...) 

      // get this information from Google's API Console after registering your app 
      var parameters = new OAuth2Parameters 
      { 
       ClientId = @"", 
       ClientSecret = @"", 
       RedirectUri = @"", 
       Scope = @"https://www.google.com/m8/feeds/", 
       AccessCode = @"<from previous step>", 
      }; 

      // get an access token 
      OAuthUtil.GetAccessToken(parameters); 

      // setup connection to contacts service 
      var contacts = new ContactsRequest(new RequestSettings("<appname>", parameters)); 

      // get each contact 
      foreach (var contact in contacts.GetContacts().Entries) 
      { 
       System.Diagnostics.Debug.WriteLine(contact.ContactEntry.Name.FullName); 
      } 
+3

이 다른 모든 포스트는이 부분을 얼버무 것 같은 : 이제 브라우저에서 응용 프로그램을 승인하는 URL을 사용하여 얻을 // 액세스 코드 (...) 그게 무슨 뜻입니까? –

+0

OAuth2Parameters를 사용하지 않는이 샘플은 http://dotnetoday.blogspot.com.es/2012/10/c-google-api-getting-google-contacts.html입니다. – Kiquenet