google-calendar-api - 设置了错误 : No access, 刷新 token 或 API key

标签 google-calendar-api

我正在尝试在 Node 中制作一个应用程序来访问我的谷歌日历,所以我按照 https://developers.google.com/calendar/quickstart/nodejs 中的步骤进行了操作。但我收到了 Error: Error: No access, refresh token or API key is set. .
是的,我已经创建了凭据。
是的,我已经下载了 json,重命名为 client_secret.json 并添加到应用程序文件夹中。
这是代码:

    const fs = require('fs'); 
    const readline = require('readline'); 
    const {google} = require('googleapis'); 
    const OAuth2Client = google.auth.OAuth2; 
    const SCOPES = ['https://www.googleapis.com/auth/calendar.readonly']; 
    const TOKEN_PATH = './client_secret.json';
        
    try {   
        const content = fs.readFileSync('client_secret.json');   
        authorize(JSON.parse(content), listEvents); 
    } catch (err) {   
        return console.log('Error loading client secret file:', err); 
    }
        
    function authorize(credentials, callback) {   
        const {client_secret, client_id, redirect_uris} = credentials.installed;   
        let token = {};   
        const oAuth2Client = new OAuth2Client(client_id, client_secret, redirect_uris[0]);
        
          // Check if we have previously stored a token.   
        try {
            token = fs.readFileSync(TOKEN_PATH);   
        } catch (err) {
            return getAccessToken(oAuth2Client, callback);   
        }   
        oAuth2Client.setCredentials(JSON.parse(token));   
        callback(oAuth2Client); 
    }
        
    function getAccessToken(oAuth2Client, callback) {   
        const authUrl = oAuth2Client.generateAuthUrl({
            access_type: 'offline',
            scope: SCOPES,   
        });   
        console.log('Authorize this app by visiting this url:', authUrl);   
        const rl = readline.createInterface({
            input: process.stdin,
            output: process.stdout,   
        });   
        rl.question('Enter the code from that page here: ', (code) => {
            rl.close();
            oAuth2Client.getToken(code, (err, token) => {
                if (err) return callback(err);
                oAuth2Client.setCredentials(token);
                // Store the token to disk for later program executions
                try {
                    fs.writeFileSync(TOKEN_PATH, JSON.stringify(token));
                    console.log('Token stored to', TOKEN_PATH);
                } catch (err) {
                    console.error(err);
                }
            callback(oAuth2Client);
            });   
        }); 
    }
        
    function listEvents(auth) {   
        const calendar = google.calendar({version: 'v3', auth});   
        calendar.events.list({
            calendarId: 'primary',
            timeMin: (new Date()).toISOString(),
            maxResults: 10,
            singleEvents: true,
            orderBy: 'startTime',   }, (err, {data}) => {
                if (err) return console.log('The API returned an error: ' + err);
                const events = data.items;
                if (events.length) {
                    console.log('Upcoming 10 events:');
                    events.map((event, i) => {
                    const start = event.start.dateTime || event.start.date;
                    console.log(`${start} - ${event.summary}`);
                });
            } else {
              console.log('No upcoming events found.');
            }   
    }); 
}
有任何想法吗?

最佳答案

您能再次确认以下几点吗?

  • const TOKEN_PATH = './client_secret.json';的文件和 const content = fs.readFileSync('client_secret.json');是相同的。
  • 请从const TOKEN_PATH = './client_secret.json';修改至 const TOKEN_PATH = './credentials.json'; ,然后再次运行。
  • 至此,client_secret.json您下载的可能已经被覆盖。所以也请确认这一点。
  • 当以上修改完成后出现错误时,请确认googleapis的版本。因为据报道,googleapis 带有 v25.0.0 - v30.0.0。某些 API 有一些错误。
  • 如果您认为该错误是一个bug,请将googleapis 版本修改为v24.0.0。错误可能会被删除。

  • 引用 :
  • How do I update my google sheet in v4?
  • Create a gmail filter with Gmail API nodejs, Error: Filter doesn't have any criteria
  • Insufficient Permission when trying to create a folder on Google Drive via API(v3)
  • Youtube Data API V3 - Error fetching video with google.youtube.videos.list()
  • Google drive API - Cannot read property 'OAuth2' of undefined
  • How to run a Google App Script using Google API Service Library (Node.js)

  • 如果这些要点对您的情况没有用,我很抱歉。

    关于google-calendar-api - 设置了错误 : No access, 刷新 token 或 API key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50281159/

    相关文章:

    php - 从 Google 服务帐户 : 403 编辑 Google 日历事件

    java - 如何使用服务帐号正确获取用户的日历事件?

    google-apps-script - 为什么谷歌应用程序脚本不发送邀请

    java - 如何从 intellij idea 项目生成可执行文件?

    php - 使用 Google Calendar API 插入带有环聊 session 的事件

    php - 谷歌日历以两种方式与 php 同步

    python - 'google-api-python-client' distribution 在pyinstaller编译的running EXE上找不到

    parsing - 您认为 Google 日历中的 "Quick Add"功能如何运作?

    javascript - 如何在其他人的谷歌日历上创建事件

    google-apps-script - 如何构建指向 Google 日历事件的链接?