I am trying to authorise my site through an API. The API requires the authorisation header which should, as per their specification, be:
authorization: "Basic " + base64encode(client_id + “:” + client_secret)
I am aware that the web modules do not support window.btoa or Buffer. My code is as follows. I have tried installing various packages but continue to get {error: “invalid_client”} from the API.
const clientsecret = "XXXX"
const clientid = "XXXX"
const url = 'https://identity.xero.com/connect/token';
const redirecturi = 'https://www.someurl.co.uk/oauth';
export function exchangecode(code) {
const authorizationBasic = window.btoa(clientid + ":" + clientsecret);
let headers = {
'Authorization': 'Basic ' + authorizationBasic,
'Content-Type': 'application/x-www-form-urlencoded'
};
let data = 'grant_type=authorization_code&code=' + code + '&redirect_uri=https://www.someurl.co.uk/oauth'
let request = {
"method": 'post',
"headers": headers,
"body": JSON.stringify(data)
};
return fetch(url, request)
.then((httpResponse) => {
return httpResponse.json()
})
}
Does anyone have a working example of encoding credentials for a post request or can anyone shed any light on where I’m going wrong.