Example #1
0
        void LoadToken()
        {

            string content = file.ReadAllTextAsync().Result;
            if(!string.IsNullOrEmpty(content))
                _token = JsonConvert.DeserializeObject<AccessToken>(content);
        }
Example #2
0
 public void AuthenticateWithCode(string code)
 {
     FormUrlEncodedContent c = new FormUrlEncodedContent(
        new[] { 
             new KeyValuePair<string, string>("client_id", CLIENT_ID),
             new KeyValuePair<string, string>("client_secret", CLIENT_SECRET),
             new KeyValuePair<string, string>("code", code),
             new KeyValuePair<string, string>("grant_type", "authorization_code"),
             new KeyValuePair<string, string>("redirect_uri", REDIRECT_URI),
             }
        );
     var response = _client.PostAsync("https://accounts.google.com/o/oauth2/token", c).Result;
     string content = response.Content.ReadAsStringAsync().Result;
     _token = JsonConvert.DeserializeObject<AccessToken>(content);
     SaveToken(content);
     Connect();
 }
Example #3
0
        void AuthWithRefreshToken()
        {
            if (_token.refresh_token == null)
            {
                _token = null;
                return;
            }
            FormUrlEncodedContent c = new FormUrlEncodedContent(
             new[] { 
                    new KeyValuePair<string, string>("client_id", CLIENT_ID),
                    new KeyValuePair<string, string>("client_secret", CLIENT_SECRET),                    
                    new KeyValuePair<string, string>("grant_type", "refresh_token"),
                    new KeyValuePair<string, string>("refresh_token",_token.refresh_token),
                    }
             );

            var response = _client.PostAsync("https://accounts.google.com/o/oauth2/token", c).Result;
            string content = response.Content.ReadAsStringAsync().Result;
            _token = JsonConvert.DeserializeObject<AccessToken>(content);
            SaveToken(content);
        }
Example #4
0
 public AuthenticationManager (string accessToken)
 {
     _token = new AccessToken() { access_token = accessToken };
 }
Example #5
0
        public void Connect(AccessToken token = null)
        {
            if (token != null)
                _token = token;

            // if token exists get session cookie;
            if (_token != null && _token.access_token != null)
            {
                if (_client.DefaultRequestHeaders.Contains("Authorization"))
                    _client.DefaultRequestHeaders.Remove("Authorization");

                _client.DefaultRequestHeaders.Add("Authorization", string.Format("{0} {1}", _token.token_type, _token.access_token));
                var response = _client.GetAsync("https://www.google.com/accounts/OAuthLogin?source=wtalk&issueuberauth=1").Result;
                if (response.IsSuccessStatusCode)
                {
                    string uberAuth = response.Content.ReadAsStringAsync().Result;
                    response = _client.GetAsync(string.Format("https://accounts.google.com/MergeSession?service=mail&continue=http://www.google.com&uberauth={0}", uberAuth)).Result;
                    if (response.IsSuccessStatusCode)
                        this.IsAuthenticated = true;

                }
                else
                {
                    AuthWithRefreshToken();
                    Connect();
                }

            }
        }