ttezel / twit

Twitter API Client for node (REST & Streaming API)
4.31k stars 568 forks source link

Response to preflight request doesn't pass access control check #405

Open GuiWukai opened 6 years ago

GuiWukai commented 6 years ago

Hi,

I am running this code but it gives a 400 error. My consumer_key, consumer_secret, access_token, access_token have been generated recently and the access-level is set to read-only.

My question: Does anyone know how solve this issue?

let T = new twit({
            consumer_key: "VhUz5pCky6RJwM6ZwgSZJCDjh",
            consumer_secret: "  FvyL8eFtaQjl76aWhe10ykHKi3KxwW15kNbClgDv0EtHIX13wy",
            access_token: "781915379807428608-LHN8q38jtFp75D6aa2oTvAPO6JiaOEp",
            access_token_secret: "OGUd9nnjjELr4UxEyHuadqzcglBnToyb17E9J48Ljc8Bi",
            timeout_ms: 60*1000,
})

T.get('search/tweets', { q: 'banana since:2011-07-11', count: 100 }, function(err, data, response) {
            console.log(data)
})

This is the error I am getting:

Failed to load https://api.twitter.com/1.1/search/tweets.json?q=banana%20since%3A2011-07-11&count=100: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access. The response had HTTP status code 400. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.

Thanks!

cogitoergosumsw commented 6 years ago

Twitter API does not allow CORS. You would need to call the search function from backend and then pass the data back to frontend to render. Read here for more info

pythoncreate commented 6 years ago

any tips on how to do this, if i'm using node/express/react?

I just tried to move this function into my server.js file and then import the function from my react component.

I get an error saying that I can't import outside of my src directory