400 Error calling AJAX on Twitter

I am working on a webpage that allows you to search for Twitter users in your area. You enter a name in the search field, and it returns users with that name 50 miles from the city (the city is always the same).

I had authentication issues and found oauth.io. I used this for authentication and it seems to work. But when I do a search, my Twitter request returns as a 400 error. I'm not sure what I'm doing wrong. I went through the oauth.io documentation but found nothing.

Here is the piece of code that gets the value of what the user entered into the form field:

// Click function
  $('.user-getter').submit(function(event) {
      // prevent form refresh
      event.preventDefault();
      // zero out results if previous search has run
      $('.user-results').html('');
      // Get the values of what the person entered in search
      var query = $(this).find("input[name='user_search']").val();
      // Run function to send API request to Twitter
      getUser(query);
  }); // end click function

Here is the piece of code that calls authorization and then sends an ajax request:

var getUser = function(query) {

  OAuth.initialize('oMQua1CuWerqGKRwqVkzDx5uijo')
  OAuth.popup('twitter').done(function(twitterData) {

    $.ajax({
      type: "GET",
      url: "https://api.twitter.com/1.1/users/search.json?&geocode=42.94003620000001,-78.8677924,50mi&q=" + query,
      dataType: "jsonp"
    });

    console.log( "Data Saved: " + twitterData ); // can see in inspector tab under Network

  }); // end oAuth popup
};

Right now, I just want to see the result in console.log.

+4
1

API Twitter, OAuth.io, SDK OAuth.io Javascript $.ajax(SDK OAuth.io Javascript $.ajax )

OAuth.initialize('oMQua1CuWerqGKRwqVkzDx5uijo')
OAuth.popup('twitter').done(function(twitterData) {
    twitterData.get('/1.1/users/search.json', {
         data: {
             q: query
         }
    }).done(function(search) {
         //result of the search here
         console.log(search);
    }).fail(function(error) {
         //error management here
    });
})

SDK : - HTTP, OAuth (oauth_token, oauth_token_secret, , nonce, timestamp ..)

jsfiddles: http://jsfiddle.net/uz76E/10/

, ,

+5

Source: https://habr.com/ru/post/1547511/


All Articles