I want to add an user calendar event from android phone with firebase trigger, but I have a problem with google Oauth2.
On android side I am using firebase + google auth with calendar scope: "https://www.googleapis.com/auth/calendar"
On firebase side I have this code:
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;
}
In firebase logs I received this error:
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
What token should I send from client? How I should use it with google calendar api?
On android I used this to obtain token:
token = GoogleSignIn.getLastSignedInAccount(application).serverAuthCode
And
token = GoogleSignIn.getLastSignedInAccount(application).idToken
And
token = Tasks.await(FirebaseAuth.currentUser.getIdToken(true)) }.token
I also tried function request from Is it possible to add events to a user's google calendar (via my server) after some event in my application?
But I received
The API returned an error: Error: No refresh token is set.