使用JS在gplus-api中给出了这个错误

时间:2015-01-22 15:25:39

标签: javascript google-api google-plus google-api-js-client

您好我刚刚开始使用javascript学习gplus-api signin但是我遇到了一个错误,说明在'DOMWindow'上执行'postMessage'失败:提供的目标来源('file://')与收件人窗口的原点不匹配('null')。  这是我提到这个网站的代码,但没有帮助我! https://developers.google.com/+/web/signin/javascript-flow

我的代码

<!DOCTYPE HTML>
 <html>
   <head></head>
 <body>
    <script src="https://apis.google.com/js/client:platform.js" async defer></script>

    <meta name="google-signin-clientid" content="MY_CLIENT_ID.apps.googleusercontent.com">
    <meta name="google-signin-scope" content="https://www.googleapis.com/auth/plus.login" />
    <meta name="google-signin-requestvisibleactions" content="http://schema.org/AddAction" />
    <meta name="google-signin-cookiepolicy" content="single_host_origin" />

  <script type="text/javascript">
    (function(){
      var po = document.createElement('script');
      po.type = "text/javascript";
      po.async = true;
      po.src = "https://apis.google.com/js/client:plusone.js";
      var s = document.getElementByTagName('script');
      s.parentNode.insertBefore(po,s);
  });

 function render() {

  // Additional params including the callback, the rest of the params will
  // come from the page-level configuration.
   var additionalParams = {
     'callback': signinCallback
  };

  // Attach a click listener to a button to trigger the flow.
   var signinButton = document.getElementById('signinButton');
   signinButton.addEventListener('click', function() {
       gapi.auth.signIn(additionalParams); // Will use page level configuration
    });
 }

  function signinCallback(authResult) {
      if (authResult['status']['signed_in']) {
   // Update the app to reflect a signed in user
   // Hide the sign-in button now that the user is authorized, for   example:
      document.getElementById('signinButton').setAttribute('style',             'display: none');
    } else {
    // Update the app to reflect a signed out user
    // Possible error values:
   //   "user_signed_out" - User is signed-out
   //   "access_denied" - User denied access to your app
   //   "immediate_failed" - Could not automatically log in the user
   console.log('Sign-in state: ' + authResult['error']);
   }
  }
 </script>
  <button id="signinButton">Sign in with Google</button>
  </body>
  </html>

我的js来源是http://localhost:8080,当我尝试加载此网址时,它也会给我404错误 请帮我解决这个错误!

1 个答案:

答案 0 :(得分:0)

使用Google+登录时,您必须运行网络服务器(甚至在本地)。一个简单的示例是在您的端口设置为8080的情况下运行Python:

python -m SimpleHTTPServer 8080

您还可以配置/运行Apache或其他服务器以进行本地测试。