问题描述
我正在编写一个Node.js应用程序以从Google云端硬盘下载文件。我正在使用googleapis npm软件包,并且正在使用服务帐户进行身份验证。我假设身份验证已正确完成,因为我可以在Google Cloud Console的“驱动器API指标”标签中看到我所有应用程序的请求。大量下载后,请求开始失败,并显示以下响应:
{
"error": {
"errors": [
{
"domain": "usageLimits","reason": "dailyLimitExceededUnreg","message": "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup.","extendedHelp": "https://code.google.com/apis/console"
}
],"code": 403,"message": "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup."
}
}
我在Google Cloud Console中将这些请求视为403响应。如果我等了几个小时再不修改代码就重试,下载将重新开始。
如果我的身份验证不正确,为什么这些请求会显示在我的帐户下的Google Cloud Console中?如果我实际上在正确地进行身份验证,为什么错误消息会说这是一个“未经身份验证”的请求,并且有可能增加配额吗? (在Google Cloud Console中为Google Drive API列出的所有配额中,几乎都没有超出配额)
我在googleapis文档中使用的是身份验证代码的改编版本:
import { google } from 'googleapis'
import * as dotenv from 'dotenv'
dotenv.config()
const drive = google.drive('v3')
main()
async function main() {
await initDriveAuth()
await download('<file id>')
}
// This authenticates future requests using process.env.GOOGLE_APPLICATION_CREDENTIALS,// which contains the JSON key of a service account:
async function initDriveAuth() {
const auth = new google.auth.GoogleAuth({
scopes: 'https://www.googleapis.com/auth/drive'
})
google.options({
auth: await auth.getClient()
})
}
// This creates a download stream for the file with fileID
async function download(fileID: string) {
return await drive.files.get({
fileId: fileID,alt: 'media'
},{
responseType: 'stream'
})
}
我的.env文件如下:
GOOGLE_APPLICATION_CREDENTIALS=./config/service_account_key.json
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)