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

新手上路,请多包涵

我从 Google API Oauth 收到此错误:

idpiframe_initialization_failed”, details: “不是有效的客户端来源: http://127.0.0 .. itelist this origin for your project’s client ID

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

http://127.0.0.1:8887/

我已经将此 URL 添加到授权的 JavaScript 来源部分: 在此处输入图像描述

这是我的代码:

 <!-- 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>

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

原文由 Filipe Ferminiano 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 601
2 个回答

如果这对您来说都是一样的,请尝试将 http://localhost:8887 添加到您授权的 JavaScript 来源。我自己在某个时候犯了这个错误,这解决了它。请注意,尽管它会转换为 http://127.0.0.1:8887/ ,但您必须将此 URL 用于您的请求和事件。

原文由 user7191932 发布,翻译遵循 CC BY-SA 3.0 许可协议

重置 Chrome 缓存为我解决了这个问题。长按重新加载按钮,然后清空缓存和硬重新加载。

注意:确保您的 Chrome 开发工具面板已打开,否则长按将不起作用。

在此处输入图像描述

原文由 Kirill Kovalevskiy 发布,翻译遵循 CC BY-SA 4.0 许可协议

推荐问题