I try to set headers with Oauth 'Authorization': 'Bearer XXXXX'
.
But, I got an error.
{"_body":"{\"error\":\"Unauthenticated.\"}","status":401,"ok":false,"statusText":"Unauthorized","headers":{"Cache-Control":["no-cache","
private"],"Content-Type":["application/json"]},"type":2,"url":"http://myurl.local/api/getApi"}
p_headers = new Headers( // Header for POST, PUT, DELETE
{
'Content-Type' : 'application/json',
'Authorization' : this.tokens.data['token_type']+' '+this.tokens.data['access_token']
});
p_options = new RequestOptions({ headers: this.p_headers }); // Options headers for POST, PUT, DELETE
g_headers = new Headers({ // Header for GET
'Authorization' : this.tokens.data['token_type']+' '+this.tokens.data['access_token']
});
g_options = new RequestOptions({ headers: this.g_headers }); // Options headers for GET
getAPI() {
return new Promise((resolve, reject) => {
this.http.get(this.url, this.g_options)
.toPromise()
.then((response) =>
{
console.log('API Response : ', response.json());
resolve(response.json());
})
.catch((error) =>
{
console.error('API Error : ', error.status);
console.error('API Error : ', JSON.stringify(error));
reject(error.json());
});
});
}
postAPI() {
let data JSON.stringify({ ... });
return new Promise((resolve, reject) => {
this.http.post(this.url, data, this.p_options)
.toPromise()
.then((response) =>
{
console.log('API Response : ', response.json());
resolve(response.json());
})
.catch((error) =>
{
console.error('API Error : ', error.status);
console.error('API Error : ', JSON.stringify(error));
reject(error.json());
});
});
}
In API they call with out Authorization
is fine.
How to set header for it?