Google 캘린더 API를 사용하여 내가 소유 한 공개 캘린더의 최신 이벤트 목록을 가져 오려고합니다. 내 고객이 캘린더를 편집 할 수 있고 정보가 웹 사이트에 표시됩니다. 여기 내 코드는 (내가 Google의 예에서 편집하는) 지금까지입니다 :Google 캘린더 API : 사용자가 로그인하지 않고 공개 캘린더의 일정을 가져 오는 방법은 무엇입니까?
<html>
<head>
<script type="text/javascript">
// Your Client ID can be retrieved from your project in the Google
// Developer Console, https://console.developers.google.com
var CLIENT_ID = '<MY-CLIENT-ID>';
var SCOPES = ["https://www.googleapis.com/auth/calendar.readonly"];
/**
* Check if current user has authorized this application.
*/
function checkAuth() {
gapi.auth.authorize(
{
'client_id': CLIENT_ID,
'scope': SCOPES.join(' '),
'immediate': true
}, handleAuthResult);
}
/**
* Handle response from authorization server.
*
* @param {Object} authResult Authorization result.
*/
function handleAuthResult(authResult) {
var authorizeDiv = document.getElementById('authorize-div');
if (authResult && !authResult.error) {
// Hide auth UI, then load client library.
authorizeDiv.style.display = 'none';
loadCalendarApi();
} else {
// Show auth UI, allowing the user to initiate authorization by
// clicking authorize button.
authorizeDiv.style.display = 'inline';
}
}
/**
* Load Google Calendar client library. List upcoming events
* once client library is loaded.
*/
function loadCalendarApi() {
gapi.client.load('calendar', 'v3', listUpcomingEvents);
}
/**
* Print the summary and start datetime/date of the next ten events in
* the authorized user's calendar. If no events are found an
* appropriate message is printed.
*/
function listUpcomingEvents() {
var request = gapi.client.calendar.events.list({
'calendarId': '<MY-PUB-CAL-ID>',
'timeMin': (new Date()).toISOString(),
'showDeleted': false,
'singleEvents': true,
'maxResults': 10,
'orderBy': 'startTime'
});
request.execute(function(resp) {
var events = resp.items;
appendPre('Upcoming events:');
if (events.length > 0) {
for (i = 0; i < events.length; i++) {
var event = events[i];
var when = event.start.dateTime;
if (!when) {
when = event.start.date;
}
appendPre(event.summary + ' (' + when + ')')
}
} else {
appendPre('No upcoming events found.');
}
});
}
/**
* Append a pre element to the body containing the given message
* as its text node.
*
* @param {string} message Text to be placed in pre element.
*/
function appendPre(message) {
var pre = document.getElementById('output');
var textContent = document.createTextNode(message + '\n');
pre.appendChild(textContent);
}
</script>
<script src="https://apis.google.com/js/client.js?onload=loadCalendarApi">
</script>
</head>
<body>
<div id="authorize-div" style="display: none">
<span>Authorize access to Google Calendar API</span>
<!--Button for the user to click to initiate auth sequence -->
<button id="authorize-button" onclick="handleAuthClick(event)">
Authorize
</button>
</div>
<pre id="output"></pre>
</body>
</html>
그래서이 거의 작동하지만이 요청을 할 때이 오류가 얻을 수 있습니다 :
{
"error": {
"errors": [
{
"domain": "global",
"reason": "required",
"message": "Login Required",
"locationType": "header",
"location": "Authorization"
}
],
"code": 401,
"message": "Login Required"
}
}
왜 생각을 이 공개 캘린더에 읽기 전용으로 액세스하도록 인증되지 않았습니까 ??
미안 해요, 난 조금 혼란 스러워요의를하자 API_KEY을 설정합니다. 그래서 나는이 프로젝트에 연결되어야하는 서비스 계정을 만들고 캘린더에 해당 계정을 추가하여 볼 수있게했습니다. 그러나 나는 거기에서 어디로 가야할지 모르겠습니다. SA에는 키 ID가 있지만 어디서나 사용하도록되어 있는지 확실하지 않습니다. 내 코드는 이전에 게시 한 것과 정확히 동일하므로 수정해야하는 것이 무엇인지 잘 모르겠습니다. ( – Diericx