3

I try use new google contacts API. My task is simple - retrieve contacts from static(my personal) domain account. I register my application at APIs Console and get ClientId,ClientSecret So I try authenticate my application through .net(google SDK)

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

This code works well, but Google said that we should use OAuth2 authentication at production scenario. I try different parameters at RequestSettings but at other variant I get 401(access denied). So my question what's the right way to auth through google API v3 at installed desctop application without using other accounts credentials.

John Conde
  • 217,595
  • 99
  • 455
  • 496
Andrew Kalashnikov
  • 3,073
  • 5
  • 34
  • 57
  • ...Google said that we should use OAuth2 that's write you should auth you request after this line RequestSettings settings = new RequestSettings(appName); // Add authorization token here ContactsRequest cr = new ContactsRequest(settings); but they soid nothing about how to do this i've tied to find solution anyway to use OAuth 2.0 you can read here https://developers.google.com/accounts/docs/OAuth2Login – Vladimir Shmidt Mar 13 '12 at 22:34

1 Answers1

0

Before start to work you should get auth token. For do this you should create link whitch user should open and grand access to your app. Than you should request for token with the code witch you get later. This mechanism described at https://developers.google.com/accounts/docs/OAuth2Login something like this:

        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 it's a code what you should catch after user redirect from "access granting page" But this method is api 2 i guess... Maybe I'm wrong, who know

Vladimir Shmidt
  • 2,651
  • 1
  • 19
  • 21
  • sorry AuthResponse is a public class AuthResponse { public string access_token { get; set; } public string token_type { get; set; } public string expires_in { get; set; } } – Vladimir Shmidt Mar 14 '12 at 00:05
  • and JsonConvert.DeserializeObject it's a part of Newtonsoft.Json dll you can get it from code plex – Vladimir Shmidt Mar 14 '12 at 00:06
  • what is `GetTokenUrl` and `_gApi.Token` ? – Alexei Apr 24 '12 at 12:39
  • I've made a refactoring so code has changed. In previous version GetTokenUrl = it's a end point of google auth api _gApi.Token = it's a token what you've get from google it's mean that your have key to make api request, in all, about tokens you can read something about oAuth rules... – Vladimir Shmidt May 05 '12 at 19:00