0

I have this code:

const getToken = async () => {
  return Axios.post(
    `https://id.twitch.tv/oauth2/token?client_id=${process.env.TWITCH_ID}&client_secret=${process.env.TWITCH_SECRET}&grant_type=client_credentials`
  ).then((res) => res.data["access_token"]);
};

const getId = async (accessToken, session) => {
  const response = await Axios.get(
    `https://api.twitch.tv/helix/users?login=${session.user.name}`,
    {
      Authorization: `Bearer ${accessToken}`,
      "Client-Id": process.env.TWITCH_ID,
    }
  );
  return response.data.id;
};

export async function getServerSideProps(context) {
  const session = await getSession(context);

  if (session) {
    const accessToken = await getToken();
    console.log(accessToken);
    const id = await getId(accessToken, session);
    console.log(id);
  }
  return {
    props: {}, // will be passed to the page component as props
  };
}

This is Next.js function that will do this on every request. I am using Next.js, next-auth for authentication. Everything should work fine, even on line console.log(accessToken) I get the expected output. But in function getId it says 401 - unauthorized. I am calling Twitch api.

juliomalves
  • 42,130
  • 20
  • 150
  • 146
Skole 13
  • 182
  • 3
  • 12

0 Answers0