Select to view content in your preferred language

Twitter API 1.1 OAuth  for geolocated tweets

811
1
02-26-2014 01:00 AM
NicoleDe_La_Feld
Occasional Contributor
Hi guys,
I am customizing a story map (MapTour Template ) adding a functionality to search geolocated Tweets from the public stream, but new Twitter API require authentication.
I created an account and registered my app to obtain keys and tokens but I always get error 403.

Here is my code for auth

function oAuth(kwArgs) {
   
 var CONSUMER_KEY = "xxxxxxxxxxx";
 var CONSUMER_SECRET = "xxxxxxxxxx";
 var token = "xxxxxxxxxx";
        var tokenSecret = "xxxxxxxxxx";
         
 var access = {
     consumer: {
                 key: CONSUMER_KEY,
                 secret: CONSUMER_SECRET
            }, 
             user: {
                 key: token,
                 secret: tokenSecret
            },

     sig_method: "HMAC-SHA1"
       
 };

 kwArgs.headers= { 
                      'Content-Type': 'application/x-www-form-urlencoded',
                      
                 };
        
 // our request will be made with the HTTP GET verb
  
 return OAuth.sign("GET", kwArgs, access);
   
}


and here is the code where I request tweets
function searchTwitter() {

 var query = dojo.byId("twitter_input").value;
 var geocode = "42.37355,12.88290,1000km";
   

 try {

  var results = esri.request(oAuth({
   dataType : 'jsonp',
   url : "http://api.twitter.com/1.1/search/tweets.json",
   content : {
    q : query,
    geocode : geocode,
    count : "100",
    include_entities : "false",
    result_type : "recent",
   },
   callbackParamName : "callback",
   
  }));
  results.then(addTweets);
    
    

 } catch (e) {
  console.log(e.toString());
 }
}; 


Somebody help, please!
0 Kudos
1 Reply
MattDriscoll
Esri Contributor
Did you verify that the oAuth headers are being sent in the request?

I wouldn't put the keys in JavaScript since they could easily be discovered. You should put them on a server side script like PHP so they are not exposed.
0 Kudos