简体   繁体   English

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

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

I am trying to generate my signature with HMAC-SHA1 as suggested by the Twitter documentation.我正在尝试按照 Twitter 文档的建议使用HMAC-SHA1生成我的签名。

But I am using the abraham/twitteroauth package to make this "easier" and I leave a screenshot of what it is returning to me.但是我正在使用abraham/twitteroauth package 来使这个“更容易”,并且我留下了它返回给我的屏幕截图。

在此处输入图像描述

And the package really works. package 确实有效。

Note: What I want to do is RT and FAV to other Tweets from other users.注意:我想要做的是对其他用户的其他推文进行 RT 和 FAV。 In addition to that, create replies.除此之外,创建回复。

Any ideas what can I do or how can I generate this signature?有什么想法我能做什么或如何生成这个签名?

I tried it with POSTMAN (the one provided by Twitter) and it works there, but it doesn't work in my code.我用 POSTMAN(Twitter 提供的那个)试过它,它在那里工作,但它在我的代码中不起作用。

The data to generate the signature are:生成签名的数据是:

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

在此处输入图像描述

I am also pointing to the endpoint: https://api.twitter.com/2/users/:id/retweets我也指向端点: https://api.twitter.com/2/users/:id/retweets

Although I would like to know how to generate the signature without using packages , I want to share with you something that worked for me (using the package).虽然我想知道如何在不使用包的情况下生成签名,但我想与您分享一些对我有用的东西(使用包)。

I wanted to use Guzzle to test how the Twitter API works through Laravel.我想用Guzzle来测试 Twitter API 如何通过 Laravel 工作。

And since what I wanted to do was like a Tweet so I did it as follows.因为我想做的就像一条推文,所以我做了如下。 Using abraham/twitteroauth package.使用亚伯拉罕/twitteroauth package。

So I separated my code a bit.所以我把我的代码分开了一点。

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

//Then I called this data from another method //然后我从另一个方法调用这个数据

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
   }

Note: My user 2 has a JSON field with the information like this注意:我的用户 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