示例#1
0
        static PinAuthorizer Authorize(string path)
        {
            var auth = new PinAuthorizer();

            auth.CredentialStore = new InMemoryCredentialStore
            {
                ConsumerKey    = ConfigurationManager.AppSettings["consumerKey"],
                ConsumerSecret = ConfigurationManager.AppSettings["consumerSecret"]
            };

            auth.GoToTwitterAuthorization = pageLink => Process.Start(pageLink);

            auth.GetPin = () =>
            {
                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());
            };

            auth.AuthorizeAsync().Wait();

            var lines = new List <string> {
                auth.CredentialStore.OAuthToken, auth.CredentialStore.OAuthTokenSecret
            };

            File.WriteAllLines(path, lines);

            return(auth);
        }
示例#2
0
        public async Task <bool> Connect()
        {
            if (this.token != null)
            {
                try
                {
                    SingleUserAuthorizer singleUserAuth = new SingleUserAuthorizer
                    {
                        CredentialStore = new SingleUserInMemoryCredentialStore
                        {
                            ConsumerKey    = TwitterService.ClientID,
                            ConsumerSecret = ChannelSession.SecretManager.GetSecret("TwitterSecret"),

                            AccessToken       = this.token.accessToken,
                            AccessTokenSecret = this.token.refreshToken,

                            UserID     = ulong.Parse(this.token.clientID),
                            ScreenName = this.token.authorizationCode,
                        }
                    };
                    await singleUserAuth.AuthorizeAsync();

                    if (await this.InitializeInternal(singleUserAuth))
                    {
                        return(true);
                    }
                }
                catch (Exception ex) { Logger.Log(ex); }
            }

            try
            {
                PinAuthorizer pinAuth = new PinAuthorizer()
                {
                    CredentialStore = new InMemoryCredentialStore
                    {
                        ConsumerKey    = TwitterService.ClientID,
                        ConsumerSecret = ChannelSession.SecretManager.GetSecret("TwitterSecret"),
                    },
                    GoToTwitterAuthorization = pageLink => Process.Start(pageLink),
                    GetPin = () =>
                    {
                        while (string.IsNullOrEmpty(this.authPin))
                        {
                            Task.Delay(1000).Wait();
                        }
                        return(this.authPin);
                    }
                };

                await pinAuth.AuthorizeAsync();

                this.authPin = null;

                return(await this.InitializeInternal(pinAuth));
            }
            catch (Exception ex) { Logger.Log(ex); }

            return(false);
        }
示例#3
0
        static async Task <TwitterContext> GetTwitterContext()
        {
            var auth = new PinAuthorizer()
            {
                CredentialStore = new InMemoryCredentialStore
                {
                    ConsumerKey    = "",
                    ConsumerSecret = ""
                },
                GoToTwitterAuthorization = pageLink =>
                {
                    var psi = new ProcessStartInfo
                    {
                        FileName        = pageLink,
                        UseShellExecute = true
                    };
                    Process.Start(psi);
                },
                GetPin = () =>
                {
                    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() ?? string.Empty);
                }
            };

            await auth.AuthorizeAsync();

            var twitterCtx = new TwitterContext(auth);

            return(twitterCtx);
        }
示例#4
0
文件: Bot.cs 项目: CFTheMaster/kotori
        private IAuthorizer Authorise()
        {
            IAuthorizer auth = null;

            string consumerKey    = Config.Get <string>("Bot", "ConsumerKey");
            string consumerSecret = Config.Get <string>("Bot", "ConsumerSecret");

            if (Config.Contains(ConfigSection, "OAToken") &&
                Config.Contains(ConfigSection, "OASecret"))
            {
                auth = new SingleUserAuthorizer
                {
                    CredentialStore = new SingleUserInMemoryCredentialStore
                    {
                        ConsumerKey      = consumerKey,
                        ConsumerSecret   = consumerSecret,
                        OAuthToken       = Config.Get <string>(ConfigSection, "OAToken"),
                        OAuthTokenSecret = Config.Get <string>(ConfigSection, "OASecret"),
                    }
                }
            }
            ;
            else
            {
                auth = new PinAuthorizer
                {
                    CredentialStore = new SingleUserInMemoryCredentialStore
                    {
                        ConsumerKey    = consumerKey,
                        ConsumerSecret = consumerSecret,
                    },
                    GoToTwitterAuthorization = x => Log.Add($"Go to '{x}' in a web browser.", LogLevel.Important),
                    GetPin = () =>
                    {
                        Log.Add("After authorising you will receive a 7-digit pin number, enter this here:", LogLevel.Info);
                        return(Console.ReadLine());
                    }
                };

                auth.AuthorizeAsync().GetAwaiter().GetResult();
                Config.Set(ConfigSection, "OAToken", auth.CredentialStore.OAuthToken);
                Config.Set(ConfigSection, "OASecret", auth.CredentialStore.OAuthTokenSecret);
                Config.Save();
            }

            return(auth);
        }
示例#5
0
        public async Task <AuthorizeResult> Authorize(Action <string> displayPinPageAction, Func <string> getPinAction,
                                                      CancellationToken?token = null)
        {
            var auth = new PinAuthorizer
            {
                CredentialStore = new InMemoryCredentialStore
                {
                    ConsumerKey    = Constants.Auth.ConsumerKey,
                    ConsumerSecret = Constants.Auth.ConsumerSecret
                },
                GoToTwitterAuthorization = displayPinPageAction,
                GetPin = getPinAction
            };

            try
            {
                await auth.AuthorizeAsync().ConfigureAwait(false);
            }
            catch (TwitterQueryException ex)
            {
                if (token?.IsCancellationRequested != true)
                {
                    LogTo.ErrorException("Failed to authorize user", ex);
                }

                return(null);
            }

            var accountData = new TwitterAccountData
            {
                OAuthToken       = auth.CredentialStore.OAuthToken,
                OAuthTokenSecret = auth.CredentialStore.OAuthTokenSecret,
                AccountName      = auth.CredentialStore.ScreenName,
                UserId           = auth.CredentialStore.UserID
            };

            return(new AuthorizeResult(accountData, auth));
        }
示例#6
0
        public override async Task <Result> Connect()
        {
            try
            {
                PinAuthorizer pinAuthorizer = new PinAuthorizer()
                {
                    CredentialStore = new InMemoryCredentialStore
                    {
                        ConsumerKey    = TwitterService.ClientID,
                        ConsumerSecret = ChannelSession.Services.Secrets.GetSecret("TwitterSecret"),
                    },
                    GoToTwitterAuthorization = pageLink => ProcessHelper.LaunchLink(pageLink),
                    GetPin = () =>
                    {
                        while (string.IsNullOrEmpty(this.authPin))
                        {
                            Task.Delay(1000).Wait();
                        }
                        return(this.authPin);
                    }
                };

                await pinAuthorizer.AuthorizeAsync();

                this.authorizer = pinAuthorizer;

                this.authPin = null;

                return(await this.InitializeInternal());
            }
            catch (Exception ex)
            {
                Logger.Log(ex);
                return(new Result(ex));
            }
        }