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

Google People API无法获取emailAddresses:桌面应用权限问题排查求助

解决Google认证获取邮箱时的403权限问题

Hey there! Let's break down why you're hitting that frustrating 403 error when trying to fetch a Google user's email address.

问题根源拆解

The error message gives a clear clue: even though you're targeting emailAddresses, the people/me endpoint requires the profile scope as a base permission—even if you don't actually need any profile data. Either your current scope list is missing this, or your cached auth token (from earlier authorizations) doesn't include it (tokens don't auto-update when you add new scopes).

具体解决步骤

1. 更新Scope列表,包含必要权限

Modify your scope definition to include both the base profile scope and the email read scope for the People API:

scopes := []string{
    people.UserEmailsReadScope,
    "profile",
}
config, err := google.ConfigFromJSON(b, scopes...)

This ensures you have the foundational access to people/me plus explicit permission to read email addresses.

2. 清除缓存的认证令牌

If you've run the app before with a different set of scopes, your local token file (usually token.json in quickstart examples) won't include the new profile scope. Delete this file, then re-run your app—you'll be prompted to re-authorize, and the new token will include all required permissions.

3. 替代方案:用OAuth2 Userinfo端点(更简洁的邮箱获取方式)

If you only need the user's email and don't require other People API data, you can skip the People API entirely. Use the userinfo.email scope and call the OAuth2 userinfo endpoint directly:

// Define the scope for email access
scopes := []string{"https://www.googleapis.com/auth/userinfo.email"}
config, err := google.ConfigFromJSON(b, scopes...)
if err != nil {
    log.Fatalf("Unable to parse client secret file to config: %v", err)
}
client := getClient(config)

// Fetch user info from the OAuth2 endpoint
resp, err := client.Get("https://www.googleapis.com/oauth2/v2/userinfo")
if err != nil {
    log.Fatalf("Failed to retrieve user info: %v", err)
}
defer resp.Body.Close()

// Parse the response to extract the email
var userInfo map[string]interface{}
if err := json.NewDecoder(resp.Body).Decode(&userInfo); err != nil {
    log.Fatalf("Failed to parse user info: %v", err)
}
email := userInfo["email"].(string)
fmt.Printf("User email: %s\n", email)

This approach is often more straightforward for basic email retrieval needs.

额外检查项

  • Double-check that the People API is enabled in your Google Cloud Console (though your error points to scope issues, not API access).
  • Confirm all scopes you're using are listed on your OAuth consent screen in the Cloud Console (under "OAuth consent screen" > "Scopes for Google APIs").

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.29 21:02:46