0
votes

I have faced with a problem. I've never used OAuth 2.0 authentication and now I'm trying to make an API call using OAuth 2.0 Authentication. Well, I've made a call using Postman, but now I want to automate that. What I mean, I want to get a token in to variable and after an every new call I want to generate new access token.

I have done some things, but still I'm getting a response saying: "Authorization failure". I've wrote this in collection Pre-requrest Scripts:

let tokenUrl = 'tokenUrl';
let clientId = 'clientId';
let clientSecret = 'secret';
let scope = 'scope'

let getTokenRequest = {
    method: 'GET',
    url: tokenUrl,
    auth: {
        type: "basic",
        basic: [
            { key: "username", value: clientId },
            { key: "password", value: clientSecret }
        ]
    },
    body: {
        mode: 'formdata',
        formdata: [
            { key: 'grant_type', value: 'client_credentials' },
            { key: 'scope', value: scope }
        ]
    }
};

pm.sendRequest(getTokenRequest, (err, response) => {
    let jsonResponse = response.json(),
        newAccessToken = jsonResponse.access_token;

    console.log({ err, jsonResponse, newAccessToken })

    pm.environment.set('accessToken', newAccessToken);
    pm.variables.set('accessToken', newAccessToken);
});

enter image description here

After that, when I add a new request and in authorization type i chose inherit auth from parent and trying to send a request, it gives me a response: "Authorization failure"

P.S: This is the source website, where I took a script from: https://marcin-chwedczuk.github.io/automatically-generate-new-oauth2-tokens-when-using-postman

1
You're setting the accessToken twice, is that just because you copied the code from that tutorial? You can remove the pm.environment.set() line as it looks like it's not used and change variables to collectionVariables. If you set the Auth to 'No Auth' then in the request, manually add an Authorization Header with the {{accessToken}} value does that work?Danny Dainton
I've made as you said, but the result is same. Here is console log: jsonResponse: error:"invalid_request"brithwulf
When I try to use it on my API call it says: "Authorization failure", but as it is shown in tutorial with the following apis it works correctly.brithwulf
You might need to also add Bearer {{accessToken}} to the header as that's what the Auth helper would have done.Danny Dainton
How or where should I add this ? I've added in every field Bearer {{accessToken}}, but same result.brithwulf

1 Answers

0
votes

I've resolved my problem by changing method to POST. I had an error in the script. So, new script looks like that:

let tokenUrl = 'tokenUrl';
let clientId = 'clientId';
let clientSecret = 'secret';
let scope = 'scope'

let getTokenRequest = {
    method: 'POST',
    url: tokenUrl,
    auth: {
        type: "basic",
        basic: [
            { key: "username", value: clientId },
            { key: "password", value: clientSecret }
        ]
    },
    body: {
        mode: 'formdata',
        formdata: [
            { key: 'grant_type', value: 'client_credentials' },
            { key: 'scope', value: scope }
        ]
    }
};

pm.sendRequest(getTokenRequest, (err, response) => {
    let jsonResponse = response.json(),
        newAccessToken = jsonResponse.access_token;

    console.log({ err, jsonResponse, newAccessToken })

    pm.environment.set('accessToken', newAccessToken);
    pm.variables.set('accessToken', newAccessToken);
});