private void btnTwitterAuthorize_Click(object sender, EventArgs e) { var pin = tbTwitterPIN.Text; if (!string.IsNullOrEmpty(pin)) { pin = pin.Trim(); } else { MessageBox.Show("Copy and paste the authentication PIN code from Twitter", "uTorrent Notifier - Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } try { _oAuth.AccessTokenGet(_oAuth.OAuthToken, pin); _classRegistry.Config.Twitter.Token = _oAuth.Token; _classRegistry.Config.Twitter.TokenSecret = _oAuth.TokenSecret; _classRegistry.Config.Twitter.Pin = pin; tbTwitterPIN.Enabled = false; btnTwitterAuthorize.Enabled = false; MessageBox.Show("Successfully authenticated with Twitter", "uTorrent Notifier - Success", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch { _classRegistry.Config.Twitter.Pin = string.Empty; tbTwitterPIN.Text = string.Empty; lblTwitterPIN.Enabled = false; tbTwitterPIN.Enabled = false; btnTwitterAuthorize.Enabled = false; MessageBox.Show("An error occurred trying to authenticate. Check your network settings and browser config, and try again.", "uTorrent Notifier - Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
/// <summary> /// Performs authentication asynchronously, managing entire OAuth workflow /// </summary> /// <returns>TwitterAuthProviderUser with ScreenName and ID</returns> internal async Task <TwitterAuthProviderUser> AuthenticateAsync() { string callbackStr = OAuthTwitter.FilterRequestParameters(callback); string link = OAuthTwitter.AuthorizationLinkGet(OAuthRequestTokenUrl, OAuthAuthorizeUrl, callbackStr, false, AuthAccessType); WebAuthenticationResult webAuthenticationResult = await WebAuthenticationBroker.AuthenticateAsync( WebAuthenticationOptions.None, new Uri(link), callback); if (webAuthenticationResult.ResponseStatus == WebAuthenticationStatus.Success) { var authCallbackUri = new Uri(webAuthenticationResult.ResponseData); string verifier = OAuthTwitter.GetUrlParamValue(authCallbackUri.Query, "oauth_verifier"); string oAuthToken = OAuthTwitter.GetUrlParamValue(authCallbackUri.Query, "oauth_token"); string screenName; string userID; OAuthTwitter.AccessTokenGet(oAuthToken, verifier, OAuthAccessTokenUrl, string.Empty, out screenName, out userID); ScreenName = screenName; UserId = userID; OAuthToken = OAuthTwitter.OAuthToken; OAuthTokenSecret = OAuthTwitter.OAuthTokenSecret; return(new TwitterAuthProviderUser { UserName = screenName, Id = userID }); } return(new TwitterAuthProviderUser { UserName = "", Id = "" }); }