提问者:小点点

OAuth 2.0适用于谷歌工作表,但不适用于谷歌日历API


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

谷歌连接:绝对路径:H:/ownCloud/Wabco/Workspace/WabcoDiagram/WebContent/resources/webservicecount。json谷歌连接:绝对路径:H:/ownCloud/Wabco/Workspace/WabcoDiagram/WebContent/resources/webservicecount。线程“main”java中的json异常。lang.NoSuchMethodError:com。谷歌。应用程序编程接口。客户古格里皮斯。服务。json。AbstractGoogleJsonClient$Builder。setBatchPath(Ljava/lang/String;)Lcom/google/api/client/googleapis/services/AbstractGoogleClient$Builder;在com。谷歌。应用程序编程接口。服务。日历日历$Builder。setBatchPath(Calendar.java:6758)位于com。谷歌。应用程序编程接口。服务。日历日历$Builder。(Calendar.java:6737)在de.promolitor。wabcodiagramviewer。审计谷歌日历。getCalendarServiceLocal(AuditGoogleCalendar.java:112)位于de.promolitor。wabcodiagramviewer。审计谷歌日历。在de.promolitor处初始化CalendarServiceLocal(AuditGoogleCalendar.java:150)。wabcodiagramviewer。审计谷歌日历。main(AuditGoogleCalendar.java:156)

我的问题:我可以使用服务帐户登录到谷歌日历应用编程接口,或者我必须做什么更改,或者我的代码中是否有其他错误?

我是否可以从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();
        }
    }

}

共1个答案

匿名用户

我的问题:我可以使用服务帐户登录到谷歌日历应用编程接口,或者我必须做什么更改,或者我的代码中是否有其他错误?

是的,你可以使用谷歌日历的服务帐户。在我看来,您的代码还可以。您只需添加您已经完成的日历范围,并在google开发者控制台中为服务帐户启用google日历api。

我是否可以从Web服务帐户发送日历邀请,并使另一个用户成为活动的管理员/创建者,以便他可以在必要时更改活动?这是否需要“域权限”?我在一家使用GSuit的公司工作。

是的,您可以使用服务帐户。您必须让Gsuit管理员授予服务帐户访问日历的权限,然后它才能创建活动和邀请人员。是的,它需要“域范围的权限”?插入事件后,您应该能够修补事件并将其他人设置为组织者。检查一下这已经有一段时间了,我已经记不清了,然后说对不起。