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

调用Google Admin SDK Users List API遇401登录要求错误咨询

Fixing the "Login Required" 401 Error for Google Admin Directory API

Hey there! I totally get why this is confusing—API keys work for some Google APIs, but the Admin Directory API isn't one of them. Let's break down what's going on and how to fix it:

Why your API key isn't working

The Google Admin Directory API deals with private, domain-specific user data, which means it requires user or service account authentication (not just an API key). API keys only grant access to public, unauthenticated APIs (like public Maps data). Since you're trying to access your G Suite domain's user list, Google is rejecting the API key because it can't verify you're authorized to view that private data.

Two correct ways to authenticate for this API

You have two main options depending on your use case:

Option 1: OAuth 2.0 for manual/user-initiated calls

If you're testing the API manually or building an app that acts on behalf of a specific admin user:

  • Go to your Google Cloud Console, create an OAuth 2.0 Client ID (under "Credentials").
  • When generating an access token, request the scope https://www.googleapis.com/auth/admin.directory.user.readonly (or admin.directory.user if you need write access).
  • When calling the API, replace the key URL parameter with an Authorization header that includes your access token. Here's a curl example:
    curl -H "Authorization: Bearer YOUR_GENERATED_ACCESS_TOKEN" "https://www.googleapis.com/admin/directory/v1/users?domain=xxx-xxx.com"
    

Option 2: Service Account with Domain-Wide Delegation (for automated/server-side calls)

If you're building a script or service that needs to run automatically without user input:

  1. In Google Cloud Console, create a Service Account and download its JSON key file.
  2. Head to your G Suite Admin Console (admin.google.com), go to Security > API Controls > Domain-wide delegation, and add your service account's client ID. Grant it the same scope(s) as above (e.g., https://www.googleapis.com/auth/admin.directory.user.readonly).
  3. Use the service account to generate an access token that impersonates your G Suite domain admin. Then include that token in the Authorization header when making your API call (same format as the curl example above).

Quick reminder

Don't forget to make sure the Admin Directory API is enabled in your Google Cloud Console (under "APIs & Services > Library")—it's easy to overlook that step!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 08:29:46