如何使用没有CORS错误的axios对Instagram进行api调用

时间:2017-10-07 00:41:32

标签: reactjs cors frontend instagram-api axios

componentDidMount() {
let token = this.getAccessToken('access_token');

let config = {
  headers: {
    'Access-Control-Allow-Origin': '*',
    'Content-Type': 'application/json',
    Accept: 'application/json'
  }
}

if (token) {
  axios.get('https://api.instagram.com/v1/users/self/?access_token=' + token, config)
    .then(res => {
      console.log(res)
    })
    .catch(err => {
      console.log(err)
    })
}

尝试拨打Instagram的API,但我一直收到此错误:

enter image description here

我正在使用React。

任何帮助都会很棒。

2 个答案:

答案 0 :(得分:2)

我不认为Instagram支持跨源请求。他们确实支持jsonp。

Access-Control-Allow-Origin with instagram api

instagram jquery ajax type="GET, can't get around CORS

答案 1 :(得分:0)

您可以使用中间人在服务器端发出请求。当我搜索没有cors npm'时,有几个可供选择,但这里有一个例子:implement Dijkstra's algorithm with a binary heap