Unable to get accessToken via Axios in React

Trying to get the access token from my first axios call and place it into the 2nd. My component is like so.

When i log accessToken to the console its an empty array. When i console.log(response.data) I can see the response and my access_token

const Route = (props) => {
  const [accessToken, setAccessToken] = useState([]);
  const [route, setRoute] = useState([]);
  const [athlete, setAthlete] = useState([]);

  // get a refreshed token from strava
  useEffect(() => {
    axios({
      method: 'POST',
      url: 'https://www.strava.com/oauth/token',
      params: {
        client_id: process.env.GATSBY_STRAVA_CLIENT_ID,
        client_secret: process.env.GATSBY_STRAVA_CLIENT_SECRET,
        grant_type: 'refresh_token',
        refresh_token: process.env.GATSBY_STRAVA_REFRESH_TOKEN,
      },
    })
      .then((response) => {
        console.log(response.data);
        setAccessToken(response.data.access_token);

        console.log(accessToken);

        return axios({
          method: 'GET',
          url: `https://www.strava.com/api/v3/routes/${props.data.contentfulRoutes.slug}`,
          headers: {
            Authorization: `Bearer ${accessToken}`,
          },
        });
      })
      .then((response) => {
        console.log(response.data);
        setRoute(response.data);
        setAthlete(response.data.athlete);
      })
      .catch((err) => {
        console.log(err);
      });
  }, []);


Read more here: https://stackoverflow.com/questions/67938798/unable-to-get-accesstoken-via-axios-in-react

Content Attribution

This content was originally published by mrpbennett at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: