2

I am trying to make a GET request to the UI API from within a react project using a simple lifecycle method:

componentDidMount() {
    fetch('https://{my_salesforce_instance}.salesforce.com/services/data/v51.0/ui-api/object-info/Account')
        .then(response => console.log(response)
}

However I am just receiving a 401 (Unauthorized) error.

I have created a Connected App from my Salesforce DE org, but does anyone know how I can authorise my code to communicate with it? Or is there some other issue causing the error?

2

2 Answers 2

1

This article explains how you can create an authorization code and retrieve an access token. You then need to pass the token into your fetch request using headers:

componentDidMount() {
  const token = {
      method: 'POST',
      headers: {
          'Authorization': 'Bearer ' + accessToken,
          'Content-Type': 'application/json'
      }
  }
  fetch('https://{my_salesforce_instance}.salesforce.com/services/data/v51.0/ui- api/object-info/Account', token)
    .then(response => console.log(response)

}

1

You'll have to use one of the OAuth flows to retrieve an access token. Then you'll need to set that access token in the header of your get request.

Here is an overview of the theory on the different OAuth flows: https://help.salesforce.com/articleView?id=remoteaccess_oauth_flows.htm&language=en_US

If you want to see how they could be implemented in practice in JavaScript, I have an example application on my GitHub: https://github.com/NVandenBossche/salesforce-iam-flows

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.