angularhttpionic2uber-api

Keep getting unsupported_grant_type error using Angular 2 http post request


I am trying to authenticate the Uber API using Ionic 2 and Angular 2. I am able to successfully get the authorization code and authenticate using postman but when I put the exact same parameters in an http.post request I keep getting

{"error":"unsupported_grant_type"}

Here is my code:

token() {

let headers = new Headers();
headers.append('Content-Type', 'application/json');

let urlSearchParams = new URLSearchParams();
urlSearchParams.append('client_id', '<CLIENT_ID>');
urlSearchParams.append('client_secret', '<CLIENT_SECRET>');
urlSearchParams.append('redirect_uri', 'http://localhost:8100/callback');
urlSearchParams.append('grant_type', 'authorization_code');
urlSearchParams.append('code', this.authorizationCode);

let body = urlSearchParams.toString()


return this.http.post(`https://login.uber.com/oauth/v2/token`, body, {
        headers: headers
    })
    .subscribe(data => {
        alert('ok');
    }, error => {
        console.log(JSON.stringify(error.json()));
    });
}

Any help would be greatly appreciated. Thank you so much!


Solution

  • Try adding form url encode in header

    headers.append('Content-Type', 'application/x-www-form-urlencoded');