2016-04-15 45 views
0

我試圖將curl命令從instagram api轉換爲jquery(ajax/get)。乾淨的方法來將CURL轉換爲jQuery/Javascript [Instagram]

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

從DOC:

「爲了使這種交換,你只需要一些app標識參數一起發佈此代碼, 」

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]錯誤。

+1

您在瀏覽器控制檯有有趣的錯誤**似乎並不爲我工作** – madalinivascu

+0

獲得跨源塊請求[CORS]錯誤^^ – teddym

回答

1

首先,爲了發送查詢字符串中的值,請使用發送給jQuery.ajax的選項對象的.data屬性。如果您想以編程方式創建網址,請不要忘記對通過encodeURIComponent發送的值進行網址編碼。

0

嘗試使用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); 
    } 
});