来自Google API Oauth的客户端不是有效来源 [英] Not a valid origin for the client from Google API Oauth

查看:148
本文介绍了来自Google API Oauth的客户端不是有效来源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从Google API Oauth收到此错误:


idpiframe_initialization_failed,详情:不是客户的有效来源:



这是我的代码:

 <! - 文件顶部index.html  - > 
< html itemscope itemtype =http://schema.org/Article>
< head>
<! - BEGIN先决条件 - >
< script src =http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js>
< / script>
< script src =https://apis.google.com/js/client:platform.js?onload=startasync defer>
< / script>
<! - END先决条件 - >

<! - 继续< head>部分 - >
< script>
function start(){
gapi.load('auth2',function(){
auth2 = gapi.auth2.init({
client_id:'MY CLIENT ID.apps .googleusercontent.com',
//除了'个人资料'和'电子邮件'之外还要请求的范围
//范围:'https://www.google.com/m8/feeds/'
});
});
}
< / script>




< / head>
< body>


< button id =signinButton>使用Google登录< / button>
< script>
$('#signinButton')。click(function(){
//在步骤6中定义的signInCallback。
auth2.grantOfflineAccess()。然后(signInCallback);
} );
< / script>



<! - 文件index.html中BODY元素的最后一部分 - >
< script>
函数signInCallback(authResult){
if(authResult ['code']){

//现在隐藏用户被授权的登录按钮,例如:
$('#signinButton')。attr('style','display:none');

//将代码发送到服务器
$ .ajax({
类型:'POST',
url:'http:// http://example.com/ storeauthcode',
//在每个AJAX请求中始终包含一个`X-Requested-With`标头,
//以防止CSRF攻击。
标题:{
'X -Requested-With':'XMLHttpRequest'
},
contentType:'application / octet-stream; charset = utf-8',
success:function(result){
//处理或验证服务器响应。
},
processData:false,
data:authResult ['code']
});
} else {
//出现错误。
}
}
< / script>
<! - ... - >
< / body>
< / html>

我该如何解决这个问题?

解决方案

如果对您来说完全相同,请尝试添加 http:// localhost:8887 改为授权的JavaScript源代码。在某些时候我自己有这个错误,这就解决了。知道您必须将此URL用于您的请求以及事件,尽管它转换为 http://127.0.0.1:8887 /


I'm receiving this error from Google API Oauth:

idpiframe_initialization_failed", details: "Not a valid origin for the client: http://127.0.0.…itelist this origin for your project's client ID

I'm trying to send a request from this local path:

http://127.0.0.1:8887/

And I already added this URL to the Authorized JavaScript origins section:

This is my code:

<!-- The top of file index.html -->
<html itemscope itemtype="http://schema.org/Article">
<head>
  <!-- BEGIN Pre-requisites -->
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js">
  </script>
  <script src="https://apis.google.com/js/client:platform.js?onload=start" async defer>
  </script>
  <!-- END Pre-requisites -->

<!-- Continuing the <head> section -->
  <script>
    function start() {
      gapi.load('auth2', function() {
        auth2 = gapi.auth2.init({
          client_id: 'MY CLIENT ID.apps.googleusercontent.com',
          // Scopes to request in addition to 'profile' and 'email'
          //scope: 'https://www.google.com/m8/feeds/'
        });
      });
    }
  </script>




</head>
<body>


<button id="signinButton">Sign in with Google</button>
<script>
  $('#signinButton').click(function() {
    // signInCallback defined in step 6.
    auth2.grantOfflineAccess().then(signInCallback);
  });
</script>



<!-- Last part of BODY element in file index.html -->
<script>
function signInCallback(authResult) {
  if (authResult['code']) {

    // Hide the sign-in button now that the user is authorized, for example:
    $('#signinButton').attr('style', 'display: none');

    // Send the code to the server
    $.ajax({
      type: 'POST',
      url: 'http://example.com/storeauthcode',
      // Always include an `X-Requested-With` header in every AJAX request,
      // to protect against CSRF attacks.
      headers: {
        'X-Requested-With': 'XMLHttpRequest'
      },
      contentType: 'application/octet-stream; charset=utf-8',
      success: function(result) {
        // Handle or verify the server response.
      },
      processData: false,
      data: authResult['code']
    });
  } else {
    // There was an error.
  }
}
</script>
  <!-- ... -->
</body>
</html>

How can I fix this?

解决方案

If it's all the same to you, try adding http://localhost:8887 to your authorized JavaScript origins instead. Had that error myself at some point and this fixed it. Know that you will have to use this URL for your request as well event though it translates to http://127.0.0.1:8887/.

这篇关于来自Google API Oauth的客户端不是有效来源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