How to Convert Facebook Graph api request to Axios in React Native?

Viewed 1583

Trying to convert a Facebook Graph api request to axios format.

const infoRequest = new GraphRequest(
            '/me',
            {
                accessToken: tokenData.accessToken,
                parameters: {
                    fields: {
                        string: 'id,email,name,picture'
                    }
                }
            },
            responseInfoCallback
        );

Tried the following

const result = await axios.get('https://graph.facebook.com/me',
            {
                accessToken: tokenData.accessToken,
                parameters: {
                    fields: {
                        string: 'id,email,name,picture'
                    }
                }
            }
                );

Resulted to error

 Facebook GraphAPI error: Error: Request failed with status code 400
1 Answers

This is happening because facebook needs request url in format: https://graph.facebook.com/v9.0/me?fields=id,name&access_token=your_access_token_will_go_here

But axios turns url in format:
https://graph.facebook.com/v9.0/me?fields=id%2Cname&access_token=your_access_token_goes_here

Using axios right way with facebook Graph API

const accessToken = your_access_token;
const { data } = await axios({
   url:
     "https://graph.facebook.com/v9.0/me?fields=id,name&access_token=" + accessToken,
   method: "get",
});

Or put in try catch block

try {
  const { data } = await axios({
    url:
      "ttps://graph.facebook.com/v9.0/me?fields=id,name&access_token=" + accessToken,
    method: "get",
  });
  console.log("response from fb request", data);
} catch (err) {
   console.log(
      "There is an error occured while making request to FB Graph API: " + err
   );
}
   
Related