I am having issues with exchanging a code for a token on the Faceit OAuth. The documentation states the following:
Exchanging the Authorization Code for an Access Token
The third-party app needs to make a server-to-server call, providing the Authorization Code, Client ID, and Client Secret. In return, it will receive:
● an Access Token (used for calling the FACEIT API on behalf of the User)
● an ID Token (a digitally signed JWT containing user information from FACEIT)
● a Refresh Token (used to obtain a new Access Token when the current one expires).
To exchange the authorization code, your server must send an HTTPS POST request to the token endpoint obtained from the OpenID configuration endpoint under the key "token_endpoint."
The request should include HTTP Basic Authentication in the "Authorization" header (using your Client ID and Client Secret) and the "application/x-www-form-urlencoded" Content-type header. Include the following parameters in the POST body:
Field Description
code A one-time authorization code received on the callback page
grant_type This field must have the value "authorization_code," as per OAuth 2.0 specification
The code I have so far is as follows:
import axios from 'axios'
import qs from 'qs'
export async function authRoutes(app) {
const clientId = process.env.CLIENT_ID
const clientSecret = process.env.CLIENT_SECRET
const credentials = `${clientId}:${clientSecret}`
const base64Credentials = Buffer.from(credentials, 'utf-8').toString('base64')
const tokenEndpoint = 'https://api.faceit.com/auth/v1/oauth/token'
app.post('/register', async (request) => {
const { code } = request.body
const requestBody = qs.stringify({
grant_type: 'authorization_code',
code,
})
const headers = {
Authorization: `Basic ${base64Credentials}`,
'Content-Type': 'application/x-www-form-urlencoded',
}
try {
const tokenResponse = await axios.post(tokenEndpoint, requestBody, {
headers,
})
return tokenResponse.data
} catch (error) {
console.log(error.message)
return {
error,
}
}
})
}
When testing, I keep receiving a 401 BAD REQUEST error. Despite trying everything, I can't seem to get a response from this endpoint.
Although the endpoint is documented in the most recent Faceit documentation, I'm facing difficulties connecting to it.