Google 日历 API CalendarList.list() returns 一个奇怪的对象 - JS

Google Calendar API CalendarList.list() returns a weird object - JS

我想按照文档中所述使用 CalendarList.list() 获取使用 JS 的用户的日历列表。但对我来说,我只得到一个奇怪的对象作为响应,它总是看起来像这样:

{
    "Yd": 1,
    "mb": {
        "Vf": null,
        "yh": {
            "path": "/calendar/v3/users/me/calendarList",
            "method": "GET",
            "params": {},
            "headers": {},
            "root": "https://www.googleapis.com",
            "apiId": "calendar:v3"
        },
        "Cl": "auto",
        "G_": false,
        "VN": false,
        "aQ": false
    }
}

我正在使用 quickstart 中的标准代码,它对我来说工作正常,只有这个请求 returns 一个奇怪的结果。

在我的代码中看起来像这样(只是其中应该有趣的部分):

function initClient() {
      gapi.client.init({
        apiKey: API_KEY,
        clientId: CLIENT_ID,
        discoveryDocs: DISCOVERY_DOCS,
        scope: SCOPES
      }).then(function() {
        // Listen for sign-in state changes.
        gapi.auth2.getAuthInstance().isSignedIn.listen(updateSigninStatus);

        // Handle the initial sign-in state.
        updateSigninStatus(gapi.auth2.getAuthInstance().isSignedIn.get());
        authorizeButton.onclick = handleAuthClick;
        signoutButton.onclick = handleSignoutClick;
      }, function(error) {
        changeSpanText(JSON.stringify(error, null, 2));
      });
    }


function updateSigninStatus(isSignedIn) {
      if (isSignedIn) {
        authorizeButton.style.display = 'none';
        signoutButton.style.display = 'flex';
        changeSpanText(" You have been logged in succesfully");
        console.log(gapi.client.calendar.calendarList.list());
      } else {
        authorizeButton.style.display = 'flex';
        signoutButton.style.display = 'none';
        changeSpanText("");
      }
    }

我能够使用共享的代码有效地重现该问题,这与响应如何输出到控制台有关,进行如下一些修改:

            function updateSigninStatus(isSignedIn) {
            if (isSignedIn) {
                authorizeButton.style.display = 'none';
                signoutButton.style.display = 'flex';
                gapi.client.calendar.calendarList.list({}).then(function (response) { // Handle the results here (response.result has the parsed body).
                    console.log("Response", JSON.stringify(response, null,2));
                }, function (err) {
                    console.error("Execute error", err);
                });

            } else {
                authorizeButton.style.display = 'flex';
                signoutButton.style.display = 'none';
            }
        }

我得到了一个成功且格式正确的输出:

如果您希望只有一些详细信息字段而不是整个输出,例如 'summary',您可以执行以下操作:

            function updateSigninStatus(isSignedIn) {
            if (isSignedIn) {
                authorizeButton.style.display = 'none';
                signoutButton.style.display = 'block';
                listCalends();

            } else {
                authorizeButton.style.display = 'block';
                signoutButton.style.display = 'none';
            }
        }
         function listCalends() {
            gapi.client.calendar.calendarList.list().then(function (response) {
               var calendar = response.result.items;
               var strinx = calendar;
               console.log('Calendars owned by this user are: ');
               for (let index = 0 ; index < strinx.length; index++) {                    
                  console.log( strinx[index]['summary']);                      
               } 
            });
        }

推荐:

如果您不确定如何使用 Javascript 使用 Google API 做某事,我鼓励您使用他们的 Try this API 并单击 'Javascript' 选项它会告诉你请求是如何提出的,这样你就可以为以后的项目提供一些参考。