2

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.

7
  • 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/643039 Commented Nov 15, 2016 at 15:28
  • hello @MathieudeLorimier.. I have removed that header from request though also I am not able to have a request_token. Commented Nov 16, 2016 at 5:10
  • Looks like you are trying to get the request_token from client side. Commented Nov 17, 2016 at 12:40
  • @AniketSahrawat, not actually.. from my page I am requesting to api.twitter.com/oauth/request_token which is server for issuing token. Commented Nov 18, 2016 at 6:29
  • 1
    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. Commented Nov 21, 2016 at 2:56

2 Answers 2

1

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", ...

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

2 Comments

Hello, I have tried with adding method instead of type, though also it is not working.
any luck on that?
0

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:

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.