使用Google网上应用商店API时,如何避免跨网站错误?

时间:2011-12-21 08:22:57

标签: javascript chrome-web-store

我正在尝试按照Google网上商店API文档中提供的这个简单示例进行操作。 不幸的是,他们最小的PHP示例包含了许多我不需要的东西(并且没有设置)。

所以我正在考虑将基本内容硬编码到HTML文件中,只是为了与谷歌服务器进行通信工作:

我的代码如下所示:

<html>
  <head>
  </head>
  <body>
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script type="text/javascript">
      purchase = function() { console.log("Old purchase function"); }
        generatedJwt = "..."; // I use a real JWT here....
        google.load('payments', '1.0', {
          'packages': ['sandbox_config'],
          callback: function()
          {
            //Success handler
            var successHandler = function(purchaseAction){
              if (window.console != undefined) {
                console.log("Purchase completed successfully.",purchaseAction);
           }
          }

          //Failure handler
          var failureHandler = function(purchaseActionError)
          {
             if (window.console != undefined)
             {
            console.log("Purchase did not complete.", purchaseActionError);
         }
           }

           var purchase = function()
           {
          goog.payments.inapp.buy({
                 'jwt'     : generatedJwt,
                 'success' : successHandler,
                 'failure' : failureHandler
              });
           }
        }
      });
   </script>
   <button class="buy-button" id="buybutton1" name="buy" type="button" onClick="purchase()">
      Buy
   </button>
 </body>

当我点击购买按钮时,我会从谷歌弹出一条错误消息,并在控制台中显示以下错误:

Unsafe JavaScript attempt to access frame with URL   
http://localhost/google_iap/buy_page.html 
from frame with URL
https://sandbox.google.com/checkout/inapp/static/gwt/payments.html?viewportScreenCenterX=-857.5&viewportScreenCenterY=611#id=I1_1324454042050&parent=http%3A%2F%2Flocalhost&rpctoken=166967692&_methods=onPurchaseActionStatus%2CgetJwt%2C_ready%2C_close%2C_open%2C_resizeMe.
Domains, protocols and ports must match.

我需要做些什么来使这个简单的例子有效?

附录:

我从谷歌找到了一个更好的例子:

https://sandbox.google.com/checkout/customer/gadget/inapp/demo.html

但是,将源保存到我的计算机并在本地访问它(如http://localhost/google_iap/google_demo.html)会产生相同的跨域错误。

1 个答案:

答案 0 :(得分:0)

似乎是google sandbox was broken。这现在再次工作,所以示例似乎正常运行。但是,有关跨站点访问的警告仍然存在。