calendarList.list 请求返回的结果为空

时间:2021-04-13 09:50:25

标签: java google-api google-calendar-api google-api-java-client service-accounts

我创建了一个服务帐户来进行日历 api 调用;但是当我的代码运行时,一切看起来都很完美。但是返回的数据是空的。我在谷歌网页上提供的api调试函数调用时可以返回数据。链接 => https://developers.google.com/calendar/v3/reference/calendarList/list#examples

我的Java代码

package org.test.google;

import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.calendar.Calendar;
import com.google.api.services.calendar.CalendarScopes;
import com.google.api.services.calendar.model.CalendarList;

import java.io.FileInputStream;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.Arrays;
import java.util.Set;
import java.util.stream.Collectors;

public class CalendarTest {
    private static final Set<String> scopes = Arrays.stream(new String[]{
            CalendarScopes.CALENDAR,
            CalendarScopes.CALENDAR_EVENTS,
            CalendarScopes.CALENDAR_EVENTS_READONLY,
            CalendarScopes.CALENDAR_READONLY,
            CalendarScopes.CALENDAR_SETTINGS_READONLY,
    }).collect(Collectors.toSet());


    public static void main(String[] args) throws GeneralSecurityException, IOException {
        NetHttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
        JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();

        Credential credential = getCredential(httpTransport, jsonFactory);

        Calendar service = new Calendar.Builder(httpTransport, jsonFactory, credential).setApplicationName("space").build();


        CalendarList calendarList = service.calendarList().list().execute();

        System.out.println(calendarList);

        if (calendarList.getItems().size() == 0) {
            System.out.println("no data");
        }
    }


    private static GoogleCredential getCredential(HttpTransport httpTransport, JsonFactory jsonFactory) throws IOException {
        return GoogleCredential.fromStream(
                new FileInputStream("/Users/clk528/Download/google/json/space-0c23309aa763.json"),
                httpTransport,
                jsonFactory
        ).createScoped(scopes);
    }
}

结果 enter image description here

enter image description here

1 个答案:

答案 0 :(得分:0)

您需要记住,服务帐户是没有 Web ui 界面的虚拟用户。因此,当您与服务帐户共享对日历的访问权限时,域范围内的委派不会自动将其添加到日历列表列表中。

日历列表只是出现在网页界面左下角的列表,当您与用户共享一个cleandr并且用户接受访问时,网页界面会自动将其添加到用户日历列表中服务帐户不会发生这种情况。

如果您希望日历显示在服务帐户的日历列表中,您需要让服务帐户执行 calendarlist.insert 以添加它

CalendarList calendarList = service.calendarList().list().execute();

模仿

如果您将用户设置为在您的域上模拟,我希望 calendar.list 方法然后返回该用户的日历列表。

.setServiceAccountUser("user@domain.com")
相关问题