Post request returning 403 when trying to call IBM AppID management API /users

190 Views Asked by At

I'm trying to create a custom IBM AppID Management Api interface in my application. In order to do that, I'm using IBM IAM Token Manager library to get an IAM access token.

const itm = require('@ibm-functions/iam-token-manager')
const m = new itm({
    "iamApiKey": apiKey
})
m.getAuthHeader().then(token => {
    console.log("this one won't work", token)
}

var headers = 
{
    'accept': 'application/json',
    'Authorization': token,
    'Content-Type': 'application/json'
};
var options = 
{
    url: replacedIssUrl+"/users",
    method: 'POST',
    headers: headers,
    body: dataString
};
function callback(error, response, body) {
    console.log(response)
    if (!error && response.statusCode == 200) {
        console.log(body); //returns "body: "Forbidden"
    }
}
request(options, callback)

Whenever I try to pre-register a user with the library's generated token, the callback returns Status 403 - Forbidden, but if it gets the IAM Access token directly through ibmcloud shell (ibmcloud iam oauth-tokens), it works fine.

Does anybody have any clue why this is happening? I know for a fact that the IAM Token Manager library generated access token is working, because I'm using it to get the user ID on the same code.

When something is wrong with my Access Token, it usually returns "Unauthorized", not "Forbidden".

I have no clue why this is happening.

Thanks in advance.

1

There are 1 best solutions below

0
On

When passing an IAM token in the headers, App ID expects it to be preceded by the "Bearer " string :

var headers = 
{
    'accept': 'application/json',
    'Authorization': 'Bearer ' + token,
    'Content-Type': 'application/json'
};