Getting access token with axios

34,853

Solution 1

According to the docs from Lyft (https://developer.lyft.com/docs/authentication), you need to use HTTP Basic auth.

var axios = require("axios");

axios.request({
  url: "/oauth/token",
  method: "post",
  baseURL: "https://api.lyft.com/",
  auth: {
    username: "vaf7vX0LpsL5",
    password: "pVEosNa5TuK2x7UBG_ZlONonDsgJc3L1"
  },
  data: {
    "grant_type": "client_credentials",
    "scope": "public"    
  }
}).then(function(res) {
  console.log(res);  
});

Happy coding :)

!IMPORTANT THING!
I strongly recommend you to change your secret_id and client_secret asap, because they are not the things to be public, if you use them for an important project or something like that.

Solution 2

I have solved my problem with this code.

var reqData = "grant_type=password&username=test&password=asd";
         Axios({
    method: 'post',
    url: 'http://localhost:60439/token',
        data: (reqData),   

    headers: { 
      "Content-Type": "application/x-www-form-urlencoded",
    }
  }).then((response) =>{
            console.log(response)
        }).catch((error) =>{
            console.log(error);
        })
Share:
34,853
Mike
Author by

Mike

Updated on July 09, 2022

Comments

  • Mike
    Mike almost 2 years

    I'm working with the Lyft API, and trying to figure out how to get an access token with axios with a node script.

    I can manually get an access token by using Postman by filling out the form like this:

    Getting token inside of Postman

    When I fill out the form, I can get a new token from Lyft successfully.

    I'm trying to translate this into a POST request using axios by doing this:

    var axios = require('axios');
    var data = {
    "grant_type": "client_credentials",
    "scope": "public",
    "client_id": "XXXXXXXXX",
    "client_secret": "XXXXXXXX"
    };
    var url = "https://api.lyft.com/oauth/token";
      return axios.post(url, data)
        .then(function(response){
            console.log(response.data)
        })
        .catch(function (error) {
          console.log(error);
        });
    

    When I run the script, I get this error:

    { error_description: 'Unauthorized', error: 'invalid_client' }
    

    What am I missing from my axios request? Any help would be appreciated!

  • Mike
    Mike over 7 years
    Thanks it worked! Also, I just refreshed the credentials, thanks for the security tip
  • Rajendra
    Rajendra almost 6 years
    Although Axios is great for API access, using your client ID and secret in Javascript is not secure, as code is visible to public. I'm also searching for this issue ... to use oAuth credentials in browser or Cordova apps.
  • technoY2K
    technoY2K over 4 years
    Yes, and don't make the mistake I made. The following config is incorrect. data: { 'grant_type': 'client_credentials' } You must use the grant_type=credentials format.
  • Akhil Ghatiki
    Akhil Ghatiki almost 3 years
    this answer is missing content type.