How to make api call to Instagram with axios without CORS error

谁都会走 提交于 2019-12-11 07:06:43

问题


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)
    })
}

Trying to make an call to Instagram's API but I keep getting this error:

I'm using React.

Any help would be great.


回答1:


I don't think Instagram supports cross origin requests. They do support jsonp though.

Access-Control-Allow-Origin with instagram api

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




回答2:


You can use a middleman to make your request server-side. There are several to choose from when I search 'no cors npm', but here is an example: https://www.npmjs.com/package/cors



来源:https://stackoverflow.com/questions/46615606/how-to-make-api-call-to-instagram-with-axios-without-cors-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!