OAuth 2.0 适用于 Google 工作表但不适用于 Google 日历 API

OAuth 2.0 Works for Google Sheets but not for Google Calendar API

我将 Google API 用于 reading/writing Google 工作表和 Google 驱动器 API。 我最近分配了一个项目,需要发送日历邀请。该网站是托管在 Apache Tomcat 8.5 上的 JSF 页面。对于其他 API,我使用的是服务帐户。 我现在尝试以同样的方式授权日历服务,但失败了。第一行是表格服务授权成功,日历服务授权失败:

GoogleConnection: Absolut Path: H:/ownCloud/Wabco/Workspace/WabcoDiagram/WebContent/resources/webserviceaccount.json GoogleConnection: Absolut Path: H:/ownCloud/Wabco/Workspace/WabcoDiagram/WebContent/resources/webserviceaccount.json Exception in thread "main" java.lang.NoSuchMethodError: com.google.api.client.googleapis.services.json.AbstractGoogleJsonClient$Builder.setBatchPath(Ljava/lang/String;)Lcom/google/api/client/googleapis/services/AbstractGoogleClient$Builder; at com.google.api.services.calendar.Calendar$Builder.setBatchPath(Calendar.java:6758) at com.google.api.services.calendar.Calendar$Builder.(Calendar.java:6737) at de.promolitor.wabcodiagramviewer.audit.AuditGoogleCalendar.getCalendarServiceLocal(AuditGoogleCalendar.java:112) at de.promolitor.wabcodiagramviewer.audit.AuditGoogleCalendar.initializeCalendarServiceLocal(AuditGoogleCalendar.java:150) at de.promolitor.wabcodiagramviewer.audit.AuditGoogleCalendar.main(AuditGoogleCalendar.java:156)

我的问题: 我可以使用服务帐户登录到 Google 日历 API,或者我必须做哪些更改,或者我的代码中是否存在其他错误?

我可以从 Web 服务服务帐户发送日历邀请并让另一个用户成为活动的管理员/创建者,以便他可以在必要时更改活动吗?这需要 "domain-wide authority" 吗? 我在一家使用 GSuit 的公司工作。

示例代码。本地函数用于本地测试。

package de.promolitor.wabcodiagramviewer.audit;

import java.io.FileInputStream;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;

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.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.util.DateTime;
import com.google.api.services.calendar.Calendar;
import com.google.api.services.calendar.CalendarScopes;
import com.google.api.services.calendar.model.Event;
import com.google.api.services.calendar.model.Events;
import com.google.api.services.drive.Drive;
import com.google.api.services.drive.DriveScopes;
import com.google.api.services.gmail.GmailScopes;
import com.google.api.services.plus.Plus;
import com.google.api.services.sheets.v4.Sheets;
import com.google.api.services.sheets.v4.SheetsScopes;

public class AuditGoogleCalendar {
    /** Application name. */
    private static final String APPLICATION_NAME = "Wabco Audit";

    /** Global instance of the {@link FileDataStoreFactory}. */
    // private static FileDataStoreFactory DATA_STORE_FACTORY;

    /** Global instance of the JSON factory. */
    private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();

    private static GoogleCredential credential;

    /** Global instance of the HTTP transport. */
    private static HttpTransport httpTransport;

    private static Plus plus;

    private static Sheets service;
    private static Drive driveService;
    private static Calendar calendarService;

    /**
     * Global instance of the scopes required by this quickstart.
     *
     * If modifying these scopes, delete your previously saved credentials at
     * ~/.credentials/sheets.googleapis.com-java-quickstart
     */
    private static final List<String> SCOPES = Arrays.asList(SheetsScopes.SPREADSHEETS, DriveScopes.DRIVE,
            GmailScopes.MAIL_GOOGLE_COM, CalendarScopes.CALENDAR);

    static {
        try {
            httpTransport = GoogleNetHttpTransport.newTrustedTransport();
        } catch (Throwable t) {
            t.printStackTrace();
            System.exit(1);
        }
    }


