ADC で Drive API を叩く
#GoogleCloud
Application Default Credential
GCP metadata server から spreadsheet などの API 叩けるか
Spreadsheet や Calendar などの Google Workspace の API は scope を渡せば叩ける
しかし Drive API は叩けない
code:example.js
const { google } = require("googleapis");
(async function () {
const auth = new google.auth.GoogleAuth({
projectId: "pokutuna-playground",
scopes: [
"https://www.googleapis.com/auth/cloud-platform",
"https://www.googleapis.com/auth/drive.readonly",
"https://www.googleapis.com/auth/spreadsheets.readonly",
],
});
# sheet の内容は読める
const sheets = google.sheets({ version: "v4", auth });
await sheets.spreadsheets.values.get({
spreadsheetId: "...",
range: "...",
}).then(res => console.log(res.data));
# drive は読めない
# 設定したプロジェクトで Drive API を有効にしていても Access Not Configured エラーになる
const drive = google.drive({ version: "v3", auth });
await drive.files.list({
q: { pageSize: 3 },
}).then(res => console.log(res.data))
})();
Access Not Configured. Drive API has not been used in project 764086051850 before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/drive.googleapis.com/overview?project=764086051850 then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.',
projectId 渡しても常に 764086051850
これはみんな共通
LocalでApplication Default Credentialsを利用している時にG Suite APIやFirebase APIを実行するとgoogleapi: Error 403: Request had insufficient authentication scopes. になる · Issue #124 · gcpug/nouhau
QuotaProject 渡しても Drive API については無視される
gcloud auth application-default login --client-id-file を使うといいという話
! どうやら Drive API を叩くにはユーザが作った OAuth2 Client App が必須ってことかなあ
client-id-file とは
OAuth2 Client ID ファイル
https://console.cloud.google.com/apis/credentials から作るやつ
ちなみに デスクトップアプリ で作らないといけない
ERROR: (gcloud.auth.application-default.login) Only client IDs of type 'installed' are allowed, but encountered type 'web'
これで作ったファイルをダウンロードして gcloud auth application-default login に渡す
https://gyazo.com/6eb27ceaa1bccd93928b86733f8c7a55
code:command.sh
gcloud auth application-default login --scopes=https://www.googleapis.com/auth/cloud-platform,https://www.googleapis.com/auth/drive.readonly,https://www.googleapis.com/auth/spreadsheets.readonly --client-id-file=...
叩けるようになる
client-id-file 渡さないなら quotaProject が 764086051850 に固定される
なぜ?
これは Metadata Server で使えないんじゃないのか
確かに?
普通に Drive のファイルリスト取得できた via Cloud Functions
JWT (idToken) からはいける
Metadata Server & サービスアカウントキーもこっち
OAuth2 として認証する時はだめ
ローカルの ADC はこっち (client-id-file 渡さない場合)
code:function.js
const { google } = require("googleapis");
exports"drive-api" = async (req, res) => {
const auth = new google.auth.GoogleAuth({
projectId: "pokutuna-dev",
scopes: [
"https://www.googleapis.com/auth/cloud-platform",
"https://www.googleapis.com/auth/drive.readonly",
],
});
const drive = google.drive({ version: "v3", auth });
await drive.files
.list({
q: { pageSize: 3 },
})
.then((res) => console.log(JSON.stringify(res.data)))
.catch(console.error);
res.send("ok");
};
別の話題
gcloud auth login --enable-gdrive-access というのもある