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);
});
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
pm.environment.set()
line as it looks like it's not used and changevariables
tocollectionVariables
. If you set the Auth to 'No Auth' then in the request, manually add anAuthorization
Header with the{{accessToken}}
value does that work? – Danny DaintonBearer {{accessToken}}
to the header as that's what the Auth helper would have done. – Danny Dainton