创建Instagram API地理订阅的CORS问题

CORS Issue with Creating an Instagram API Geography Subscription

本文关键字:CORS 问题 Instagram API 创建      更新时间:2023-09-26

我正在尝试向Instagram地理订阅API端点发出POST请求。

我得到了和其他人一样的错误:

    OPTIONS https://api.instagram.com/v1/subscriptions/ 
    XMLHttpRequest cannot load https://api.instagram.com/v1/subscriptions/. 
    No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:1337' is therefore not allowed access. The response had HTTP status code 405.

我在前端使用Node/Express和Angular。

这是我在Angular中使用$http服务发出的请求:

    $http.post('https://api.instagram.com/v1/subscriptions/', 
    {   
        client_id: 'XXX',
        client_secret: 'XXX',
        object: 'geography',
        aspect: 'media',
        lat: 35.657872,
        lng: 139.70232,
        radius: 1000,
        callback_url: 'http://localhost:1337/auth/instagram/callback'
    })
    .success(function (data, status) {
        console.log("success", data);
    })
    .error(function (data, status) {
        console.log("error", data);
    });

通过其他SO帖子的帮助,我设置了这样的标题:http://repl.it/BDYf

我的应用程序的整体结构取自这个MEAN堆栈生成器和repl所需的in文件。如果你对这些内容有任何困惑- https://github.com/FullstackAcademy/fsg/tree/master/generated/server

我非常感谢一些帮助-我有一个CORS问题之前,我用$http跑来跑去。jsonp,但是正如你所知道的,你不能对POST请求使用jsonp,这是API要求的。

谢谢!

我偶然发现了这个答案,尽管它有意义

[https://stackoverflow.com/a/22850652/1271376] [1]

不可能直接从客户端调用POST,你必须设置一个代理服务器,使Instagram API调用POST和DELETE,你的客户端应用程序可以调用代理服务器

您需要在$httpProvider上启用跨域请求。例如:

module.config(['$httpProvider', function ($httpProvider) {
    // configure the HTTP provider to enable cross-origin requests
    $httpProvider.defaults.useXDomain = true;
}]);