虽然我想知道如何在不使用包的情况下生成签名,但我想与您分享一些对我有用的东西(使用包)。
我想使用 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"}