You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Google Sign-In本地虚拟主机配置报错求助(localhost可正常使用)

排查本地虚拟主机Google Sign-In配置失败的原因

Hey there, let's break down why your Google Sign-In works on http://localhost but not with your custom local virtual host like http://opencart.loc—I've run into this exact issue before, so here's what to check step by step:

1. OAuth 2.0 凭据的授权域名未正确配置

This is the most common culprit. Google's OAuth system requires that every domain making sign-in requests is explicitly whitelisted in your Google Cloud project's credentials—localhost is a special case allowed by default, but custom local domains aren't. Here's how to fix it:

  • Log into your Google Cloud Console and navigate to your project.
  • Go to APIs & Services > Credentials.
  • Find your existing OAuth 2.0 Client ID (the one you're using for Sign-In) and click the edit button.
  • Under Authorized JavaScript origins, add http://opencart.loc (include the port number if your virtual host uses one, like http://opencart.loc:8080).
  • Under Authorized redirect URIs, add the full callback URL your app uses for Google Sign-In (e.g., http://opencart.loc/google-signin-callback—make sure this matches exactly what's in your code).
  • Save the changes and wait 5-10 minutes for Google's servers to propagate the updates (yes, sometimes it takes a bit!).

2. Local DNS/Hosts File Issues

Even if your virtual host works in the browser, double-check that your system's hosts file is correctly mapping opencart.loc to 127.0.0.1:

  • Open your hosts file (on Windows it's C:\Windows\System32\drivers\etc\hosts; on macOS/Linux it's /etc/hosts).
  • Verify there's a line like: 127.0.0.1 opencart.loc
  • If it's missing, add it, save the file, and flush your DNS cache (on Windows run ipconfig /flushdns, on macOS sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder).

Google Sign-In relies on cross-domain cookies to manage authentication sessions, and custom local domains might trigger stricter browser cookie policies than localhost:

  • Open your browser's DevTools (F12), go to the Application tab, and check the Cookies section for opencart.loc and Google-related domains (like accounts.google.com). If cookies are being blocked, that's a problem.
  • Try testing in incognito/private mode to bypass any cached cookie restrictions.
  • Ensure your web server isn't setting overly strict SameSite cookie attributes that block cross-domain cookies.

4. Misconfigured Sign-In Initialization Code

Double-check your Google Sign-In initialization code to make sure it's not restricting domains accidentally:

  • Avoid setting the hosted_domain parameter unless you're using a G Suite domain (this will block non-G Suite accounts and custom local domains).
  • Make sure the client_id in your code matches exactly the one from your Google Cloud credentials (copy-paste it to avoid typos!).

Here's a clean example of the initialization code to reference:

gapi.load('auth2', function() {
  const auth2 = gapi.auth2.init({
    client_id: 'YOUR_CLIENT_ID.apps.googleusercontent.com',
    scope: 'profile email'
  });

  // Attach sign-in to your button
  auth2.attachClickHandler(document.getElementById('google-signin-button'), {},
    function(googleUser) {
      // Success handler
      console.log('Signed in as: ' + googleUser.getBasicProfile().getName());
    },
    function(error) {
      // Error handler (check this for more details!)
      console.error('Sign-in error:', error);
    }
  );
});

5. Browser Cache or Stored OAuth Tokens

Sometimes old cached tokens or session data can cause conflicts. Try:

  • Clearing your browser's cache and cookies specifically for opencart.loc and Google domains.
  • Testing with a different browser to rule out browser-specific issues.

Start with the first step (OAuth credentials configuration)—that's almost always the fix for this exact scenario. If you're still stuck, check the browser's console for specific error messages (under the Console tab in DevTools) and share them—they'll give more clues about what's going wrong!

内容的提问来源于stack exchange,提问作者Parnit Das

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 03:53:56