Google日历:错误:超出了未经身份验证的使用的每日限制

时间:2020-03-16 05:12:21

标签: google-calendar-api google-oauth

我正在尝试遵循Google文档(https://developers.google.com/calendar/quickstart/nodejs)中提供的google calendar api nodejs示例来访问我的主要google日历事件,但出现以下错误:“超出了未经身份验证的使用的每日限制。继续使用需要注册。”

我知道我正确地通过了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必须批准对这些敏感范围的访问。但是,同意屏幕不会显示“未经验证的应用程序”。该应用程序正在开发而非生产中。

Google的文档说:“ DailyLimitExceeded错误表示您的项目已达到礼节性的API限制。在开发人员控制台的“配额”页面上,我的使用记录为0。我已启用Google Calendar 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,然后作为参数传递给此google cloud函数。

    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) => {...

有什么建议吗?谢谢。

1 个答案:

答案 0 :(得分:1)

我发现了问题。

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

应为:

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