2011-12-21 11 views
3

新しいGoogleコンタクトAPIを使用してみます。 私の仕事は簡単です - 私の個人的なドメインアカウントから連絡先を取得してください。 I APIコンソールで自分のアプリケーションを登録し、だから私は(SDKグーグル)ネットを通じて自分のアプリケーションを認証してみてくださいClientIdを、ClientSecret を得るGoogle contact api v3 authentication

RequestSettings settings = new RequestSettings(appName,login,password); 
ContactsRequest cr = new ContactsRequest(settings); 
Feed<Contact> contacts = cr.GetContacts(); 
foreach (Contact entry in contacts.Entries) 
{ 
     .... 
} 

このコードはうまく動作しますが、Googleは、我々は製造時のOAuth2認証を使用するべきであると述べましたシナリオ。 私はRequestSettingsで異なるパラメータを試しますが、他の変種では401(アクセス拒否)を取得します。 他のアカウントの資格情報を使用せずにインストールされたdesctopアプリケーションでgoogle API v3を使って認証する正しい方法は何ですか?

+0

... GoogleはOAuth2 を使用する必要があると言っています。この行の後にあなたが要求する認証を書いてください。 RequestSettings settings = new RequestSettings(appName); //ここに認証トークンを追加してください ContactsRequest cr = new ContactsRequest(settings); しかし、彼らはこれを行う方法については何もしていません 解決策を見つけるために私は結びついています。とにかくOAuth 2.0を使用するにはhttps://developers.google.com/accounts/docs/OAuth2Login –

答えて

0

作業を開始する前に、認証トークンを取得する必要があります。これを行うには、あなたが開いて、あなたのアプリへの壮大なアクセスが必要なリンクの邪魔を作成する必要があります。 あなたが後で取得するコード魔法使いとトークンを要求する必要があります。

 private const string GetTokenUrl = "https://accounts.google.com/o/oauth2/token";  
     private new bool Auth(bool needUserCredentionals = true) 
     { 
      var dic = new Dictionary<string, string>(); 
      dic.Add("grant_type", "authorization_code"); 
      dic.Add("code", ResponseCode); 
      dic.Add("client_id", ApplicationId); 
      dic.Add("client_secret", ApplicationSecret); 
      dic.Add("redirect_uri", HttpUtility.UrlEncode(AppRedirectUrl)); 
      var str = String.Join("&", dic.Select(item => item.Key + "=" + item.Value).ToArray()); 
      var client = new WebClient(); 
      client.Headers.Add("Content-type", "application/x-www-form-urlencoded"); 
      string s; 
      try { s = client.UploadString(GetTokenUrl, str); } 
      catch (WebException) { return false; } 
      catch (Exception) { return false; } 
      AuthResponse response; 
      try { response = JsonConvert.DeserializeObject<AuthResponse>(s); } 
      catch (Exception) { return false; } 
      Token = response.access_token; 
      SessionTime = DateTime.Now.Ticks + response.expires_in; 
      if (needUserCredentionals) 
       if (!GetUserInfo()) return false; 
      return true; 
     } 

     public class AuthResponse 
     { 
      public string access_token { get; set; } 
      public string token_type { get; set; } 
      public long expires_in { get; set; } 
     } 

にResponseCodeが、それはあなたが 「アクセス許可ページ」からリダイレクトするユーザーの後にキャッチする必要がありますどのようなコードです。しかし、この方法は、たぶん私は...私は推測するAPI 2: このメカニズムは次のようにhttps://developers.google.com/accounts/docs/OAuth2Login 何かを説明します私が間違っている、間違っている、知っている

+0

ここをクリックしてください申し訳ありませんAuthResponseはパブリッククラスですAuthResponse { 公開ストリングaccess_token {get;セット; } 公開ストリングtoken_type {get;セット; } 公開ストリングexpires_in {get;セット; } } –

+0

とJsonConvert.DeserializeObjectこれはNewtonsoft.Json dllの一部です。コードプレックス –

+0

から取得できます。「GetTokenUrl'と '_gApi.Token'は何ですか? – Alexei