Oauth2.0 on a third party api

On integrating a third party API for an LMS, i get the error, “full authentication is required”. Where and how should i enter the parameters that the API is expecting , and the credentials for log in and accessing the LMS through the GET request in API?

the params in the image are the ones that the api is expecting. I was told that I also need to enter my personal credentials for the LMS software, how to proceed?

The following is an example for a Microsoft login.

You will need to refer to the documentation for your system to confirm if its the same.

You also need the tenant ID (for the application) which goes in the URL.

The grant_type is ‘password’ which looks the same as yours.

The username and password will be your test account.

The client_ID comes from your application, and the client_secret needs to be generated from within your application dashboard.

The scope for mine is ‘openid profile email’. Not sure if it will be the same for you.

But since it is an oauth2.0, the access token becomes invalid after an hour. How do i set it up so it redirects to the refresh access token url, gets the new token and adds it in the Header as Bearer:xyz automatically?

This isn’t usually a problem, as you usually have a collection with individual requests for the login and at least one other request. You just make sure they are in the right order in the collection\folder. This means that is gets a new token every time.

If that is an issue, you can use the pre-request method instead.

This will run once before each request and can be coded to check for the expiry date.

You set the pre-request to check the token expiry date and get a new one if needed.

Something like


pm.test("Check for collectionVariables", function () {
    let vars = ['clientId', 'clientSecret', 'tenantId', 'username', 'password', 'scope'];
    vars.forEach(function (item, index, array) {
        console.log(item, index);
        pm.expect(pm.collectionVariables.get(item), item + " variable not set").to.not.be.undefined;
        pm.expect(pm.collectionVariables.get(item), item + " variable not set").to.not.be.empty; 
    });

    if (!pm.collectionVariables.get("bearerToken") || Date.now() > new Date(pm.collectionVariables.get("bearerTokenExpiresOn") * 1000)) {
        pm.sendRequest({
            url: 'https://login.microsoftonline.com/' + pm.collectionVariables.get("tenantId") + '/oauth2/v2.0/token',
            method: 'POST',
            header: 'Content-Type: application/x-www-form-urlencoded',
            body: {
                mode: 'urlencoded',
                urlencoded: [
                    { key: "client_id", value: pm.collectionVariables.get("clientId"), disabled: false },
                    { key: "scope", value: pm.collectionVariables.get("scope"), disabled: false },
                    { key: "username", value: pm.collectionVariables.get("username"), disabled: false },
                    { key: "password", value: pm.collectionVariables.get("password"), disabled: false },                    
                    { key: "client_secret", value: pm.collectionVariables.get("clientSecret"), disabled: false },
                    { key: "grant_type", value: "password", disabled: false },
                ]
            }
        }, function (err, res) {
            if (err) {
                console.log(err);
            } else {
                let resJson = res.json();
                pm.collectionVariables.set("bearerTokenExpiresOn", resJson.expires_in);
                pm.collectionVariables.set("bearerToken", resJson.id_token);
            }
        });
    }
});