I’m having issues with oauth when requesting an access token, in exchange for an auth token. I have the client id and client secret correct, and redirect_uri is the same one as the one I used to retrieve the auth token.
I am receiving data: { code: 40010001, message: ‘invalid_grant’ } error with status: 422 .
Here is a code snippet. It is in TypeScript.
const tokenEndpoint = 'https://api.alpaca.markets/oauth/token';
try {
const data = new URLSearchParams({
grant_type: 'authorization_code',
code: authToken,
client_id: this.ALPACA_CLIENT_ID,
client_secret: this.ALPACA_CLIENT_SECRET,
redirect_uri: this.ALPACA_REDIRECT_URI,
});
console.log('Request Data:', data.toString());
const response = await axios.post(tokenEndpoint, data.toString(), {
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
});
console.log('Access Token Response:', response.data);
return response.data.access_token;
} catch (error) {
console.error('Error exchanging authorization code:', error);
throw error;
}