【问题标题】:CORS Issue with Creating an Instagram API Geography Subscription创建 Instagram API 地理订阅的 CORS 问题
【发布时间】:2015-11-18 08:02:40
【问题描述】:

我正在尝试向 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.it 所需的文件来自这个 - https://github.com/FullstackAcademy/fsg/tree/master/generated/server

非常感谢一些帮助 - 在使用 $http.jsonp 之前,我遇到了一个 CORS 问题,但正如您所知,您不能将 jsonp 用于 POST 请求,这是 API 所要求的.

谢谢!

【问题讨论】:

    标签: javascript node.js express cors instagram-api


    【解决方案1】:

    我遇到了这个答案,虽然它是有道理的

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

    无法直接从客户端进行 POST 调用,您必须设置一个代理服务器,让 Instagram API 调用 POST 和 DELETE,并且您的客户端应用程序可以调用代理服务器。

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-03
        • 2023-03-03
        • 1970-01-01
        • 1970-01-01
        • 2016-12-02
        相关资源
        最近更新 更多