【问题标题】:How to get/generate HMAC-SHA1 signature for Twitter in PHP using TwitterOAuth (abraham/twitteroauth)如何使用 TwitterOAuth (abraham/twitteroauth) 在 PHP 中为 Twitter 获取/生成 HMAC-SHA1 签名
【发布时间】:2022-01-03 20:33:49
【问题描述】:

我正在尝试按照 Twitter 文档的建议使用 HMAC-SHA1 生成我的签名。

但我正在使用 abraham/twitteroauth 包来让这个“更容易”,我留下了它返回给我的屏幕截图。

而且这个包真的很管用。

注意:我想要做的是 RT 和 FAV 到其他用户的其他推文。除此之外,创建回复。

任何想法我可以做什么或如何生成此签名?

我用 POSTMAN(Twitter 提供的那个)试过它,它在那里工作,但它在我的代码中不起作用。

生成签名的数据有:

  • consumer_key
  • consumer_secret
  • access_token
  • token_secret

我也指向端点:https://api.twitter.com/2/users/:id/retweets

【问题讨论】:

  • 您能描述错误或问题吗?你是说使用 TwitterOAuth 包是可行的,所以你可以使用它吗?
  • @AndyPiper 我想知道如何在不使用包的情况下生成签名。

标签: php laravel twitter hmacsha1


【解决方案1】:

虽然我想知道如何在不使用包的情况下生成签名,但我想与您分享一些对我有用的东西(使用包)。

我想使用 Guzzle 来测试 Twitter API 如何通过 Laravel 工作。

因为我想做的就像一条推文,所以我做了如下。使用 abraham/twitteroauth 包。

所以我把我的代码分开了一点。

private function getApplicationInfo(){
      //Aplication information
      $api_key = 'twitter_api_key';
      $api_key_secret = 'twitter_api_key_secret';
    
      $credentials = [
         'api_key' => $api_key,
         'api_key_secret' => $api_key_secret,
      ];
    
      return $credentials;
   }

//然后我从另一个方法调用了这个数据

public function likeTweet(){
      $application_info = $this->getApplicationInfo();
      $user = User::whereId(2)->first(); 
      $user_info = json_decode($user->user_info, true);

      
      $twitter_info = new TwitterOAuth(
         $application_info['api_key'], 
         $application_info['api_key_secret'], 
         $user_info['oauth_token'], $user_info['oauth_token_secret']
      );

      $twitter_info->setApiVersion('2');
    
      $data = [
         'tweet_id' => '1463484369100853255' //Tweet to which you are going to RT or like
      ];

      //The ID of the user who is going to like or RT
      $statues = $twitter_info->post("users/twitter_user_id/retweets", $data, true); // Note the true
   }

注意:我的用户 2 有一个 JSON 字段,其中包含这样的信息

{"oauth_token": "user_id_twitter-token", "oauth_token_secret": "twitter_user_token_secret"}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-18
    • 1970-01-01
    • 2015-09-25
    • 2015-07-11
    • 1970-01-01
    • 1970-01-01
    • 2015-06-17
    • 2015-07-20
    相关资源
    最近更新 更多