2
votes

I am working on twitter login using Jquery and Plain Java Script. I have completed most of work, I am trying to get request_token from https://api.twitter.com/oauth/request_token URL. Request goes from my page as well but everytime it send me error of:

jquery.js:9536 OPTIONS https://api.twitter.com/oauth/request_token 400 ()send @ jquery.js:9536ajax @ jquery.js:9143clickToCalculate @ twt.html:71onclick @ twt.html:423 twt.html:1

XMLHttpRequest cannot load https://api.twitter.com/oauth/request_token. Response for preflight has invalid HTTP status code 400 twt.html:82

Object {readyState: 0, status: 0, statusText: "error"}

My request URL page is http://kurbhatt.github.io/twt.html, you can check it's page source as well from https://github.com/kurbhatt/kurbhatt.github.io/blob/master/twt.html source page.

I have put valid and enough data from twitter apps to this page.

Can anyone tell me why I am facing this issue ?

Since last 3-4 days I am working on this issue, still not get solution for the issue.

And sometimes it gave me another type of error:

XMLHttpRequest cannot load https://api.twitter.com/oauth/request_token. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://kurbhatt.github.io' is therefore not allowed access. The response had HTTP status code 400.

2
Just being curions, why are you sending the "Access-Control-Allow-Origin" header in your ajax request? That is supose to come from the server. See : stackoverflow.com/a/10143166/643039Mathieu de Lorimier
hello @MathieudeLorimier.. I have removed that header from request though also I am not able to have a request_token.user3145373 ツ
Looks like you are trying to get the request_token from client side.Aniket Sahrawat
@AniketSahrawat, not actually.. from my page I am requesting to api.twitter.com/oauth/request_token which is server for issuing token.user3145373 ツ
as far as I know, twitter does not provide a JS library for that, that is why they don't allow cross origin requests. what you will need is, a JS that call to a script in your own domain, and have that "script" communicating with twitter server.am05mhz

2 Answers

1
votes

You are making a "GET" request to the api.

twitter api for request_token accepts only POST.

Fix your ajax request to make a POST request. The option is method not type

$.ajax({ method: "POST", ...

0
votes

well, i would like to reply your comment in the comment, but that would be too tiring.

what you need to do to get the request token is do an HTTP POST to api.twitter.com/oauth/request_token with addtional headers, that is Authorization header, as stated in this twitter docs.

you can easily do that by creating a cURL POST request from your web server to the url above, let's say you have that cURL page in yourdomain.com/twittercurl, therefore you can make your jQuery call to that page instead of the twitter request token URL.

as I don't know which server side programming language you are using, you need to find the appropriate cURL call.

but, aside from that, here are some links to read, maybe you can find usefull informations from it: