OAuth 2.0适用于Google表格,但不适用于Google Calendar API

时间:2017-11-15 14:17:30

标签: google-api google-calendar-api google-oauth

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

  

GoogleConnection:Absolut Path:H:/ownCloud/Wabco/Workspace/WabcoDiagram/WebContent/resources/webserviceaccount.json   GoogleConnection:Absolut Path:H:/ownCloud/Wabco/Workspace/WabcoDiagram/WebContent/resources/webserviceaccount.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)       at depromolitor.wabcodiagramviewer.audit.AuditGoogleCalendar.getCalendarServiceLocal(AuditGoogleCalendar.java:112)       at 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();
        }
    }

}

1 个答案:

答案 0 :(得分:0)

  

我的问题:我可以使用服务帐户登录Google Calendar API,或者我需要做哪些更改,或者我的代码中是否有其他错误?

是的,您可以在Google日历中使用服务帐户。您的代码看起来不错,您只需添加已完成的日历范围,并在Google开发者控制台中为服务帐户启用Google日历API。

  

我可以从Web服务服务帐户发送日历邀请,并让其他用户成为活动的管理员/创建者,这样他可以在必要时更改活动吗?这是否需要&#34;域范围的权限&#34;?我在一家使用GSuit的公司工作。

是的,您可以使用服务帐户。您将不得不让Gsuit管理员授予服务帐户访问日历的权限,然后它将能够创建活动并邀请其他人。是的,它需要&#34;域范围的权限&#34;?插入活动后,您应该能够修补该活动,并将其他人设置为组织者。 check this已经有一段时间了,因为我做了这个不能再记得那个对不起了。