首页 > 解决方案 > Google 日历:错误:已超出未经验证使用的每日限制

问题描述

我正在尝试按照谷歌文档(https://developers.google.com/calendar/quickstart/nodejs)中提供的谷歌日历 api nodejs 示例来访问我的主要谷歌日历活动,但我收到此错误:“每日限制已超过未经身份验证的使用。继续使用需要注册。”

我知道我正确地通过了 OAuth 流程,因为此处列出了我的具有日历访问权限的项目:https ://myaccount.google.com/permissions?pli=1 。

这是我的范围:

const SCOPESAUTH = [
'https://www.googleapis.com/auth/calendar',
'https://www.googleapis.com/auth/calendar.readonly',
'https://www.googleapis.com/auth/calendar.events'

];

在 OAuth 同意屏幕选项卡的开发人员控制台中,我没有在此部分设置范围:“Google API 的范围”。当我尝试设置它们时,它说谷歌必须批准对这些敏感范围的访问。但是,同意屏幕不会说:“未验证的应用程序”。该应用程序正在开发而不是生产中。

Google 的文档说:“dailyLimitExceeded 错误表明您的项目已达到礼貌 API 限制。” 在开发人员控制台的配额页面上,我的使用量记录为 0。我已启用 Google 日历 API。

以下是 OAuth 云功能:

    const functionsOauthClient = new OAuth2Client(CONFIG_CLIENT_ID, CONFIG_CLIENT_SECRET,
    FUNCTIONS_REDIRECT);

// visit the URL for this Function to request tokens
exports.authgoogleapi = functions.https.onRequest((req, res) => {
    // req.query.id === device id of the user
    res.set('Cache-Control', 'private, max-age=0, s-maxage=0');
    res.redirect(functionsOauthClient.generateAuthUrl({
        access_type: 'offline',
        scope: SCOPESAUTH,
        prompt: 'consent',
        state: req.query.id
    }));
});


// redirect URI
exports.oauthcallback = functions.https.onRequest(async (req, res) => {
    res.set('Cache-Control', 'private, max-age=0, s-maxage=0');
    const code = req.query.code;
    try {
        const {tokens} = await functionsOauthClient.getToken(code);
        // store the tokens for retrieval by client app that is then sent to the getEvents cloud cloud unction
        admin.database().ref(`Devices/${req.query.state}/api_tokens`).set(tokens);
        return res.status(200).send('App successfully configured with new Credentials.');
    } catch (error) {
        return res.status(400).send(error);
    }
});

对 oauthcallback 的调用返回状态 200。

下面是引发错误的代码。请注意,req.body.token 是在 OAuth 流程期间生成的,保存到 Firebase,然后作为参数传递给这个谷歌云函数。

    exports.getEvents = functions.https.onRequest((req, res) => {
      ...

      const authClient = new OAuth2Client(CONFIG_CLIENT_ID, CONFIG_CLIENT_SECRET, FUNCTIONS_REDIRECT);
      authClient.setCredentials(req.body.token);
      const calendar = google.calendar({version: 'v3', authClient});
      return calendar.events.list({
          calendarId: 'primary',
          timeMin: (new Date()).toISOString(),
          maxResults: 10,
          singleEvents: true,
          orderBy: 'startTime',
      }, (err, result) => {...

有什么建议么?谢谢。

标签: google-calendar-apigoogle-oauth

解决方案


我发现了问题。

const calendar = google.calendar({version: 'v3', authClient})

应该:

const calendar = google.calendar({version: 'v3', auth: authClient})

推荐阅读