Instagram api获得JavaScript Access-Control-Allow-Origin

问题描述 投票:1回答:1

我已经做了我能想象到的一切,以使这个代码工作。我正在使用React。我有一个我在下面发布的端点。我尝试使用fetch然后superagent(也许这很重要),我可以不工作。我得到了这两个错误。仅供参考,我有意“x'd”了userId和访问令牌。

OPTIONS https://api.instagram.com/v1/users/xxxxxxxxx/media/recent/?access_token=xxxxxxxx 405 ()

Failed to load https://api.instagram.com/v1/users/xxxxxxx/media/recent/?access_token=xxxxxx: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access. The response had HTTP status code 405.

我尝试了this postthis postand实现了他们的解决方案但是一个返回的对象没有我需要的数据而另一个只是没有用。但是,当我在浏览器中粘贴完整的URL时,我能够恢复JSON对象。请帮忙。

      export const getIGPost = () => dispatch => {
    // fetch('https://api.instagram.com/v1/users/xxxxxxxx/media/recent/?
     access_token=xxxxxxx',{
    //     method:'GET',
    //     dataType:'jsonp',
    //     crossDomain:true
    // })
    request
        .get(`https://api.instagram.com/v1/users/xxxxxxxxx/media/recent/?access_token=xxxxxxxxx`)
        .set('Access-Control-Allow-Origin', 'http://localhost:3000')
        .then(res => {
            if(res.ok) {
                console.log(res.body)
                // dispatch(addIGPost(res.body))
            }
        })
}
javascript instagram-api options preflight
1个回答
-1
投票

对此的简单解决方案是使用CorsAnywhere

© www.soinside.com 2019 - 2024. All rights reserved.