【问题标题】:Authenticate twitter bot with static secrets?使用静态机密对 Twitter 机器人进行身份验证?
【发布时间】:2021-01-19 02:57:49
【问题描述】:

我的计划:

我正在编写一个机器人,它会不时发送一些推文,然后使用库 tweetinvi (https://github.com/linvi/tweetinvi)。

机器人当前正在使用我在开发者控制台中生成的访问令牌和密码。

问题:

机器人现在将使用我的个人帐户发送推文。这就是为什么我创建了一个新的辅助帐户,将来应该用于推文。

但是我现在如何验证机器人呢?

  1. 新帐户不是开发者帐户,我在帐户设置中看不到任何生成访问令牌和密码的选项。

  2. 此外,我不认为通常的 oauth“单击 twitter 图标并登录到 twitter”登录是我的机器人的正确选择,因为我没有网站或浏览器会话,只是一个程序跑到哪里去。

我不能只为我的辅助帐户创建某种静态的访问令牌和机密吗?

如果不可能,你会建议我做什么?

【问题讨论】:

    标签: c# twitter tweetinvi


    【解决方案1】:

    是的,您可以为您的辅助帐户创建访问令牌和密码。执行此操作的标准方法是在应用程序中使用 Twitter 实现登录,这需要 Web 后端。

    有几个备选方案:

    1. 您可以使用twurl,这是 Twitter 提供的命令行工具。您需要安装 Ruby,然后安装 twurl。使用twurl 你可以运行

    twurl authorize --consumer-key [your API token] --consumer-secret [your API secret]

    这将提供一个您应该在浏览器中打开的 URL。将此验证到您的辅助帐户并将 PIN 输入您的终端。帐户令牌和机密将放置在您的主目录中名为 .twurlrc 的隐藏文件中,您可以像当前使用主帐户令牌一样将它们用于您的机器人。

    1. 您可以使用another utility called tw-oob-oauth-cli 来做同样的事情。这有点容易,因为您不需要安装 Ruby,只需下载 Windows 二进制文件即可。您需要使用 .exe 扩展名重命名二进制文件。

    tw-oob-oauth.exe --key [your API token] --secret [your API secret]

    这将在您通过网络流程后在终端上输出帐户令牌和密码。

    【讨论】:

      【解决方案2】:

      要通过 Tweetinvi 进行身份验证,您可以按照此处的指南进行操作:https://linvi.github.io/tweetinvi/dist/authentication/authentication.html

      var appClient = new TwitterClient("CONSUMER_KEY", "CONSUMER_SECRET");
      
      // Start the authentication process
      var authenticationRequest = await appClient.Auth.RequestAuthenticationUrlAsync();
      
      // Go to the URL so that Twitter authenticates the user and gives him a PIN code.
      Process.Start(new ProcessStartInfo(authenticationRequest.AuthorizationURL)
      {
          UseShellExecute = true
      });
      
      // Ask the user to enter the pin code given by Twitter
      Console.WriteLine("Please enter the code and press enter.");
      var pinCode = Console.ReadLine();
      
      // With this pin code it is now possible to get the credentials back from Twitter
      var userCredentials = await appClient.Auth.RequestCredentialsFromVerifierCodeAsync(pinCode, authenticationRequest);
      
      // You can now save those credentials or use them as followed
      var userClient = new TwitterClient(userCredentials);
      var user = await userClient.Users.GetAuthenticatedUserAsync();
      
      Console.WriteLine("Congratulation you have authenticated the user: " + user);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-09
        • 2019-11-08
        • 1970-01-01
        • 2020-10-21
        • 2017-01-29
        相关资源
        最近更新 更多