admin管理员组

文章数量:1379654

I've worked a bit with Twitter web API, I know it works with OAuth, I've consumed the API using a python library. I've also tried a bit of Instagram API using a Java Script small library.

I know those perform web REST requests in the background, authenticating first and then querying requests as I code.

However, what if I want to perform the requets using jQuery $.ajax from a web application?

I've read some docs and sites and it seems it's just possible. Like only ajaxing to the API routes, starting with the authentication route.

But, how does this process work? I mean, I query by AJAX to the auth route and then how do I keep track of that authentication. How to keep that munication? Will the redirect URL play its role then?

Reading this site for Instagram API I start getting a clue about it, but got the doubts mentioned above.

I want to perform all AJAX requests in the Java Script server background (I'm using node.js), assuming I will provide my apps OAuth in the $.ajax. Is that OK or I can actually code it on client site keeping my OAuth tokens save?

And, if it's concern of this same question, when it es to bytes (pictures, sound, etc) how to catch the response from API.

I've worked a bit with Twitter web API, I know it works with OAuth, I've consumed the API using a python library. I've also tried a bit of Instagram API using a Java Script small library.

I know those perform web REST requests in the background, authenticating first and then querying requests as I code.

However, what if I want to perform the requets using jQuery $.ajax from a web application?

I've read some docs and sites and it seems it's just possible. Like only ajaxing to the API routes, starting with the authentication route.

But, how does this process work? I mean, I query by AJAX to the auth route and then how do I keep track of that authentication. How to keep that munication? Will the redirect URL play its role then?

Reading this site for Instagram API I start getting a clue about it, but got the doubts mentioned above.

I want to perform all AJAX requests in the Java Script server background (I'm using node.js), assuming I will provide my apps OAuth in the $.ajax. Is that OK or I can actually code it on client site keeping my OAuth tokens save?

And, if it's concern of this same question, when it es to bytes (pictures, sound, etc) how to catch the response from API.

Share Improve this question edited May 2, 2014 at 13:34 diegoaguilar asked Apr 7, 2014 at 6:40 diegoaguilardiegoaguilar 8,37616 gold badges86 silver badges133 bronze badges 9
  • "from my web site/server" --- ajax is performed from a client, not from a server. – zerkms Commented Apr 9, 2014 at 1:04
  • True, my bad. Apart, what solution would you advise me? – diegoaguilar Commented Apr 9, 2014 at 1:09
  • Honestly, I cannot get what exactly your're asking. – zerkms Commented Apr 9, 2014 at 1:10
  • 1 Twitter API doesn't support CORS, so you won't be able to perform requests. So your question is highly API vendor-specific – zerkms Commented Apr 9, 2014 at 1:15
  • 1 Your first SQL block from the question you just deleted works fine to me. Table is created successfully. – user2094178 Commented May 2, 2014 at 23:15
 |  Show 4 more ments

1 Answer 1

Reset to default 3

Okay, if I understand what you are trying to do is to make an Ajax call from a web page to the Twitter API and post/retrieve tweets and other info from Twitter.

Since the release of the API v1.1, Twitter has deprecated the v1.0 API and one of the major changes in 1.1 was Authentication Required on all Endpoints

And to do this from JavaScript and jQuery is quite possible (albeit very cumbersome, difficult and requires the use of many 3rd party JS libraries to HMAC Hash your data and keys and calculate content lengths on the client side before making your Request. Twitter API does not support CORS but does support JSONP for these kind of Ajax requests. But this is not remended - since doing this on the client side will require you to have your Twitter App Access Keys - Private keys - embedded in your script files - which is basically a big NO-NO. And hence a server side solutions to generate your oAuth tokens is remended. But once you have achieved that, it may be easier to get the token on your script and make Ajax calls using that from the browser. But I haven't gone that far in my research.

Also, this is based off of my research in Mid 2013 when my Twitter Ajax widgets stopped working because of this change and I gave up trying to fix it using that route after I realized it would promise my security keys. Things may have changed since then.

If you are still interested to find a solution, this walkthrough would be a good place to start learning about Twitter's oAuth and how the Access Tokens are generated: https://dev.twitter./docs/auth/oauth

本文标签: javascriptHow to do OAuth authentication via AJAXjQueryStack Overflow