Giter Site home page Giter Site logo

Comments (11)

DanceParty avatar DanceParty commented on June 1, 2024 1

Okay, didn't know if it was on my end. I can possibly look into contributing some tests this weekend!

from twitter-lite.

dandv avatar dandv commented on June 1, 2024

twitter-lite makes it easier to pass parameters than having to shove them into URLs :)

Check the example for user_timeline.

from twitter-lite.

DanceParty avatar DanceParty commented on June 1, 2024

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...?

image

image

from twitter-lite.

dandv avatar dandv commented on June 1, 2024

We should create some tests for the client, @peterpme.

from twitter-lite.

mbdilaver avatar mbdilaver commented on June 1, 2024

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...?

image

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.

DanceParty avatar DanceParty commented on June 1, 2024

@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.

mbdilaver avatar mbdilaver commented on June 1, 2024

@KeevanDance Yeah that looks like the ideal solution. (https://developer.mozilla.org/tr/docs/Web/HTTP/CORS)

from twitter-lite.

DanceParty avatar DanceParty commented on June 1, 2024

@mbdilaver Thanks, I appreciate the quick response :)

from twitter-lite.

waleedshkt avatar waleedshkt commented on June 1, 2024

@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.

MauriceOppenberger avatar MauriceOppenberger commented on June 1, 2024

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...?
image

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,
});

@mbdilaveany, were you able to figure out what causes the 401 error?

from twitter-lite.

acailly avatar acailly commented on June 1, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.