예제 #1
0
        private async Task <WSATwitterLoginResult> GetAccessToken(WSATwitterLoginResult result, string oauthVerifier)
        {
            try
            {
                IDictionary <string, string> additionalParts = new Dictionary <string, string>()
                {
                    { "oauth_token", _oauthToken }
                };

                string authHeader = _headerGenerator.Generate("POST", "https://api.twitter.com//oauth/access_token", additionalParts, null, null);

                IDictionary <string, string> body = new Dictionary <string, string>()
                {
                    { "oauth_verifier", oauthVerifier }
                };

                HttpResponseMessage response = await MakeRequest("https://api.twitter.com//oauth/access_token", authHeader, new FormUrlEncodedContent(body), HttpMethod.Post);

                if (!response.IsSuccessStatusCode)
                {
                    result.Success      = false;
                    result.ErrorMessage = await response.Content.ReadAsStringAsync();

                    return(result);
                }

                string content = await response.Content.ReadAsStringAsync();

                IDictionary <string, string> parsed = ParseResponse(content);

                if (!parsed.ContainsKey("oauth_token") || !parsed.ContainsKey("oauth_token_secret"))
                {
                    result.Success      = false;
                    result.ErrorMessage = "GetAccessToken response does not contain the expected properties";
                    return(result);
                }

                if (parsed.ContainsKey("user_id"))
                {
                    result.UserId = parsed["user_id"];
                }

                if (parsed.ContainsKey("screen_name"))
                {
                    result.ScreenName = parsed["screen_name"];
                }

                _oauthToken       = parsed["oauth_token"];
                _oauthTokenSecret = parsed["oauth_token_secret"];

                return(result);
            }
            catch (Exception e)
            {
                result.Success      = false;
                result.ErrorMessage = e.Message;
                return(result);
            }
        }
예제 #2
0
        public async Task <WSATwitterLoginResult> Login()
        {
            Logout();

            WSATwitterLoginResult result = new WSATwitterLoginResult()
            {
                Success = true
            };

            result = await GetRequestToken(result);

            if (!result.Success)
            {
                return(result);
            }

            var userLoginResult = await UserLogin(result);

            if (!userLoginResult.Item1.Success)
            {
                return(userLoginResult.Item1);
            }

            result = await GetAccessToken(userLoginResult.Item1, userLoginResult.Item2);

            if (!result.Success)
            {
                return(result);
            }

            IsLoggedIn = true;

            try
            {
                StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync(_savedDataFilename, CreationCollisionOption.ReplaceExisting);

                await FileIO.WriteTextAsync(file, string.Format("{0}&{1}", _oauthToken, _oauthTokenSecret));
            }
            catch
            {
            }

            return(result);
        }
예제 #3
0
        private async Task <WSATwitterLoginResult> GetRequestToken(WSATwitterLoginResult result)
        {
            try
            {
                IDictionary <string, string> additionalParts = new Dictionary <string, string>()
                {
                    { "oauth_callback", _oauthCallback }
                };

                string authHeader = _headerGenerator.Generate("POST", "https://api.twitter.com/oauth/request_token", additionalParts, null, null);

                HttpResponseMessage response = await MakeRequest("https://api.twitter.com/oauth/request_token", authHeader, null, HttpMethod.Post);

                if (!response.IsSuccessStatusCode)
                {
                    result.Success      = false;
                    result.ErrorMessage = await response.Content.ReadAsStringAsync();

                    return(result);
                }

                string content = await response.Content.ReadAsStringAsync();

                IDictionary <string, string> parsed = ParseResponse(content);

                if (!parsed.ContainsKey("oauth_callback_confirmed") || parsed["oauth_callback_confirmed"] == "false" || !parsed.ContainsKey("oauth_token") || !parsed.ContainsKey("oauth_token_secret"))
                {
                    result.Success      = false;
                    result.ErrorMessage = "GetRequestToken response does not contain the expected properties";
                    return(result);
                }

                _oauthToken       = parsed["oauth_token"];
                _oauthTokenSecret = parsed["oauth_token_secret"];

                return(result);
            }
            catch (Exception e)
            {
                result.Success      = false;
                result.ErrorMessage = e.Message;
                return(result);
            }
        }
예제 #4
0
        private async Task <Tuple <WSATwitterLoginResult, string> > UserLogin(WSATwitterLoginResult result)
        {
            try
            {
                Uri requestUri = new Uri(string.Format("https://api.twitter.com/oauth/authenticate?oauth_token={0}", _oauthToken));

                Uri callbackUri = new Uri(_oauthCallback);

                WebAuthenticationResult authentication = await WebAuthenticationBroker.AuthenticateAsync(WebAuthenticationOptions.None, requestUri, callbackUri);

                if (authentication.ResponseStatus != WebAuthenticationStatus.Success)
                {
                    result.Success      = false;
                    result.ErrorMessage = authentication.ResponseStatus.ToString();
                    return(new Tuple <WSATwitterLoginResult, string>(result, null));
                }

                IDictionary <string, string> parsed = ParseResponse(authentication.ResponseData);

                if ((parsed.ContainsKey("oauth_token") && parsed["oauth_token"] != _oauthToken) || !parsed.ContainsKey("oauth_verifier"))
                {
                    result.Success      = false;
                    result.ErrorMessage = "UserLogin response does not contain the expected properties";
                    return(new Tuple <WSATwitterLoginResult, string>(result, null));
                }

                string oauthVerifier = parsed["oauth_verifier"];

                return(new Tuple <WSATwitterLoginResult, string>(result, oauthVerifier));
            }
            catch (Exception e)
            {
                result.Success      = false;
                result.ErrorMessage = e.Message;
                return(new Tuple <WSATwitterLoginResult, string>(result, null));
            }
        }