我有一个基本上用于将文档发送到通过 Google 登录的驱动器的应用程序,但是在发送文档时我想使用 Google 服务帐户,但收到几个令牌错误
//first import service account key file
const serviceAccountKey = require('../../../serviceAccount.json')
//after instances client account service
const authService = new google.auth.GoogleAuth({
credentials: serviceAccountKey,
scopes: ['https://www.googleapis.com/auth/drive']
})
const googleAccountService = google.drive({
version: 'v3',
auth: authService
})
//and then
try {
const response = googleAccountService.files.list({
q: 'mimeType = "application/vnd.google-apps.folder"'
})
if (response.data) {
return response.data
}
} catch (error) {
console.log(error);
}
//但它捕获了错误
数据:{ 错误:'无效的授权', error_description: 'JWT 无效:令牌必须是短期令牌(60 分钟)且处于合理的时间范围内。检查 JWT 声明中的 iat 和 exp 值。 },
这是使用服务帐户的正确方法。
const { google } = require("googleapis");
const credentialFilename = "credentials.json";
const scopes = ["https://www.googleapis.com/auth/drive"];
const auth = new google.auth.GoogleAuth({keyFile: credentialFilename, scopes: scopes});
const drive = google.drive({ version: "v3", auth });