Unable to get acess token aftet getting code

var params = {
‘apiSecret’ : API_SECRET,
‘code’ : authorizationCode,
‘grant_type’ : “authorization_code”,
‘redirect_uri’ : REDIRECT_URI
};

if (authorizationCode) {
// Step 4: Exchange the authorization code for an access token
upstox.getAccessToken(params)
.then(function (response) {
const accessToken = response.access_token;
console.log(‘Access Token:’, accessToken);
})
.catch(function (err) {
console.error(‘Error getting access token:’, err);
// Handle the error as needed
});
} else {
console.error(‘Authorization code not found.’);
}

@Sidhant_Pradhan

Could you please provide the reply you got from the API?

Also, it seems that the client_id is not included in the parameters. Could you verify this and attempt once more?

Thank you!

the erorr show as

Error getting access token: { message: ‘Forbidden’ }

and the params are in node modules are

getAccessToken method requires following- 
     var params: {
         "apiSecret" : "your_apiSecret",
         "code" : "your_code_generated_in login",
         "redirect_uri" : "your_redirect_uri"
     };
 
 var accessToken;
 
 upstox.getAccessToken(params)
     .then(function(response) {
       accessToken = response.access_token;
     })
     .catch(function(err) {
         // handle error 
     });

i tried different method but this shows eror as
Error obtaining access token: Request failed with status code 401

const authorizationCode = req.body.requestToken;
const params = {
code: authorizationCode,
client_id: API_KEY,
client_secret: API_SECRET,
redirect_uri: REDIRECT_URI,
grant_type: ‘authorization_code’,
};

const headers = {
‘accept’: ‘application/json’,
‘Api-Version’: ‘2.0’,
‘Content-Type’: ‘application/x-www-form-urlencoded’,
};

if (authorizationCode) {

  axios.post(accessTokenUrl, new URLSearchParams(params), { headers })
.then(response => {
  const accessToken = response.data.access_token;
  console.log('Access Token:', accessToken);
// Now you can use the access token for making authorized requests to Upstox API

})
.catch(error => {
console.error(‘Error obtaining access token:’, error.message);
// Handle the error as needed
});

  } else {
    console.error('Authorization code not found.');
  }

@Sidhant_Pradhan

The error you’re encountering is due to the use of the v1 SDK. It’s important to upgrade to the v2 SDK, as v1 is no longer supported by the Upstox team.

You can find the documentation for the v2 SDK here: API Documentation – Fast Secure Free – Upstox | Upstox Developer API

@Sidhant_Pradhan

Could you provide the response you received from Upstox for the axios-based API call? This will help us offer more specific guidance.

Additionally, kindly share your UCC (user id) so we can review the logs related to this issue.

Thank you!

the reponse i got

Error obtaining access token: Request failed with status code 401

and my user id is 58AWW8

probelm solved .

Thank you!

@Sidhant_Pradhan

I’m glad to hear that the issue has been resolved. If you have any more questions or encounter any other issues in the future, feel free to reach out. Have a great day!