Google api OAuth2.0适用于谷歌工作表,但不适用于谷歌日历API

Google api OAuth2.0适用于谷歌工作表,但不适用于谷歌日历API,google-api,google-calendar-api,google-oauth,Google Api,Google Calendar Api,Google Oauth,我正在使用GoogleAPI来读/写GoogleSheets和GoogleDriveAPI。 我最近被分配了一个项目,需要发送日历邀请。该网站是一个JSF页面,托管在ApacheTomcat8.5上。对于其他API,我使用的是服务帐户。 我现在尝试以同样的方式授权日历服务,但失败了。第一行是工作表服务的成功授权,它在日历服务上失败: 谷歌连接:绝对路径:H:/ownCloud/Wabco/Workspace/WabcoDiagram/WebContent/resources/webservice

我正在使用GoogleAPI来读/写GoogleSheets和GoogleDriveAPI。 我最近被分配了一个项目,需要发送日历邀请。该网站是一个JSF页面,托管在ApacheTomcat8.5上。对于其他API,我使用的是服务帐户。 我现在尝试以同样的方式授权日历服务,但失败了。第一行是工作表服务的成功授权,它在日历服务上失败:

谷歌连接:绝对路径:H:/ownCloud/Wabco/Workspace/WabcoDiagram/WebContent/resources/webserviceccount.json 谷歌连接:绝对路径:H:/ownCloud/Wabco/Workspace/WabcoDiagram/WebContent/resources/webserviceccount.json 线程“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; 位于com.google.api.services.calendar.calendar$Builder.setBatchPath(calendar.java:6758) 在com.google.api.services.calendar.calendar$Builder上(calendar.java:6737) 位于de.promolitor.wabcodiagramviewer.audit.AuditGoogleCalendar.getCalendarServiceLocal(AuditGoogleCalendar.java:112) 位于de.promolitor.wabcodiagramviewer.audit.AuditGoogleCalendar.initializeCalendarServiceLocal(AuditGoogleCalendar.java:150) 位于de.promolitor.wabcodiagramviewer.audit.AuditGoogleCalendar.main(AuditGoogleCalendar.java:156)

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

我是否可以从Web服务帐户发送日历邀请,并使另一个用户成为活动的管理员/创建者,以便他可以在必要时更改活动?这是否需要“域权限”? 我在一家使用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();
        }
    }

}
package de.promolitor.wabcodiagramviewer.audit;
导入java.io.FileInputStream;
导入java.io.IOException;
导入java.security.GeneralSecurityException;
导入java.util.ArrayList;
导入java.util.array;
导入java.util.HashMap;
导入java.util.List;
导入java.util.Map;
导入javax.faces.context.FacesContext;
导入javax.servlet.ServletContext;
导入com.google.api.client.auth.oauth2.Credential;
导入com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
导入com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
导入com.google.api.client.http.HttpTransport;
导入com.google.api.client.json.JsonFactory;
导入com.google.api.client.json.jackson2.JacksonFactory;
导入com.google.api.client.util.DateTime;
导入com.google.api.services.calendar.calendar;
导入com.google.api.services.calendar.CalendarScopes;
导入com.google.api.services.calendar.model.Event;
导入com.google.api.services.calendar.model.Events;
导入com.google.api.services.drive.drive;
导入com.google.api.services.drive.DriveScopes;
导入com.google.api.services.gmail.GmailScopes;
导入com.google.api.services.plus.plus;
导入com.google.api.services.sheets.v4.sheets;
导入com.google.api.services.sheets.v4.SheetsScopes;
公共类审核谷歌日历{
/**应用程序名称*/
私有静态最终字符串应用程序\u NAME=“Wabco Audit”;
/**{@link FileDataStoreFactory}的全局实例*/
//私有静态文件数据存储工厂数据存储工厂;
/**JSON工厂的全局实例*/
私有静态最终JsonFactory JSON_FACTORY=JacksonFactory.getDefaultInstance();
私有静态GoogleCredential凭证;
/**HTTP传输的全局实例*/
专用静态HttpTransport-HttpTransport;
私人静态加号;
私人静电纸服务;
专用静态驱动服务;
专用静态日历服务;
/**
*此快速启动所需作用域的全局实例。
*
*如果修改这些作用域,请删除以前保存的凭据
*~/.credentials/sheets.googleapis.com-java-quickstart
*/
私有静态最终列表范围=Arrays.asList(SheetsScopes.SPREADSHEETS,DriveScopes.DRIVE,
GmailScopes.MAIL_GOOGLE_COM,CalendarScopes.CALENDAR);
静止的{
试一试{
httpTransport=GoogleNetHttpTransport.newTrustedTransport();
}捕获(可丢弃的t){
t、 printStackTrace();
系统出口(1);
}
}
public static GoogleCredential authorize()引发IOException、GeneralSecurityException{
字符串relativeWebPath=“/resources/”+“webserviceaccount.json”;
ServletContext ServletContext=(ServletContext)FacesContext.getCurrentInstance().getExternalContext()
.getContext();
字符串absoluteDiskPath=servletContext.getRealPath(relativeWebPath);
System.out.println(“谷歌连接:绝对路径:+absoluteDiskPath”);
GoogleCredential credential=GoogleCredential.fromStream(新文件输入流(绝对磁盘路径))
.createScoped(范围);
返回凭证;
}
public static GoogleCredential authorizeLocal()引发IOException、GeneralSecurityException{
String absoluteDiskPath=“H:/ownCloud/Wabco/Workspace/WabcoDiagram/WebContent/resources/webservicecaccount.json”;
System.out.println(“谷歌连接:绝对路径:+absoluteDiskPath”);
GoogleCredential credential=GoogleCredential.fromStream(新文件输入流(绝对磁盘路径))
.createScoped(范围);
返回凭证;
}
公共静态日历getCalendarService()引发IOException、GeneralSecurityException{
凭证=授权();
返回新的Calendar.Builder(httpTransport、JSON\u工厂、凭证)。setApplicationName(应用程序\u名称)
.build();
}