shernshiou / node-uber

Uber API nodejs wrapper
MIT License
188 stars 66 forks source link

invalid_grant on request when obtaining access_token from somewhere else #70

Open maakle opened 7 years ago

maakle commented 7 years ago

Hello, I have an iOS App where I use SSO to authenticate the user and then save the accessToken & refreshToken locally to keychain on my device. Then I'm calling my server who uses a javascript background function to call your library to make a request to Uber. So far, I'm trying to set up your library with my 2 local tokens like this:

 var uber = new uberClient({
    client_id: '...',
    client_secret: '...',
    server_token: '...',
    name: 'My App',
    sandbox: true, //optional
    access_token: accessToken,
    refresh_token: refreshToken
  });

afterwards I want to call the uber.requests.getEstimatesAsync endpoint like this:

uber.requests.getEstimatesAsync({
      "start_latitude": pickupLocation["lat"],
      "start_longitude": pickupLocation["lng"],
      "end_latitude": dropoffLocation["lat"],
      "end_longitude": dropoffLocation["lng"]
      })
      .then(function(res) { 
        console.log(JSON.stringify(res)); 
      })
      .error(function(err) { 
        console.error(err); 
      });
})

Though every time I get an "invalid_grant" error while doing this. Did I make a mistake authenticating myself or setting up the Uber client wrong? Is it even possible to add my local accessToken & refreshToken manually to your uber client? I'm using a Developer account for doing this, therefore I should actually have all the required permissions for the request endpoint, but I also obtained them previously in the App.

Best regards, Matt

MAKEADM commented 7 years ago

Hello, currently i have the same issue.

supermarioim commented 7 years ago

I also have same issue. Tried setting access_token with uber.access_token = 'xxx' and in init options and all the same, i always get "invalid_grant" error. I'm using latest version of module.

maakle commented 7 years ago

So I think it's a problem with the library itself. Because once I made the request with http with the "request" library (https://github.com/request/request) it worked. Include for that at the top of your code:

var request = require('request');

Both OAuth2 and SSO accessToken worked. You should give the method a pickupLocation with latitude and longitude and your obtained accessToken from Uber like this:

function getAllAvailableUberProducts(pickupLocation, accessToken){
    var lat = pickupLocation["lat"].toString();
    var lng = pickupLocation["lng"].toString();

    var options = {
        uri: "https://api.uber.com/v1.2/products?latitude="+lat+"&longitude="+lng,
        method: 'GET',
        headers: {
            "Authorization": "Bearer " + accessToken,
            "Accept-Language": "en_US",
            "Content-Type": "application/json"
        }
    };

    request(options, function (error, response, body) {
        if (!error && response.statusCode == 200) {
            console.log(JSON.parse(body).products);
        } else {
            console.log(error);
        }
    });
}

I hope this helps someone.