I've created an app with working log in to twitter via angularjs front end and Laravel back end and using the Satellizer library.
Now I want to interact with the Twitter Rest API, using the oAuth generated by satellizer, does anyone have a suggestion or library to do this? I've checked out thujohn/twitter Laravel plugin but I cant make it play nice with Satellizer and it doesnt cover all the api calls I want (search tweets, reply to tweets, get user timelines, get profiles)
All help appreciated!
Lewis
For anyone else in a similar situation: I found it easiest to just write my own functions rather than use a second lib.
Example route :
Route::post('tweet', ['middleware' => 'auth', 'uses' => 'TwitterController@tweet']);
Example controller function:
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;
}
And this is in my angularJS twitter factory:
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);
});
});
};