【问题标题】:The result returned by the calendarList.list request is emptycalendarList.list 请求返回的结果为空
【发布时间】:2021-07-08 08:52:21
【问题描述】:

我创建了一个服务帐户来进行日历 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);
    }
}

结果

【问题讨论】:

  • 此服务帐户是否有权访问日历,或者您是否在使用模拟?在日历 API 上试试这个,您以自己的身份发出请求,而不是以服务帐户的身份提出请求

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


【解决方案1】:

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

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

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

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

假冒

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

.setServiceAccountUser("user@domain.com")

【讨论】:

    猜你喜欢
    • 2012-06-12
    • 2023-03-28
    • 2022-09-24
    • 2015-03-17
    • 2022-11-14
    • 1970-01-01
    • 1970-01-01
    • 2021-10-07
    • 2018-10-25
    相关资源
    最近更新 更多