繁体   English   中英

如何使用 TwitterOAuth (abraham/twitteroauth) 在 PHP 中获取/生成 Twitter 的 HMAC-SHA1 签名

[英]How to get/generate HMAC-SHA1 signature for Twitter in PHP using TwitterOAuth (abraham/twitteroauth)

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

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

在此处输入图像描述

package 确实有效。

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

有什么想法我能做什么或如何生成这个签名?

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

生成签名的数据是:

  • 消费者密钥
  • 消费者秘密
  • 访问令牌
  • token_secret

在此处输入图像描述

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

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

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

因为我想做的就像一条推文,所以我做了如下。 使用亚伯拉罕/twitteroauth package。

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

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"}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM