向谷歌日历 API 发出 ajax 请求

Make an ajax request to google calendar api

本文关键字:ajax 请求 发出 API 谷歌 日历      更新时间:2023-09-26

我正在尝试向谷歌日历API发出ajax请求,但出了点问题。在这里,我有非常简单的代码,但我不明白为什么它不起作用。

var mykey = 'my server key'; // I am using here server key(which I created in credentials in console.developers.google.com) Maybe I am using wrong version?! I've tried browser key, but it didn't help me.
var calendarid = 'my client_id'; // here I am using VALID clientId, which I created in my project in developer console. Below this code I attached screen.
$.ajax({
    type: 'GET',
    url: encodeURI('https://www.googleapis.com/calendar/v3/calendars/' + calendarid+ '/events?key=' + mykey),
    dataType: 'json',
    success: function (response) {
        //console.log('Nice!')
    },
    error: function (response) {
        //console.log('Bad!')
    }
});

我在浏览器控制台中总是有错误 404(正常)。任何帮助将不胜感激!

这是我的客户端 ID 的屏幕截图。

您需要使用 OAuth 显示登录提示并获取访问令牌,然后才能联系日历 API。最简单的方法是使用 Google API JavaScript 库。请参阅此示例。