Django jwt token error. "Given token not valid for any token type, token_not_valid"

Viewed 4033

I am using Django Rest Framework for authentication and Axios in the frontend. When I pass the token to an axios.get method directly it fetches the API endpoint (getUserInfo) correctly, but when I use a variable (string) and pass it dynamically it fails and it gives the error described above.

First case: (succeeds)

const config = {
    headers: { Authorization: 'Bearer <token string>' },
  };

  await axios.get(URLS.FETCH_USER, config)

Second case: (fails)

const config = {
    headers: { Authorization:  `Bearer ${accessToken}` },
  };

  await axios.get(URLS.FETCH_USER, config).etc

Note that the token is fresh and taken directly from a postman login request.

1 Answers

The issue was that the token was a string inside extra two quotation marks "". That was the reason. I solved it by parsing the string as follows:

const config = {
    headers: { Authorization:  `Bearer ${JSON.parse(accessToken)}` },
  };
Related