简体   繁体   中英

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.

But I am using the abraham/twitteroauth package to make this "easier" and I leave a screenshot of what it is returning to me.

在此处输入图像描述

And the package really works.

Note: What I want to do is RT and FAV to other Tweets from other users. 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.

The data to generate the signature are:

  • consumer_key
  • consumer_secret
  • access_token
  • token_secret

在此处输入图像描述

I am also pointing to the endpoint: 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.

And since what I wanted to do was like a Tweet so I did it as follows. Using abraham/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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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