【问题标题】:Clean way to convert CURL to Jquery/Javascript [Instagram]将 CURL 转换为 Jquery/Javascript [Instagram] 的简洁方法
【发布时间】:2016-04-15 09:48:03
【问题描述】:

我正在尝试将 curl 命令从 instagram api 转换为 jquery(ajax/get)。

 curl -F 'client_id=CLIENT_ID' \
    -F 'client_secret=CLIENT_SECRET' \
    -F 'grant_type=authorization_code' \
    -F 'redirect_uri=AUTHORIZATION_REDIRECT_URI' \
    -F 'code=CODE' \
    https://api.instagram.com/oauth/access_token

来自文档:

“为了进行这种交换,您只需发布此代码, 以及一些应用识别参数”

var clientId = "my client id";
var clientSecret = "my client secret";
var redirectURI = "http://localhost:3000/instagram";
var myCode = "my instagram code";
var uri = 'https://api.instagram.com/oauth/access_token?client_id=' + clientId + '&client_secret=' + clientSecret + '&grant_type=authorization_code&redirect_uri=' + redirectURI + '&code=' + myCode;
var url = encodeURIComponent(uri);

$.ajax({
   type: "POST",
   dataType: "json",
   url: '  https://api.instagram.com/oauth/access_token?client_id=' + clientId + '&client_secret=' + clientSecret + '&grant_type=authorization_code&redirect_uri=' + redirectURI + '&code=' + myCode,
   success: function (result) {
      console.log(result);
   }
});

似乎对我不起作用...

获取跨域块请求 [CORS] 错误。

【问题讨论】:

  • 您在浏览器控制台中遇到的有趣错误似乎对我不起作用
  • 获取跨域块请求 [CORS] 错误 ^^

标签: jquery ajax curl instagram


【解决方案1】:

首先,为了在查询字符串中发送值,请使用您发送到jQuery.ajax 的选项对象的.data 属性。如果您想以编程方式创建 url,请不要忘记对您通过 encodeURIComponent 发送的值进行 URL 编码。

【讨论】:

    【解决方案2】:

    尝试使用 jsonp。比如:

    function myinstagramfunction (json_object)
    {
        // I fetch here the json object that contains the info of all the pictures returned
        console.log("OK");
    }
    $.ajax({
       type: "GET",
       dataType: "jsonp",
       url: 'https://api.instagram.com/v1/tags/YOUR_HASHTAG/media/recent?access_token=YOUR_ACCESS_TOKEN_HERE&callback=myinstagramfunction',
       success: function (result) {
          console.log(result);
       }
    });
    

    【讨论】:

      猜你喜欢
      • 2011-01-20
      • 1970-01-01
      • 2012-08-25
      • 2010-12-06
      • 1970-01-01
      • 2011-06-04
      • 1970-01-01
      • 1970-01-01
      • 2022-01-08
      相关资源
      最近更新 更多