2012-07-11 92 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

相关问题