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

chrome.identity.getProfileUserInfo无法准确获取登录用户邮箱的问题

How to Get the Active Signed-In User's Email (Not Chrome Profile Email)

Ah, I totally get where you're coming from—this is a super common pitfall with Chrome's identity APIs. The chrome.identity.getProfileUserInfo() method is tied to the local Chrome profile (the one you select when launching the browser), not the active Google account the user is signed into for services like Gmail or Drive. That’s why it spits out an email even if the user isn’t actively logged into a Google account—because the profile itself was created with that email linked.

Here’s how you can reliably fetch the email of the currently signed-in Google user (not just the profile’s associated email):

1. Update Your Extension’s Permissions

First, make sure your manifest.json has the right permissions and OAuth2 scopes. You’ll need the identity permission, plus a scope to access the user’s email:

{
  "manifest_version": 3,
  "name": "Your Extension Name",
  "permissions": ["identity"],
  "oauth2": {
    "client_id": "YOUR_OAUTH_CLIENT_ID.apps.googleusercontent.com",
    "scopes": ["https://www.googleapis.com/auth/userinfo.email"]
  }
}

Note: You’ll need to create an OAuth client ID in the Google Cloud Console for your extension—this is mandatory to authenticate requests to Google’s APIs.

2. Use chrome.identity.getAuthToken to Verify Active Sign-In

Instead of relying on getProfileUserInfo, use chrome.identity.getAuthToken to confirm there’s an active signed-in user. This method will:

  • Return a valid token if the user is signed into a Google account
  • Prompt the user to sign in if they aren’t (when interactive: true is set)
  • Fail if the user cancels the sign-in prompt

Here’s a code snippet for your popup:

async function getActiveUserEmail() {
  try {
    // Request an auth token—triggers a sign-in prompt if needed
    const { token } = await chrome.identity.getAuthToken({ interactive: true });
    
    // Fetch the user's email using Google's UserInfo API
    const response = await fetch('https://www.googleapis.com/oauth2/v3/userinfo', {
      headers: { Authorization: `Bearer ${token}` }
    });
    
    const userInfo = await response.json();
    return userInfo.email; // This is the active signed-in user's email
  } catch (error) {
    // Handle cases where no active session exists or user cancels sign-in
    console.error('No active signed-in user:', error);
    return null;
  }
}

// Run when the popup loads
document.addEventListener('DOMContentLoaded', async () => {
  const email = await getActiveUserEmail();
  if (email) {
    console.log('Active user email:', email);
    // Display the email in your popup UI
  } else {
    console.log('No active user signed in');
    // Show a message asking the user to sign in
  }
});

3. Key Benefits of This Approach

  • Guarantees an active session: The token is only issued if the user is actively signed into a Google account (not just a Chrome profile).
  • Works safely on shared devices: If someone else uses the same Chrome profile but signs out of their Google account, getAuthToken will prompt for sign-in instead of returning the old profile email.
  • Secure: The token is short-lived and scoped only to access the user’s email, so you’re not requesting unnecessary permissions.

Quick Notes

  • If you just want to check for an existing session (without prompting sign-in), set interactive: false in getAuthToken. This will fail silently if no active session exists.
  • Always handle errors gracefully—if the user cancels the sign-in prompt, your extension should respect that and avoid actions requiring the email.

内容的提问来源于stack exchange,提问作者Richard Bernstein

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 03:31:44