Comments (11)
Okay, didn't know if it was on my end. I can possibly look into contributing some tests this weekend!
from twitter-lite.
twitter-lite
makes it easier to pass parameters than having to shove them into URLs :)
Check the example for user_timeline.
from twitter-lite.
Thank you for the quick reply, but the error still stands, I don't think it is related to how I am passing the variables in...?
from twitter-lite.
We should create some tests for the client, @peterpme.
from twitter-lite.
Thank you for the quick reply, but the error still stands, I don't think it is related to how I am passing the variables in...?
You have to use a CORS proxy (like https://cors-anywhere.herokuapp.com/
). Change your subdomain like below. There are also other CORS services that you can use or you can setup your own. Don't use these public proxy services for production.
After changing the subdomain like this, the CORS error no longer appears but I get 401
error for my GET
and POST
requests. I couldn't figure out what's the problem. At least you can give it a shot.
const client = new Twitter({
subdomain: "cors-anywhere.herokuapp.com/https://api",
consumer_key: X,
consumer_secret: X,
access_token_key: X,
access_token_secret: X,
});
from twitter-lite.
@mbdilaver So maybe for production creating my own backend as a simple data pass through may be the best way for now?
from twitter-lite.
@KeevanDance Yeah that looks like the ideal solution. (https://developer.mozilla.org/tr/docs/Web/HTTP/CORS)
from twitter-lite.
@mbdilaver Thanks, I appreciate the quick response :)
from twitter-lite.
@DanceParty, Check out this solution.
The reason behind CORS error as I've recently found out is the absence of Access-Control-Allow-Origin
in the request header which browser doesn't send. This is actually a good requirement by twitter because it encourages the devs to use server-side as opposed to browser which can adds a cyber security risk of leaking twitter app api keys form bad hands.
You can use the above solution for the development mode. But for production, if you are creating static site hosted on Netlify, Firebase, Heroku, for example, you can use their in-built proxy feature. This means then when making such twitter requests from your custom domain, it'll not encounter preflight CORS issue.
Hope this slightly long comment helps out.
from twitter-lite.
Thank you for the quick reply, but the error still stands, I don't think it is related to how I am passing the variables in...?
You have to use a CORS proxy (like
https://cors-anywhere.herokuapp.com/
). Change your subdomain like below. There are also other CORS services that you can use or you can setup your own. Don't use these public proxy services for production.
After changing the subdomain like this, the CORS error no longer appears but I get401
error for myGET
andPOST
requests. I couldn't figure out what's the problem. At least you can give it a shot.const client = new Twitter({ subdomain: "cors-anywhere.herokuapp.com/https://api", consumer_key: X, consumer_secret: X, access_token_key: X, access_token_secret: X, });
@mbdilaveany, were you able to figure out what causes the 401 error?
from twitter-lite.
I think I found the origin of the 401 error.
This lib delegate authentication header generation to oauth-1.0a
dependency (https://github.com/ddo/oauth-1.0a) when it uses Twitter OAuth 1.0a authentication method (https://developer.twitter.com/en/docs/authentication/oauth-1-0a)
Authentication header should be computed with the endpoint URL without the proxy, but when you pass the subdomain containing the CORS proxy, it is used everywhere, event for this authentication header generation.
I found this workaround:
// In browser, use a CORS proxy
// From https://github.com/draftbit/twitter-lite/issues/41#issuecomment-467403918
const subdomain = isBrowser
? `${configuration.corsProxyURL.slice("https://".length)}https://api`
: "api";
const client = new Twitter({
subdomain,
version: "1.1",
consumer_key: consumerKey,
consumer_secret: consumerSecret,
access_token_key: accessTokenKey,
access_token_secret: accessTokenSecret,
});
// Monkey patch oauth client used by twitter-lite in order to
// ignore cors proxy url when generating authentication headers
const originalAuthorizeFunction = client.client.authorize;
client.client.authorize = function (request, token) {
let requestWihoutCorsProxy = request;
if (request.url.startsWith(configuration.corsProxyURL)) {
const requestUrlNotProxyfied = request.url.slice(
configuration.corsProxyURL.length
);
requestWihoutCorsProxy = { ...request, url: requestUrlNotProxyfied };
}
return originalAuthorizeFunction.call(
client.client,
requestWihoutCorsProxy,
token
);
};
from twitter-lite.
Related Issues (20)
- URL Card Doesn't Appear
- API v2 documentation
- Rate limit handling could be more explicit
- Cannot like a tweet with v2 HOT 3
- TypeScript does not like `new Twitter({ bearer_token })` HOT 1
- How do i tweet to someone who just followed me?
- twitter-lite client stream method always defaults to api version 1.1 HOT 3
- POST to the Cards endpoint fails HOT 1
- Two ways to access data? HOT 1
- Only allow code to post tweets from the account defined in the "follow" parameter.
- Stops Posting Tweets After a Certain Time HOT 1
- How to post link text / images HOT 3
- OAuth 2 error for POST /2/tweets FIXED HOT 15
- Media from Tweet lookup
- Twitter Bookmarks API Endpoint HOT 1
- Tweet on behalf of other users?
- Does `twitter-lite` report the current status of rate limits, in order to get the count of the API requests remaining in the current time-window? HOT 1
- Upcoming API changes
- ReferenceError: Property 'Buffer' doesn't exist
- Could not authenticate you
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from twitter-lite.