使用 Twitter API 和 Laravel AngularJS Satellizer
Use Twitter API with Laravel AngularJS Satellizer
我创建了一个应用程序,可以通过 angularjs 前端和 Laravel 后端并使用 Satellizer 库登录到 Twitter。
现在我想与 Twitter Rest API 交互,使用 satellizer 生成的 oAuth,有没有人有建议或库来做到这一点?我已经检查了 thujohn/twitter Laravel 插件,但我不能让它与 Satellizer 一起玩得很好,它没有涵盖我想要的所有 api 调用(搜索推文、回复推文、获取用户时间线, 获取配置文件)
感谢所有帮助!
刘易斯
对于处于类似情况的其他人:我发现最简单的方法是编写自己的函数而不是使用第二个库。
示例路线:
Route::post('tweet', ['middleware' => 'auth', 'uses' => 'TwitterController@tweet']);
示例控制器功能:
public function searchTweets(Request $request)
{
//
$url = 'https://api.twitter.com/1.1/search/tweets.json';
$client = new GuzzleHttp\Client();
$user = User::find($request['user']['sub']);
$profileOauth = new Oauth1([
'consumer_key' => Config::get('app.twitter_key'),
'consumer_secret' => Config::get('app.twitter_secret'),
'token' => $user->oauthToken,
'token_secret' => $user->oauthVerifier
]);
$client->getEmitter()->attach($profileOauth);
$response = $client->get($url, ['auth' => 'oauth',
'query' => ['q' => $request->input('q'),
'result_type' => $request->input('result_type'),
#'since_id' => $request->input('since_id', 0),
#'max_id' => $request->input('max_id', 0),
'count' => $request->input('count')]
])->json();
return $response;
}
这是我的 angularJS 推特工厂:
var vm = {};
vm.tweets = [];
vm.busy = false;
vm.outreaches = 0;
vm.retweets = 0;
vm.favourites = 0;
vm.followed = 0;
vm.status = '';
vm.since_id = null;
vm.until = null;
vm.max_id = null;
vm.count = 100;
vm.q = '';
vm.result_type = 'mixed';
/*
* mixed: Include both popular and real time results in the response.
* recent: return only the most recent results in the response
* popular: return only the most popular results in the response.
*/
vm.searchTweets = function () {
if (vm.busy) {
return;
}
vm.busy = true;
var url = "/twitter/search";
$http({
url: url,
method: "GET",
params: {
q: vm.q,
result_type: vm.result_type || 'mixed',
count: vm.count || 100,
since_id: vm.since_id || null,
//until: vm.until || null,
max_id: vm.max_id || null
}
}).then(function (response) {
var newTweets = response.data.statuses;
console.log('newTweets: ');
console.log(newTweets);
for (var i = 0; i < newTweets.length; i++) {
vm.tweets.push(newTweets[i]);
}
vm.since_id = vm.tweets[vm.tweets.length - 1].id_str;
vm.busy = false;
}, function (response) {
// called asynchronously if an error occurs
// or server returns response with an error status.
console.log(response);
angular.forEach(response.data.errors, function(value, key){
$window.Materialize.toast(value.message, 3000);
});
});
};
我创建了一个应用程序,可以通过 angularjs 前端和 Laravel 后端并使用 Satellizer 库登录到 Twitter。
现在我想与 Twitter Rest API 交互,使用 satellizer 生成的 oAuth,有没有人有建议或库来做到这一点?我已经检查了 thujohn/twitter Laravel 插件,但我不能让它与 Satellizer 一起玩得很好,它没有涵盖我想要的所有 api 调用(搜索推文、回复推文、获取用户时间线, 获取配置文件)
感谢所有帮助!
刘易斯
对于处于类似情况的其他人:我发现最简单的方法是编写自己的函数而不是使用第二个库。
示例路线:
Route::post('tweet', ['middleware' => 'auth', 'uses' => 'TwitterController@tweet']);
示例控制器功能:
public function searchTweets(Request $request)
{
//
$url = 'https://api.twitter.com/1.1/search/tweets.json';
$client = new GuzzleHttp\Client();
$user = User::find($request['user']['sub']);
$profileOauth = new Oauth1([
'consumer_key' => Config::get('app.twitter_key'),
'consumer_secret' => Config::get('app.twitter_secret'),
'token' => $user->oauthToken,
'token_secret' => $user->oauthVerifier
]);
$client->getEmitter()->attach($profileOauth);
$response = $client->get($url, ['auth' => 'oauth',
'query' => ['q' => $request->input('q'),
'result_type' => $request->input('result_type'),
#'since_id' => $request->input('since_id', 0),
#'max_id' => $request->input('max_id', 0),
'count' => $request->input('count')]
])->json();
return $response;
}
这是我的 angularJS 推特工厂:
var vm = {};
vm.tweets = [];
vm.busy = false;
vm.outreaches = 0;
vm.retweets = 0;
vm.favourites = 0;
vm.followed = 0;
vm.status = '';
vm.since_id = null;
vm.until = null;
vm.max_id = null;
vm.count = 100;
vm.q = '';
vm.result_type = 'mixed';
/*
* mixed: Include both popular and real time results in the response.
* recent: return only the most recent results in the response
* popular: return only the most popular results in the response.
*/
vm.searchTweets = function () {
if (vm.busy) {
return;
}
vm.busy = true;
var url = "/twitter/search";
$http({
url: url,
method: "GET",
params: {
q: vm.q,
result_type: vm.result_type || 'mixed',
count: vm.count || 100,
since_id: vm.since_id || null,
//until: vm.until || null,
max_id: vm.max_id || null
}
}).then(function (response) {
var newTweets = response.data.statuses;
console.log('newTweets: ');
console.log(newTweets);
for (var i = 0; i < newTweets.length; i++) {
vm.tweets.push(newTweets[i]);
}
vm.since_id = vm.tweets[vm.tweets.length - 1].id_str;
vm.busy = false;
}, function (response) {
// called asynchronously if an error occurs
// or server returns response with an error status.
console.log(response);
angular.forEach(response.data.errors, function(value, key){
$window.Materialize.toast(value.message, 3000);
});
});
};