如何使用Cordova将事件添加到Google日历

时间:2016-08-03 12:55:47

标签: javascript cordova google-calendar-api

问题

我无法使用带有Cordova的Google Calendar API添加活动,因为他们不允许file://来源。

我的工作

因此,我尝试向网址POST https://www.googleapis.com/calendar/v3/calendars/primary/events?key={YOUR_API_KEY}link)发出请求。但我必须登录...寻找答案我遇到了这个链接:

所以,现在我可以登录。或者至少获得一个访问令牌。但是我仍然需要插入一个事件,所以我再次尝试使用access_token参数的前一个链接(POST https://www.googleapis.com/calendar/v3/calendars/primary/events?key={YOUR_API_KEY})(即使使用refresh_token)。而且我总是有一个错误,说我必须被记录......我不知道自己要做什么......

代码

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();
    }
};

小结

我必须向Google日历添加一个活动,但我无法使用Google Calendar API,因为他们不允许file://来源。我设法登录Google,但我无法向https://www.googleapis.com/calendar/v3/calendars/primary/events?key={YOUR_API_KEY}添加向HTTP请求发送的事件。 任何人都知道如何使用Cordova轻松地将活动添加到Google日历?

修改

如果我更改标题,则会收到其他错误:"Invalid credentials"。现在,我更改了insertEvent功能,我正在执行此操作:

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

0 个答案:

没有答案