Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Help with Uber API authentication

I'm attempting to make an Uber ride request using their API in a simple Javascript app. I've been able to successfully get price and time estimates for this without authentication.

Ā 

Following the docs for authentication I get stuck on step three to request an access token. The below function is what I'm trying to use to pass the authorization code in return for a access token. The authorization code is passed as a url paramater from Uber like so http://localhost:8888/Spwn2.0/?code=BKDF107k7Wi3EGS4xremZGAUSb2kuy

Ā 

    function getToken() {
      code = location.search.split('code=')[1]
      console.log("code: " + code);

      $.ajax({
        url: "http://login.uber.com/oauth/v2/token",
        type: "GET",
        xhrFields: {
          withCredentials: false
        },
        headers: {
            Authorization: "Token " + uberServerToken
        },
        data: {
            client_secret: uberClientSecret,
            client_id: uberClientId,
            grant_type: "authorization_code",
            redirect_uri: "http://localhost:8888/Spwn2.0",
            code: code
        },
        success: function(result) {
          console.log(result);

        }
      });

    }
Edgar Gil
Edgar Gil
13,322 Points

Try removing the space on "Token " -> "Token"

Edgar Gil I tried that. and got the following error:

Response for preflight is invalid (redirect)