Issues translating token refresh request from API Docs to Node.js/Javascript

I’m currently attempting to have my token refresh whenever I boot up my chatbot. I’m able to get an access token and a refresh token when first using the code, but when I restart the bot to see if it will automatically updated, it doesn’t seem to be working. I believe the issue to be somewhere within my request call, most likely the options here for the refresh request:

let optsAuth = {
url: “https://id.twitch.tv/oauth2/token--data-urlencode?grant_type=refresh_token&refresh_token=” +
refreshToken + “&client_id=” + clientID + “&client_secret=” + clientSecret,
method: “POST”
}
getAuthToken(optsAuth);

Here is the getAuthToken() method:

function getAuthToken(optsAutho) {
request(optsAutho, function(err, response, body) {
try {
console.log(body);
response = JSON.parse(body);
console.log(response);
authToken = response.access_token;
refreshToken = response.refresh_token;
} catch (err) {
console.log(err);
}
})
}

I’m not too comfortable with making calls to the API quite yet, but I’ve been able to squeeze by with pretty much everything I’ve needed, but this.

curl -X POST https://id.twitch.tv/oauth2/token
    --data-urlencode
    ?grant_type=refresh_token
    &refresh_token=<your refresh token>
    &client_id=<your client ID>
    &client_secret=<your client secret>

This is the refresh request given in the docs for the New API, but I’m unsure as to how that would translate into Node.js/Javascript. I’ve been using the request module to make all the calls to the API, and just grouping all the…parameters, I guess you’d call them, (the clientID and refreshtoken, etc.) into the url while keeping the same formatting from the code shown in the docs, except for the new lines of course.

This has worked for all of the my other requests, even getting the access token and refresh token. After some testing, it seems I’m receiving a “404 page not found” back when making the refresh request instead of the JSON object with the new tokens. This is why I believe it to be something with my request and not actually on Twitch’s end.

Any help with this would be greatly appreciated. If you need any further information, please feel free to ask.

Thank you for your time!

Remove --data-urlencode from the URL, in the example from the docs that is part of the curl request options, not part of the URL which is why you’re getting a 404 error

That worked wonders! Thank you very much for the quick reply!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.