OAuth 2.0适用于Google Sheets,但不适用于Google Calendar API



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

谷歌连接:绝对路径:H:/ownCloud/Wabco/Wablespace/WabcoDiagram/WebContent/resources/webserviceaccount.jsonGoogleConnection:绝对路径:H:/ownCloud/Wabco/Wablespace/WabcoDiagram/WebContent/resources/webserviceaccount.json线程"main"java.lang.NoSuchMethodError.com.google.api.client.googleapi.services.json.AbstractGoogleJsonClient$Builder.setBatchPath(Ljava/lang/String;)Lcom/google/api/client/googleapi/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.wabcodegramwiewer.audit.AuditGoogleCalendar.getCalendarServiceLocal(AuditGoogle日历.java:112)位于de.promolitor.wabcodegramwiewer.audit.AuditGoogleCalendar.initializeCalendarServiceLocal(AuditGoogle日历.java:150)位于de.promolitor.wabcodegramwiewer.audit.AuditGoogleCalendar.main(AuditGoogle日历.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();
}
}
}

我的问题:我可以使用服务帐户登录Google Calendar API吗?或者我必须做什么更改,或者我的代码中是否有其他错误?

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

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

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

最新更新