Hi all,
I’m implementing a login system using OAuth plugin. I have installed the last version from the nraboy repo. The issue occurs when i try to get the user data from the Twitter API. Here is my function:
getTwitterProfile: function() {
var deferred = $q.defer();
var token = angular.fromJson(getStoredToken());
createTwitterSignature('GET',
'https://api.twitter.com/1.1/users/show.json');
$http.get("https://api.twitter.com/1.1/users/show.json", {
params: {
screen_name: token.screen_name
}
}).success(function(result) {
alert('USER FROM TWITTER: '+result);
deferred.resolve(result);
}).error(function(error) {
alert("Error: "+JSON.stringify(error));
deferred.reject(false);
});
return deferred.promise;
}
My token it’s OK, because if i change the url by the following on the get request for , it works:
// Get user Timeline
getTwitterProfile: function() {
var deferred = $q.defer();
var token = angular.fromJson(getStoredToken());
createTwitterSignature('GET',
'https://api.twitter.com/1.1/statuses/home_timeline.json');
$http.get("https://api.twitter.com/1.1/statuses/home_timeline.json")
.success(function(result) {
alert('USER TIMELINE: '+result);
deferred.resolve(result);
}).error( function(error) {
alert("Error: "+JSON.stringify(error));
deferred.reject(false);
});
return deferred.promise;
}
The only difference is the URL and the params that i passed to request the user by name, following the Twitter API Docs: https://dev.twitter.com/rest/reference/get/users/show
The error msg is: Could not authenticate you.
But if i change the url, the Twitter API authenticates me so is not my token.
My createTwitterSignature function:
function createTwitterSignature(method, url) {
var token = angular.fromJson(getStoredToken());
var oauthObject = {
oauth_consumer_key: clientId,
oauth_nonce: $cordovaOauthUtility.createNonce(32),
oauth_signature_method: "HMAC-SHA1",
oauth_token: token.oauth_token,
oauth_timestamp: Math.round((new Date()).getTime() / 1000.0),
oauth_version: "1.0"
};
var signatureObj = $cordovaOauthUtility.createSignature(
method, url, oauthObject, {}, clientSecret, token.oauth_token_secret);
$http.defaults.headers.common.Authorization =
signatureObj.authorization_header;
}
I do the same with Facebook and it works like a charm:
// Get facebook profile
getFacebookProfile: function() {
var deferred = $q.defer();
var token = JSON.parse(getStoredToken());
$http.get("https://graph.facebook.com/v2.2/me", {
params: {
access_token: token,
fields: "id,name,gender,location,website,picture,relationship_status",
format: "json"
}
})
.success(function(result) {
deferred.resolve(result);
})
.error( function(error) {
alert("Error: "+error);
deferred.reject(false);
});
return deferred.promise;
}
Any ideas? maybe is my http request that is wrong?
I appreciate your help!
Thanks!