1

I'm trying to make a REST API call to get a list of communities, using Axios from node. Here's the code that is getting called;

async (req, res, next) => {
      try {
      console.log(`going to call ${req.user.sfInstanceUrl}/services/data/v49.0/connect/communities`);
      console.log(`actual bearer token: ${req.user.sfAccessToken}`);
      const communitiesResponse = await axios.get(`${req.user.sfInstanceUrl}/services/data/v49.0/connect/communities`, {
        'Authorization': `Bearer ${req.user.sfAccessToken}`
      });
      if (communitiesResponse.status === 200) {
        console.log(`communities response is: ${JSON.stringify(communitiesResponse)}`);
        res.status(200).send(communitiesResponse['communities']);
        } 
      } catch (error) {
        console.log(error);
            }

now, this endpoint is getting called and I can see that both the instance URL and the access token are set correctly with the logging statements. Axios is returning a 401 Unauthorized error however (boo!)..

When I take this out of the logs and run a cURL statement, the data is returned and i'm authorised. Eg, the cURL statement that works;

curl --location --request GET 'https://adams-scv-demo.my.salesforce.com/services/data/v49.0/connect/communities' --header 'Authorization: Bearer {myValidAuthToken}'

i'm confused, sad and afraid at this... wondering what security setting I may have missed in my org to allow this site to call into the API? (the login stuff obviously works though)

0
0

From the axios docs looks like you are missing the headers object in the request.

Change it to below

const communitiesResponse = await axios.get(`${req.user.sfInstanceUrl}/services/data/v49.0/connect/communities`,
      { 
        headers: {
          'Authorization': `Bearer ${req.user.sfAccessToken}`
       }
});
1
  • doh! thanks for being my rubber duck :) that should've been really obvious... – adam.sellers Oct 29 '20 at 5:49

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.