Client Authorization example using `node-fetch` instead of depreciated `request`

The request library is depreciated and I'm trying to extrapolate what is needed in order to use the node-fetch library in node.js. Is there anywhere I could look at an example of using node-fetch instead of the depreciated request library that is in the current documentation example?

Hi Dan,

Thanks for bringing this to our attention. I actually have a group of developers going through all of our tutorials right now to update them and make sure they are valid. I know this won't help you right this minute, but I am meeting with them tomorrow and will make sure that this one gets on their radar to fix. If you want to direct message me in a couple of days I can give you an update on status.

Thanks
John Carnell
Manager, Developer Engagement.

Hi John:

That's great to hear! I built a proof of concept application using the old request library and now that it's proven (mostly) I want to rebuild things that are more "per spec". I'm going to keep stabbing away at figuring out how to make my request using node-fetch and if I come up with something before I hear back from your side, I'll post what I discover.

Thanks for such a fast response!

Dan

With a little more digging, I finally got and used the access token using node-fetch. Code is below:

const fetch = require('node-fetch')

const client_auth_id = process.env.client_auth_id
const client_auth_secret = process.env.client_auth_secret
let auth = "Basic " + Buffer.from(client_auth_id+":"+client_auth_secret).toString('base64')

let url = "https://login.mypurecloud.com/oauth/token"

let headers = {
    "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8",
    "Authorization": auth
}

fetch(url, {
    method: 'POST',
    headers: headers,
    body: 'grant_type=client_credentials'
})
.then(res => res.json())
.then(json => {

    let url = 'https://api.mypurecloud.com/api/v2/authorization/roles'

    fetch(url, {
        method: 'GET',
        headers: {'Authorization': json.token_type + " " + json.access_token}
    })
    .then( res => res.text())
    .then (text => {
        console.log(text);
    })

})

Hi Dan,

Sorry I missed your response back. Thanks for the code snippet. I asked some of our content devs to mixup our code examples to use both node-fetch and axios so that we ended up with a couple of different code examples using some more modern libraries. They should be published in the next week or so.

Thanks,
John Carnell
Manager, Developer Engagement

1 Like

Hi John,

I see the examples are updated now, much appreciated! Especially the updated Code Authorization example.

Thanks,
Dan

1 Like

Hi Dan,

Excellent! I have been working with our partner developer evangelist team and they have some very good JavaScript developers and had some extra bandwidth, so they were able to turn this around quickly. :slight_smile:

Thanks @PrinceMerluza for your help.

Thanks,
John Carnell
Manager, Developer Engagement

1 Like

This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.