I'm trying to send a request to the Schoology API but I'm met with a 401 error, which means that my Authentication headers are incorrect (I think).
I'm using Axios to send the request like this:
function getUsers(headers) {
axios.get('https://api.schoology.com/v1/users', {
headers: headers
}).then(data => {
console.log(data);
}).catch(err => {
console.error(err)
})
}
And I'm calling this function in another file like this:
const schoologyHeaders = {
"Accept": "application/json",
"Host": "api.schoology.com",
"Content-Type": "application/json",
"Authorization": {
"realm": "Schoology API",
"oauth_consumer_key": process.env.SCHOOLOGY_CONSUMER_KEY,
"oauth_token": "",
"oauth_nonce": uuidv4(),
"oauth_timestamp": Math.floor(Date.now()/1000).toString(),
"oauth_signature_method": "PLAINTEXT",
"oauth_version": "1.0",
"oauth_signature": process.env.SCHOOLOGY_CONSUMER_SECRET + "%26"
}
}
schoology.getUsers(schoologyHeaders)
I believe all of my headers conform to the schema laid on in the link above under the OAuth Types - Two-Legged section, but I keep getting this 401 error.
How should I go about debugging this? I couldn't find much in the error itself to help me through this, and I've checked multiple times that my headers follow what is laid out in the API documentation. Could the nonce or timestamp be the issue? Any help is appreciated!
Note:
oauth_tokenis intentionally left blank per the Two-Legged auth header schema in the link above
I was able to solve this issue by reformatting the Authorization header to be a string rather than an object:
const schoologyHeaders = {
"Host": "api.schoology.com",
"Content-Type": "application/json",
"Authorization": `OAuth realm="Schoology API", oauth_consumer_key="${process.env.SCHOOLOGY_CONSUMER_KEY}", oauth_token="", oauth_nonce="${uuidv4()}", oauth_timestamp="${Math.floor(Date.now()/1000).toString()}", oauth_signature_method="PLAINTEXT", oauth_version="1.0", oauth_signature="${process.env.SCHOOLOGY_CONSUMER_SECRET}%26"`,
}