static void authorize() { auth = new PinAuthorizer() { Credentials = new InMemoryCredentials { ConsumerKey = "FiLff3SigcHPhHQxsSmpVQ", //ConfigurationManager.AppSettings["consumerKey"], ConsumerSecret = "xzmPCd8drcHurgaDG3SRdRooZCBzf1ZvgmekadUYZcQ" //ConfigurationManager.AppSettings["consumerSecret"] }, GoToTwitterAuthorization = pageLink => Process.Start(pageLink), GetPin = () => { Console.Write("Enter Twitter PIN: "); var input = Console.ReadLine().Trim(); return input; }, AuthAccessType = AuthAccessType.Write }; auth.Authorize(); if (!auth.IsAuthorized) { Console.WriteLine("Not authorized"); Environment.Exit(0); } }
public void Authorize() { var auth = new PinAuthorizer() { UserAgent = "AAafeen v" + Assembly.GetExecutingAssembly().GetName().Version.ToString(), Credentials = new InMemoryCredentials() { ConsumerKey = "BFtEaHnLWCMp9UjJEjBg", ConsumerSecret = "VYwKqXpJsLPwYhdSzmQD0ri3K2Fh4n4MQyFIxZ6ME" }, GoToTwitterAuthorization = uri => Process.Start(uri), GetPin = () => { var f = new InputPinCodeWindow(); f.ShowDialog(); return f.PinCode; } }; try { auth.Authorize(); } catch (Exception ex) { throw new Exception("認証に失敗しました。", ex); } Methods.twCtx.AuthorizedClient = auth; }
public ITwitterAuthorizer Auth() { var credentials = new InMemoryCredentials { ConsumerKey = this.consumerKey, ConsumerSecret = this.consumerSecret }; if (!string.IsNullOrEmpty(accessToken) && !string.IsNullOrEmpty(oAuthToken)) { credentials.AccessToken = this.accessToken; credentials.OAuthToken = this.oAuthToken; } var auth = new PinAuthorizer { Credentials = credentials, UseCompression = true, GoToTwitterAuthorization = pageLink => Process.Start(pageLink), GetPin = () => { Console.WriteLine("\nAfter you authorize this application, Twitter will give you a 7-digit PIN Number.\n"); Console.Write("Enter the PIN number here: "); return Console.ReadLine(); } }; auth.Authorize(); return auth; }
public void Authorize_Requires_GetPin_Handler() { const string AuthLink = "https://authorizationlink"; var pinAuth = new PinAuthorizer {Credentials = new InMemoryCredentials()}; var oAuthMock = new Mock<IOAuthTwitter>(); oAuthMock.Setup(oAuth => oAuth.AuthorizationLinkGet(It.IsAny<string>(), It.IsAny<string>(), "oob", false, AuthAccessType.NoChange)) .Returns(AuthLink); pinAuth.OAuthTwitter = oAuthMock.Object; string destinationUrl = string.Empty; pinAuth.GoToTwitterAuthorization = link => destinationUrl = link; var ex = Assert.Throws<InvalidOperationException>(() => pinAuth.Authorize()); Assert.True(ex.Message.Contains("GetPin")); }
public void Authorize_Launches_Browser() { const string AuthLink = "https://authorizationlink"; var pinAuth = new PinAuthorizer {Credentials = new InMemoryCredentials()}; var oAuthMock = new Mock<IOAuthTwitter>(); oAuthMock.Setup(oAuth => oAuth.AuthorizationLinkGet(It.IsAny<string>(), It.IsAny<string>(), "oob", false, AuthAccessType.NoChange)) .Returns(AuthLink); pinAuth.OAuthTwitter = oAuthMock.Object; pinAuth.GetPin = () => "1234567"; string destinationUrl = string.Empty; pinAuth.GoToTwitterAuthorization = link => destinationUrl = link; pinAuth.Authorize(); Assert.Equal(AuthLink, destinationUrl); }
public static IOAuthCredentials Authenticate() { var auth = new PinAuthorizer { Credentials = new InMemoryCredentials { ConsumerKey = "????????????????", ConsumerSecret = "????????????????" }, AuthAccessType = AuthAccessType.Write, UseCompression = true, GoToTwitterAuthorization = pageLink => Process.Start(pageLink), GetPin = () => { // this executes after user authorizes, which begins with the call to auth.Authorize() below. Console.WriteLine("\nAfter you authorize this application, Twitter will give you a 7-digit PIN Number.\n"); Console.Write("Enter the PIN number here: "); return Console.ReadLine(); } }; auth.Authorize(); return auth.Credentials; }
public void Authorize_Gets_Access_Token() { string screenName = "JoeMayo"; string userID = "123"; const string PinCode = "1234567"; const string AuthToken = "token"; const string AuthLink = "https://authorizationlink?oauth_token=" + AuthToken; var pinAuth = new PinAuthorizer {Credentials = new InMemoryCredentials()}; var oAuthMock = new Mock<IOAuthTwitter>(); oAuthMock.Setup(oAuth => oAuth.AuthorizationLinkGet(It.IsAny<string>(), It.IsAny<string>(), "oob", false, AuthAccessType.NoChange)) .Returns(AuthLink); oAuthMock.Setup(oAuth => oAuth.AccessTokenGet(AuthToken, PinCode, It.IsAny<string>(), string.Empty, out screenName, out userID)); pinAuth.OAuthTwitter = oAuthMock.Object; pinAuth.GetPin = () => PinCode; string destinationUrl = string.Empty; pinAuth.GoToTwitterAuthorization = link => destinationUrl = link; pinAuth.Authorize(); oAuthMock.Verify(oauth => oauth.AccessTokenGet(AuthToken, PinCode, It.IsAny<string>(), string.Empty, out screenName, out userID), Times.Once()); Assert.Equal(screenName, pinAuth.ScreenName); Assert.Equal(userID, pinAuth.UserId); Assert.Equal(AuthLink, destinationUrl); }
private static TwitterContext CreateContext(string accessToken, string oAuthToken) { var auth = new PinAuthorizer() { Credentials = new InMemoryCredentials { ConsumerKey = "rgRMroMnWQ8IoUNYcNIX0BsLV", ConsumerSecret = "vfX2T7V9kwmwDzCyBW8ZP91jElRhGnCNnG6fbfcvz1ysOrt8Bz", AccessToken = accessToken, OAuthToken = oAuthToken }, GoToTwitterAuthorization = pageLink => Process.Start(pageLink), GetPin = () => { Console.WriteLine( "\nAfter authorizing this application, Twitter " + "will give you a 7-digit PIN Number.\n"); Console.Write("Enter the PIN number here: "); var pin = Console.ReadLine(); return pin; } }; auth.AuthAccessType = AuthAccessType.Write; auth.Authorize(); return new TwitterContext(auth); }
public void Authorize_Returns_If_Already_Authorized() { var oAuthMock = new Mock<IOAuthTwitter>(); var pinAuth = new PinAuthorizer { Credentials = new InMemoryCredentials { ConsumerKey = "consumerkey", ConsumerSecret = "consumersecret", OAuthToken = "oauthtoken", AccessToken = "accesstoken" }, OAuthTwitter = oAuthMock.Object, GetPin = () => "1234567", GoToTwitterAuthorization = link => { } }; pinAuth.Authorize(); oAuthMock.Verify(oauth => oauth.AuthorizationLinkGet(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>(), false, AuthAccessType.NoChange), Times.Never()); }
public void Authorize_Requires_Credentials() { var pinAuth = new PinAuthorizer(); var ex = Assert.Throws<ArgumentNullException>(() => pinAuth.Authorize()); Assert.Equal("Credentials", ex.ParamName); }
// From CredentialToStringAndLoadDemo static ITwitterAuthorizer PerformAuthorization() { InMemoryCredentials credentials; // validate that credentials are present if (!GetCredentials(out credentials)) { return null; } // configure the OAuth object var auth = new PinAuthorizer { Credentials = credentials, UseCompression = true, GoToTwitterAuthorization = pageLink => Process.Start(pageLink), GetPin = () => { // this executes after user authorizes, which begins with the call to auth.Authorize() below. Console.WriteLine("\nAfter you authorize this application, Twitter will give you a 7-digit PIN Number.\n"); Console.Write("Enter the PIN number here: "); return Console.ReadLine(); } }; // start the authorization process (launches Twitter authorization page). try { auth.Authorize(); } catch (WebException wex) { MessageBox.Show( "Unable to authorize with Twitter right now. Please check pin number" + " and ensure your credential keys are correct. Exception received: " + wex.ToString()); return null; } File.WriteAllLines(CredentialsFile, new string[] { auth.Credentials.ToString() }); return auth; }
static ITwitterAuthorizer DoPinOAuth() { // validate that credentials are present if (ConfigurationManager.AppSettings["twitterConsumerKey"].IsNullOrWhiteSpace() || ConfigurationManager.AppSettings["twitterConsumerSecret"].IsNullOrWhiteSpace()) { Console.WriteLine("You need to set twitterConsumerKey and twitterConsumerSecret in App.config/appSettings. Visit http://dev.twitter.com/apps for more info.\n"); Console.Write("Press any key to exit..."); Console.ReadKey(); return null; } // configure the OAuth object var auth = new PinAuthorizer { Credentials = new InMemoryCredentials { ConsumerKey = ConfigurationManager.AppSettings["twitterConsumerKey"], ConsumerSecret = ConfigurationManager.AppSettings["twitterConsumerSecret"] }, AuthAccessType = AuthAccessType.NoChange, UseCompression = true, GoToTwitterAuthorization = pageLink => Process.Start(pageLink), GetPin = () => { // this executes after user authorizes, which begins with the call to auth.Authorize() below. Console.WriteLine("\nAfter authorizing this application, Twitter will give you a 7-digit PIN Number.\n"); Console.Write("Enter the PIN number here: "); return Console.ReadLine(); } }; // start the authorization process (launches Twitter authorization page). auth.Authorize(); return auth; }
ITwitterAuthorizer PerformAuthorization() { // validate that credentials are present if (string.IsNullOrWhiteSpace(twitterConsumerKey) || string.IsNullOrWhiteSpace(twitterConsumerSecret)) { MessageBox.Show(@"Error while setting " + "App.config/appSettings. \n\n" + "You need to provide your twitterConsumerKey and twitterConsumerSecret in App.config \n" + "Please visit http://dev.twitter.com/apps for more info.\n"); return null; } // configure the OAuth object var auth = new PinAuthorizer { Credentials = new InMemoryCredentials { ConsumerKey = twitterConsumerKey, ConsumerSecret = twitterConsumerSecret }, UseCompression = true, GoToTwitterAuthorization = pageLink => Process.Start(pageLink), GetPin = () => { // this executes after user authorizes, which begins with the call to auth.Authorize() below. PinWindow pinw = new PinWindow(); pinw.Owner = this; if (pinw.ShowDialog() == true) return pinw.Pin; else return ""; } }; // start the authorization process (launches Twitter authorization page). try { auth.Authorize(); } catch (WebException ex) { /*MessageBox.Show("Unable to authroize with Twitter right now. Please check pin number", "Twitter Archive Eraser", MessageBoxButton.OK, MessageBoxImage.Information); */ MessageBox.Show(ex.Message); return null; } return auth; }