    public static GoogleCredential authorize() throws IOException, GeneralSecurityException {
        String relativeWebPath = "/resources/" + "webserviceaccount.json";
        ServletContext servletContext = (ServletContext) FacesContext.getCurrentInstance().getExternalContext()
                .getContext();
        String absoluteDiskPath = servletContext.getRealPath(relativeWebPath);
        System.out.println("GoogleConnection: Absolut Path: " + absoluteDiskPath);

        GoogleCredential credential = GoogleCredential.fromStream(new FileInputStream(absoluteDiskPath))
                .createScoped(SCOPES);
        return credential;
    }

    public static GoogleCredential authorizeLocal() throws IOException, GeneralSecurityException {
        String absoluteDiskPath = "H:/ownCloud/Wabco/Workspace/WabcoDiagram/WebContent/resources/webserviceaccount.json";
        System.out.println("GoogleConnection: Absolut Path: " + absoluteDiskPath);

        GoogleCredential credential = GoogleCredential.fromStream(new FileInputStream(absoluteDiskPath))
                .createScoped(SCOPES);
        return credential;

    }

    public static Calendar getCalendarService() throws IOException, GeneralSecurityException {
        Credential credential = authorize();
        return new Calendar.Builder(httpTransport, JSON_FACTORY, credential).setApplicationName(APPLICATION_NAME)
                .build();
    }

    public static Calendar getCalendarServiceLocal() throws IOException, GeneralSecurityException {
        Credential credential = authorizeLocal();
        return new Calendar.Builder(httpTransport, JSON_FACTORY, credential).setApplicationName(APPLICATION_NAME)
                .build();
    }

    public static Sheets getSheetsService() throws IOException, GeneralSecurityException {
        credential = authorize();
        return new Sheets.Builder(httpTransport, JSON_FACTORY, credential).setApplicationName(APPLICATION_NAME).build();
    }

    public static Sheets getSheetsServiceLocal() throws IOException, GeneralSecurityException {
        credential = authorizeLocal();
        return new Sheets.Builder(httpTransport, JSON_FACTORY, credential).setApplicationName(APPLICATION_NAME).build();
    }

    public static Drive getDriveService() throws IOException, GeneralSecurityException {
        GoogleCredential credential = authorize();
        return new Drive.Builder(httpTransport, JSON_FACTORY, credential).setApplicationName(APPLICATION_NAME).build();
    }

    public static void initializeSheetService() throws IOException, GeneralSecurityException {
        service = getSheetsService();
    }

    public static void initializeSheetServiceLocal() throws IOException, GeneralSecurityException {
        service = getSheetsServiceLocal();
    }

    public static void initializeDriveService() throws IOException, GeneralSecurityException {
        driveService = getDriveService();
    }

    public static void initializeCalendarService() throws IOException, GeneralSecurityException {
        calendarService = getCalendarService();
    }

    public static void initializeCalendarServiceLocal() throws IOException, GeneralSecurityException {
        calendarService = getCalendarServiceLocal();
    }

    public static void main(String[] args) {
        try {
            initializeSheetServiceLocal();
            initializeCalendarServiceLocal();

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

My Questions: Can I login with a Service account into the Google Calendar API, or what are the changes I have to do, or is there another error in my code somewhere?

是的,您可以将服务帐户用于 Google 日历。您的代码在我看来没问题,您只需添加您已完成的日历范围,并在 Google 开发人员控制台中为服务帐户启用 google 日历 api。

Can I send out Calendar Invites from a Web Service Service account and make another user the Admin / Creator of the event, so he can change the event if necessary? Does this require "domain-wide authority"? I am working for a company that uses GSuit.

是的,您可以使用服务帐户。您将必须让 Gsuit 管理员授予服务帐户访问日历的权限,然后它才能创建活动并邀请其他人。是的,它需要 "domain-wide authority"?插入活动后,您应该可以修补 活动并将其他人设置为组织者。 check this 已经有一段时间没做这个了,实在记不住了,抱歉。