![](/img/trans.png)
[英]Authenticating a Google Cloud Function as a service account on other Google APIs
[英]Authorizing with Google APIs using a service account
我正在嘗試使用 nodeJS 和服務帳戶文件從 google-api 訪問一些 google 組。 我找不到像這樣工作的代碼示例。 我整理了一些似乎有效的代碼,但我不斷收到錯誤 400 - 來自 API 的錯誤請求,但沒有任何線索表明出了什么問題。
我使用的是正確的課程嗎? 我可以在這里使用服務帳戶嗎? 我是不是缺少示波器之類的?
我的代碼:
import { Auth, google } from "googleapis";
const main = async () => {
const auth = new Auth.GoogleAuth({
keyFile: "/path-to-service-account-file.json",
scopes: "https://www.googleapis.com/auth/admin.directory.group.readonly",
});
const client = await auth.getClient();
// Obtain a new drive client, making sure you pass along the auth client
const admin = google.admin({ version: 'directory_v1', auth: client });
const groups = await admin.groups.list();
console.log(groups.data.groups);
}
main().then(() => process.exit(0)).catch(err => {
console.error(err);
process.exit(1);
});
該文件(我認為)是一個標准的服務帳戶文件:
{
"type": "service_account",
"project_id": "groupmembers-*******",
"private_key_id": "*********",
"private_key": ...,
"client_email": "test-admin-nodejs@groupmembers-****.iam.gserviceaccount.com",
"client_id": "*****",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/test-admin-nodejs%40groupmembers-******.iam.gserviceaccount.com"
}
當我運行這段代碼時,我得到以下響應:
status: 400,
statusText: 'Bad Request',
request: {
responseURL: 'https://admin.googleapis.com/admin/directory/v1/groups'
}
我感興趣的是它為我生成的不記名令牌。 該錯誤還打印出請求信息:
config: {
url: 'https://admin.googleapis.com/admin/directory/v1/groups',
method: 'GET',
userAgentDirectives: [ [Object] ],
paramsSerializer: [Function (anonymous)],
headers: {
'x-goog-api-client': 'gdcl/5.1.0 gl-node/17.4.0 auth/7.14.1',
'Accept-Encoding': 'gzip',
'User-Agent': 'google-api-nodejs-client/5.1.0 (gzip)',
Authorization: 'Bearer ya29.c.b0AX***********************************************jzxc........................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................',
Accept: 'application/json'
},
我不知道這是否正常或相關,但我從未見過末尾帶有一堆點的不記名令牌。
400 錯誤不是授權/認證問題,而是客戶端請求問題。 根據官方文檔,api調用需要一些查詢參數。 其中之一是customer
查詢參數:
客戶的 Google Workspace 帳號的唯一 ID。 如果是多域帳戶,要獲取客戶的所有組,請填寫此字段而不是域。 作為賬戶管理員,您還可以使用 my_customer 別名來表示您賬戶的 customerId。 customerId 也作為用戶的一部分返回
根據經驗,暗示您必須使用此值。 盡管文檔應該更好,並且應該明確說明這是必需的。 因此,總而言之,您的請求應如下所示:
const groups = await admin.groups.list({
customer: "my_customer"
});
console.log(groups.data.groups);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.