简体   繁体   中英

Generating Access token with axios in react-native

POSTMAN sample

the same process i want to do it in react-native and i have tried like that

var baseHeaders = {
      'Content-Type': 'application/x-www-form-urlencoded',
      'Authorization': 'Bearer ' + btoa(client_id + ':' + client_secret)
  };

var params = {
    client_id: client_id,
    client_secret: client_secret,
    grant_type: "client_credentials",
}

axios({
      method: 'POST',
      url: "http://transrv02-ap01.transsyssolutions.com:8080/apex/apxprd/oauth/token",
      headers: baseHeaders,  
      body:params      
    })
      .then((responseJson) => {  console.log("clientid---"+responseJson)})
      .catch((error) => {
        console.error(error);
      });

but it have showing 401 error. Anyone can help me! thanks in advance....

You can try this...

axios.post('http://transrv02-ap01.transsyssolutions.com:8080/apex/apxprd/oauth/token',
    params,
    {
      headers: baseHeaders
    })
  .then((responseJson) => {  console.log("clientid---"+responseJson)})
  .catch((error) => {
    console.error(error);
  });

Finally I Found My own way not in axios

var baseHeaders = {
  'Content-Type': 'application/x-www-form-urlencoded',
  'Authorization': "Basic " + btoa(client_id + ":" + client_secret)
};
console.log(JSON.stringify(baseHeaders) + "baseHeaders")

var params = "grant_type=client_credentials";

console.log(JSON.stringify(params) + "params")

return fetch('http://apex/apxprd/oauth/token',{
  method: "POST",
  body: params,
  headers: baseHeaders
}).then((response) => response.json()).then((responsetokenJson) => {
  console.log(JSON.stringify(responsetokenJson) + "responseJsonclientid")
  var token = responsetokenJson.access_token
  console.log("this.props.tokens--" + token)

  this.setState({
    accessToken: token
  })
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM