Mohammad
Mohammad

Reputation: 621

how can access to an API which is have a token with Axios in vuejs?

I have an API that has a JSON file, I gonna get information from that with Axios but it has Token and I don't know how can use it anybody can help me? here it's API

‫‪https://api.nytimes.com/svc/movies/v2/reviews/picks.json‬‬

I try this but its didn't work and gave me error 401 and this GET https://api.nytimes.com/svc/movies/v2/reviews/picks.json%E2%80%AC%E2%80%AC 401 (Unauthorized)

<script>
import axios from "axios";
export default {
  data() {
    return {};
  },
  methods: {
    async getDataFromApi() {
      const res=await axios.get("https://api.nytimes.com/svc/movies/v2/reviews/picks.json‬‬");
      console.log(res.data)
    },
  },
};
</script>

please, someone helping me

Upvotes: 0

Views: 534

Answers (1)

Ari Shojaei
Ari Shojaei

Reputation: 360

401 Error means you not authenticated. you must add a token in axios authorization header and send it with your HTTP request.

const res = await axios.get('https://api.nytimes.com/svc/movies/v2/reviews/picks.json', {
  headers: {
    authorization: 'my secret token'
  }
});

This is the hard code way, for more efficiency, you must define interceptors for axios to send the token with every HTTP request. see this: https://gist.github.com/srph/38f67a10e991b6cb2d83

Upvotes: 1

Related Questions