如何使用firebase数据库触发器添加Google日历活动

时间:2018-05-11 11:04:15

标签: android typescript firebase google-calendar-api google-cloud-functions

我想使用firebase触发器从Android手机添加用户日历事件,但我在使用Google Oauth2时遇到问题。

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

在firebase方面我有这个代码:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul id='myid'>
  <li id='1'>skuID 1</li>
  <li id='2'>skuID 2</li>
  <li id='3'>skuID 3</li>
  <li id='4'>skuID 4</li>
  <li id='5'>skuID 5</li>
</ul>

在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;
}

我应该从客户端发送什么令牌? 我应该如何使用google calendar api?

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

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

并且

token = GoogleSignIn.getLastSignedInAccount(application).serverAuthCode

并且

token = GoogleSignIn.getLastSignedInAccount(application).idToken

我还尝试了Is it possible to add events to a user's google calendar (via my server) after some event in my application?

的功能请求

但我收到了

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

0 个答案:

没有答案