0

I need to access a twitter usertimeline with angular...

All keys are correct, it is working with php/curl, but i cannot manage to make it work within angular. I am using oauth-signature-js, then a $http request. I want to keep it as simple as possible.

The ng-controller :

app.controller('twitterController', function($scope, $http){ // VARIABLES var screen_name = 'grapax_be'; var count = 1; var consumer_key = 'xxxxxxxxxxxxxxxxxxxxxxxxx'; var consumer_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; var oauth_access_token = 'xxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; var oauth_access_token_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'; var oauth_timestamp = Math.round((new Date()).getTime() / 1000.0); var oauth_nonce = oauth_timestamp; var oauth_signature_method = 'HMAC-SHA1'; var oauth_version = '1.0' // GENERATE OAUTH SIGNATURE var httpMethod = 'GET'; var url = 'https://api.twitter.com/1.1/statuses/user_timeline.json'; var parameters = { oauth_consumer_key : consumer_key, oauth_token : oauth_access_token, oauth_nonce : oauth_nonce, oauth_timestamp : oauth_timestamp, oauth_signature_method : oauth_signature_method, oauth_version : oauth_version, }; var oauth_signature = oauthSignature.generate(httpMethod, url, parameters, consumer_secret, oauth_access_token_secret, { encodeSignature: true }); // HTTP REQUEST var authorization_string = "OAuth " + 'oauth_consumer_key="' + consumer_key + '", ' + 'oauth_nonce="' + oauth_nonce + '", ' + 'oauth_signature="' + oauth_signature + '", ' + 'oauth_signature_method="' + oauth_signature_method + '", ' + 'oauth_timestamp="' + oauth_timestamp + '", ' + 'oauth_token="' + oauth_access_token + '", ' + 'oauth_version="' + oauth_version + '"'; $http.jsonp('https://api.twitter.com/1.1/statuses/user_timeline.json', { headers: {'Authorization': authorization_string}, params:{ count: count, screen_name: screen_name } }).success(function(res){ console.log('res:', res) }); }); 

This is still giving me a error 400.

Here is plunker

Any tips, questions, remarks, are welcome.... Thank you!

Johan

3
  • 1
    Can you share a plunker? If not, can you put here the CURL (you can get this from chrome network console) Commented May 1, 2016 at 0:23
  • @pablorsk Here is a plunker : plnkr.co/edit/kF9iqCOv31ZXuuy4vlSp When i was speaking of the php/curl, i was just mentioning that the keys provided (consumer, secret, ...) where working in another environment (php). Cheerz! Commented May 1, 2016 at 1:30
  • I have answered my own question, and included the php/cURL method i use. It would be nice if you can mark it as useful if you find it so... Cheerz.. Commented May 13, 2016 at 1:24

2 Answers 2

1

Headers cannot be set in jsonp request. One solution can be to implement this using http client on server rather than the browser.

Sign up to request clarification or add additional context in comments.

3 Comments

OK i understand now i should avoid using the browser for security reasons. I am using Angular on top of a Wordpress/Apache server. Does this mean that i should keep using php/curl in this case for http request, so not use Angular for this task ?
It depends on security framework in place. In this scenario where jsonp is used for authentication, javascript in general will not be able solve the problem.
I have answered my own question, which is just an extension of yours. It would be nice if you can mark it as useful if you find it so :) Thank you.
1

Own further answer:

  1. Headers cannot be set in jsonp request.
  2. Using Apache/WP at server side, it was just better to stick with a php/cURL request, and calling it with a simple $http.get() for Angular integration.

By the way this PHP/cURL method is working great : http://iag.me/socialmedia/build-your-first-twitter-app-using-php-in-8-easy-steps/

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.