来自Google API Oauth的客户端不是有效来源

Fil*_*ano 29 javascript oauth-2.0 google-oauth

我从Google API Oauth收到此错误:

idpiframe_initialization_failed",详细信息:"不是客户端的有效来源:http://127.0.0....为项目的客户端ID列出此来源

我正在尝试从此本地路径发送请求:

http://127.0.0.1:8887/

我已将此URL添加到Authorized JavaScript origin部分: 在此输入图像描述

这是我的代码:

<!-- 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>
Run Code Online (Sandbox Code Playgroud)

我怎样才能解决这个问题?

And*_*ash 73

我和你的问题非常相似.我尝试添加多个白名单端口localhost,但没有任何工作.结束删除凭据并重新设置.对于我的设置,谷歌一定是个bug.

  • 大声笑这个工作......这是过去许多谷歌API问题的解决方案......绝对需要记住这一个.(wtf google ???) (7认同)
  • 是的,2019年仍未修复 (4认同)
  • 这不好笑.这真是难过;这真是伤心. (3认同)
  • 可悲的是,我为这个答案获得了如此多的赞成(并继续得到他们),因为似乎没有任何改变 (2认同)
  • 这个答案让我想起了我的 Windows 技术支持日子。“你试过重启吗?” 修复了 95% 的问题。点个赞吧! (2认同)

Kir*_*kiy 29

重置Chrome缓存可以为我解决。长按“重新加载”按钮,然后长按“清空缓存和硬重新加载”。

在此处输入图片说明

  • 一种解决方案是**以隐身模式进行检查**。有时硬重新加载不适合。 (7认同)
  • 我已经在检查控制台日志中是否有错误,并且刚刚检查了开发控制台的“网络”选项卡中的“禁用缓存”选项,瞧,错误消失了。 (3认同)

use*_*932 6

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

  • 你是怎样做的? (2认同)

小智 6

我也有同样的问题;但这对我有用:

  1. 打开console.developers
  2. 打开项目名称
  3. 单击凭证
  4. 在“名称”下,单击“ Web 客户端 1
  5. 在“ URLs ”下,添加“ http://localhost:3000


vte*_*ier 5

我在网络上的许多地方阅读过,人们经常使用这些地方来重做凭据的创建,以使其生效。
所以我做到了,我为同一项目创建了一个新凭据,并使用了新的用户ID,它运行得非常完美。

Nb:我也使用localhost而不是127.0.0.1,IP无效。


use*_*944 5

我摆弄了大约 10 分钟,然后当我在浏览器中尝试http://localhost/(而不是 127.0.0.1)时它终于起作用了

在每个你可以做白名单的地方添加了网址:https : //console.developers.google.com/apis/credentials/