我想从Android手机添加一个用户日历事件和firebase触发器,但我有一个问题与谷歌Oauth2 .

在android方面我使用firebase google auth与日历范围:“https://www.googleapis.com/auth/calendar

在firebase方面我有这个代码:

import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
import * as google from 'googleapis';

admin.initializeApp();
export const addEvent = functions.database
    .ref('users/{user}/calendarEvents/{eventId}')
    .onCreate((snapshot, context) => {
        const data = snapshot.val()
        const calendar = google.google.calendar('v3');
        const authClient = getOauthClient(data.token);
        return new Promise((resolve, reject) => {
            calendar.events.insert({
                auth: authClient,
                calendarId: 'primary',
                resource: getCalendarEvent(data),
            }, function (err, event) {
                if (err) {
                    console.error(err);
                    reject.apply(err);
                }
                else {
                    resolve.apply(event.data);
                }
            });
        }).then(() => snapshot.ref.remove());
    });

function getCalendarEvent(data) {
    const start = new Date()
    start.setTime(data.date)
    const end = new Date()
    end.setTime(data.date + (1000 * 60 * 60))
    return {
        'id': String(data.id),
        'summary': data.summary,
        'description': data.description,
        'start': {
            'dateTime': start.toISOString()
        },
        'end': {
            'dateTime': end.toISOString()
        },
        'attendees': [
            { 'email': data.email }
        ],
        'reminders': {
            'useDefault': false,
            'overrides': [
                { 'method': 'email', 'minutes': 24 * 60 },
                { 'method': 'popup', 'minutes': 10 }
            ]
        }
    }
}

function getOauthClient(accessToken) {
    const oauth = new google.google.auth.OAuth2();
    oauth.setCredentials({ access_token: accessToken });
    return oauth;
}

在firebase日志中,我收到此错误:

No access, refresh token or API key is set.
at OAuth2Client.<anonymous> (/user_code/node_modules/googleapis/node_modules/google-auth-library/build/src/auth/oauth2client.js:336:35)
at step (/user_code/node_modules/googleapis/node_modules/google-auth-library/build/src/auth/oauth2client.js:57:23)
at Object.next (/user_code/node_modules/googleapis/node_modules/google-auth-library/build/src/auth/oauth2client.js:38:53)
at /user_code/node_modules/googleapis/node_modules/google-auth-library/build/src/auth/oauth2client.js:32:71
at __awaiter (/user_code/node_modules/googleapis/node_modules/google-auth-library/build/src/auth/oauth2client.js:28:12)
at OAuth2Client.getRequestMetadataAsync (/user_code/node_modules/googleapis/node_modules/google-auth-library/build/src/auth/oauth2client.js:329:16)
at OAuth2Client.<anonymous> (/user_code/node_modules/googleapis/node_modules/google-auth-library/build/src/auth/oauth2client.js:441:51)
at step (/user_code/node_modules/googleapis/node_modules/google-auth-library/build/src/auth/oauth2client.js:57:23)
at Object.next (/user_code/node_modules/googleapis/node_modules/google-auth-library/build/src/auth/oauth2client.js:38:53)
at /user_code/node_modules/googleapis/node_modules/google-auth-library/build/src/auth/oauth2client.js:32:71

我应该从客户端发送什么令牌?我应该如何使用谷歌日历API?

在android上我使用它来获取令牌:

token = GoogleSignIn.getLastSignedInAccount(application).serverAuthCode

token = GoogleSignIn.getLastSignedInAccount(application).idToken

token = Tasks.await(FirebaseAuth.currentUser.getIdToken(true)) }.token

我也尝试过Is it possible to add events to a user's google calendar (via my server) after some event in my application?的功能请求

但我收到了

The API returned an error: Error: No refresh token is set.