我使用谷歌服务帐户时出错:无效的jwt

问题描述 投票:0回答:1

我有一个基本上用于将文档发送到通过 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 值。 },

node.js google-api google-drive-api service-accounts google-api-nodejs-client
1个回答
0
投票

这是使用服务帐户的正确方法。

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 });
© www.soinside.com 2019 - 2024. All rights reserved.