Reputation: 13
I am trying to call 2 3rd party apis. 1st to login and receive Bearer token and 2nd to post message. 1st works fine and I get the token. but when I try to call second api to post message it fails, probably because I dont know how to set the received token in 2nd api
here is my code
var myJSONObject = {
"email": auth[0],
"password": auth[1]
};
req.post({
url: "{{server_url}}/auth/login",
method: "POST",
json: true,
body: myJSONObject
}, function (error, res, body){
if(error){
console.log(error.message);
} else {
var myJSONObject1 = {
"category":"SYSTEM",
"type": "ALERT",
"keywords":"FUNCTION|createSomethingLvl1",
"status":"UNREAD",
"from": "[email protected]",
"to": "[email protected]",
"subject": "Some nice subject",
"body": "Some detailed body that contains information that informs the person"
};
req.post({
url: "{{server_url}}/api/message",
method: "POST",
headers: {
"Authorization": res.body.access_token,
"Content-Type": "application/json"
},
json: true,
body: myJSONObject1
}, function (err, res1, body){
if(error){
console.log(err.message);
} else {
console.log(res1.body);
}
});
}
});
Upvotes: 1
Views: 1006
Reputation: 707318
If this is a "typical" http bearer token, then you need the word "Bearer" in front of the token like this:
"Authorization": "Bearer " + res.body.access_token,
You can see examples of a Bearer token in the OAuth RFC 6750 where it shows the grammar as:
b64token = 1*( ALPHA / DIGIT / "-" / "." / "_" / "~" / "+" / "/" ) *"="
"Bearer" 1*SP b64token
And, here's an example from a Google API doc:
Authorization: Bearer AbCdEf123456
And, another example from an OAuth doc:
Authorization: Bearer vF9dft4qmT
Upvotes: 1