Problem:
I can't add an event using the Google Calendar API with Cordova because they don't allow file:// origin.
What I do:
So, I try to do a request to the URL POST https://www.googleapis.com/calendar/v3/calendars/primary/events?key={YOUR_API_KEY} (link). BUT I have to be logged in... Looking for an answer I came across this link:
So, right now I can be logged in. Or at least get an access token. But I still need to insert an event, so I tried again with the previous link (POST https://www.googleapis.com/calendar/v3/calendars/primary/events?key={YOUR_API_KEY}) with the access_token parameter (even with refresh_token). And I always have an error saying I have to be logged... I don't know what I have to do...
Code:
var CLIENT_ID = '<CLIEND_ID>';
var CLIENT_SECRET = '<CLIENT_SECRET>';
var CALENDAR_API_KEY = '<CALENDAR_API_KEY>';
var SCOPES = ["https://www.googleapis.com/auth/calendar"];
var evento = {
titulo: "Nice event",
detalles: "Details",
location: "Madrid",
email: "prueba@test.com"
};
googleapi.authorize({
client_id: CLIENT_ID,
client_secret: CLIENT_SECRET,
redirect_uri: 'http://localhost',
scope: SCOPES.join(' ')
}).done(function (data) {
alert('Access Token: ' + data.access_token);
evento.access_token = data.access_token;
addEvent(evento);
}).fail(function (data) {
alert(data.error);
});
function addEvent(evento) {
var event = {
calendarId: "primary",
'summary': evento.titulo,
'location': evento.location,
'description': evento.detalles,
'start': {
'dateTime': '2016-08-28T09:00:00-07:00',
'timeZone': ' Europe/Madrid'
},
'end': {
'dateTime': '2016-08-28T17:00:00-07:00',
'timeZone': ' Europe/Madrid'
},
'attendees': [
{'email': evento.email}
],
'reminders': {
'useDefault': false,
'overrides': [
{'method': 'email', 'minutes': 24 * 60},
{'method': 'popup', 'minutes': 60}
]
}
};
googleapi.insertEvent(event).done(function (data) {
alert(JSON.stringify(data));
}).fail(function (data) {
alert(JSON.stringify(data));
alert(JSON.stringify(data.error));
});
}
// authorize using InAppBrowser
var googleapi = {
authorize: function (options) {
var deferred = $.Deferred();
//Build the OAuth consent page URL
var authUrl = 'https://accounts.google.com/o/oauth2/auth?' + $.param({
client_id: options.client_id,
redirect_uri: options.redirect_uri,
response_type: 'code',
scope: options.scope
});
//Open the OAuth consent page in the InAppBrowser
var authWindow = window.open(authUrl, '_blank', 'location=no,toolbar=no');
$(authWindow).on('loadstart', function (e) {
var url = e.originalEvent.url;
var code = /\?code=(.+)$/.exec(url);
var error = /\?error=(.+)$/.exec(url);
if (code || error) {
//Always close the browser when match is found
authWindow.close();
}
if (code) {
//Exchange the authorization code for an access token
$.post('https://accounts.google.com/o/oauth2/token', {
code: code[1],
client_id: options.client_id,
client_secret: options.client_secret,
redirect_uri: options.redirect_uri,
grant_type: 'authorization_code'
}).done(function (data) {
deferred.resolve(data);
}).fail(function (response) {
deferred.reject(response.responseJSON);
});
} else if (error) {
//The user denied access to the app
deferred.reject({
error: error[1]
});
}
});
return deferred.promise();
},
insertEvent: function (options) {
var deferred = $.Deferred();
$.post('https://www.googleapis.com/calendar/v3/calendars/' + options.calendarId + '/events?key=' + CALENDAR_API_KEY + '&alt=json', {
client_id: CLIENT_ID,
client_secret: CLIENT_SECRET,
access_token: options.access_token,
scope: SCOPES.join(' '),
summary: options.titulo,
location: options.location,
description: options.detalles,
start: options.start,
end: options.end,
attendees: options.attendees,
reminders: options.reminders
}).done(function (data) {
alert("done");
deferred.resolve(data);
}).fail(function (response) {
alert("fail");
deferred.reject(response.responseJSON);
});
return deferred.promise();
}
};
Recap:
I have to add an event to the Google Calendar and I can't use the Google Calendar API because they don't allow a file:// origin. I managed to log on Google but I'm not able to add an event doing a HTTP request to https://www.googleapis.com/calendar/v3/calendars/primary/events?key={YOUR_API_KEY}. Anyone knows how to easily add an event to Google Calendar with Cordova?
EDIT:
If I change the headers I get a different error: "Invalid credentials". Right now, I change the insertEvent function and I'm doing this:
insertEvent: function (options) {
var deferred = $.Deferred();
$.ajax({
type: "POST",
url: 'https://www.googleapis.com/calendar/v3/calendars/' + options.calendarId + '/events?key=' + CALENDAR_API_KEY + '&alt=json',
data: {
summary: options.titulo,
location: options.location,
description: options.detalles,
start: options.start,
end: options.end,
attendees: options.attendees,
reminders: options.reminders
},
headers: {
"authorization": "Bearer " + options.access_token
}
}).done(function (data) {
alert("done");
deferred.resolve(data);
}).fail(function (response) {
alert("fail");
deferred.reject(response.responseJSON);
});