2012-07-11 91 views
0

我正在嘗試對Shopify進行API OAUTH2調用進行身份驗證。當我去我的應用程序,第一個屏幕出現。我按下安裝,然後將我重定向到我開始的位置。問題是,當我重定向時,我似乎無法在我的jQuery ajax函數中捕獲它,因此我無法獲得我需要從OAUTH創建永久令牌的臨時令牌。第二張圖片顯示了我按下安裝後發生的情況。我的代碼到目前爲止在下面。在運行ajax調用之後,沒有任何console.log()函數被調用。使用jQuery OAUTH獲取臨時訪問令牌

爲我的應用我的應用程序URL設置爲

http://localhost 

和我的應用程序從

http://localhost/shippingcalculator. 

運行我測試了外部REST客戶端程序的調用,我設法順利拿到我的訪問令牌,所以它不是我的憑據問題。

200OK and then the 302 Redirect

Runs fine in a REST client tester

<!DOCTYPE html> 

<script type="text/javascript"> 
function getParameterByName(name) { 
    name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]"); 
    var regexS = "[\\?&]" + name + "=([^&#]*)"; 
    var regex = new RegExp(regexS); 
    console.log(window.location.search); 
    var results = regex.exec(window.location.search); 
    if (results == null) return ""; 
    else return decodeURIComponent(results[1].replace(/\+/g, " ")); 
} 

function getTemporaryToken(token) { 
    jso_configure({ 
     "shopify": { 
      client_id: "67d7af798dd0a42e731af51ffa", //This is your API Key for your App 
      //redirect_uri: "", OPTIONAL - The URL that the merchant will be sent to once authentication is complete. Must be the same host as the Return URL specified in the application settings 
      authorization: "https://emard-ratke3131.myshopify.com/admin/oauth/authorize", 
      //In your case the authorization would be https://SHOP_NAME.myshopify.com/admin/oauth/authorize 
      scope: ["read_products"], //Set the scope to whatever you want to access from the API. Full list here: http://api.shopify.com/authentication.html 
     } 
    }); 
    $.oajax({ 
     url: "https://emard-ratke3131.myshopify.com/admin/oauth/authorize", 
     jso_provider: "shopify", 
     jso_scopes: ["read_products"], 
     jso_allowia: true, 
     success: function(data) { 
      //Use data and exchange temporary token for permanent one 
      if (jqXHR.status === 200) { 
       console.log("200"); 
      } 
      if (jqXHR.status === 302) { 
       console.log("300"); 
      } 
      console.log("Response (shopify):"); 
      console.log(data); 
     }, 
     error: function(e) { 
      console.log("Fail GET Request"); 
      console.log(e); 
     }, 
     complete: function(xmlHttp) { 
      // xmlHttp is a XMLHttpRquest object 
      console.log(xmlHttp.status); 
     } 
    }); 
    console.log("Code: " + getParameterByName("code")); 
} 
} 
$(document).ready(function() { 
    getTemporaryToken(); 
}); 
</script> 
</head> 
<body> 
Hello World! 
</body> 
</html> 

回答

3

這是一個非常糟糕的主意,做這一切的客戶端,你的API密鑰將是清楚的人看和用。最重要的是,你將在哪裏存儲令牌?在所有瀏覽器中都有跨站點腳本限制,阻止JavaScript調用除js加載的站點之外的站點。

您應該確實進行身份驗證並從服務器進行所有API調用。

+0

好吧,我已經寫了一個node.js服務器來協助我的OAuth調用 – Marc 2012-07-11 22:46:38

+0

在cookie或html5本地存儲器中存儲令牌不是壞主意 – sepehr 2016-10-26 07:18:49

相關問題