通过App Engine访问Google表格会导致异常

时间:2016-12-15 13:46:56

标签: java google-app-engine google-sheets

我有一个简单的Java应用程序试图访问Google工作表,但每当我运行它时,我都会遇到异常。代码是:

public class SheetsIntegration {
private static HttpTransport transport;
private static JacksonFactory jsonFactory;
private static AppEngineDataStoreFactory dataStoreFactory;
private Sheets service;
private static final String APPLICATION_NAME = "My App";

private static List<String> scopes = Arrays.asList(SheetsScopes.SPREADSHEETS);

public SheetsIntegration() {
    try {
        transport = GoogleNetHttpTransport.newTrustedTransport();
        dataStoreFactory = AppEngineDataStoreFactory.getDefaultInstance();
        jsonFactory = JacksonFactory.getDefaultInstance();
        service = getSheetsService();
    } catch (Exception e) {
        // handle exception
    }
}

public static Credential authorize() throws IOException {
    InputStream in = AccessSheet.class.getResourceAsStream("/client_secret_access.json");
    GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(jsonFactory, new InputStreamReader(in));

    // Build flow and trigger user authorization request.
    GoogleAuthorizationCodeFlow flow =
            new GoogleAuthorizationCodeFlow.Builder(
                    transport, jsonFactory, clientSecrets, scopes)
                    .setDataStoreFactory(dataStoreFactory)
                    .setAccessType("offline")
                    .build();
    AuthorizationCodeInstalledApp app = new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver());
    Credential credential = app.authorize("user");// This is line 55
    return credential;
}

public static Sheets getSheetsService() throws IOException {
    Credential credential = authorize();
    return new Sheets.Builder(transport, jsonFactory, credential)
            .setApplicationName(APPLICATION_NAME)
            .build();
}

当我实例化SheetsIntegration时,我得到以下异常......

  

java.lang.NoClassDefFoundError:无法初始化类   org.mortbay.jetty.Server at   com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver.getRedirectUri(LocalServerReceiver.java:98)     在   com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp.authorize(AuthorizationCodeInstalledApp.java:76)     在   com.google.appengine.demos.SheetsIntegration.authorize(SheetsIntegration.java:55)     在   com.google.appengine.demos.SheetsIntegration.getSheetsService(SheetsIntegration.java:60)     在   com.google.appengine.demos.SheetsIntegration。(SheetsIntegration.java:37)

SheetsIntegration.java:55是我尝试创建Credential对象的地方 - 我在这一行添加了注释。

在我的pom中,我有以下条目:

<dependency>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty</artifactId>
<version>6.1.26</version>
</dependency>

有人能说清楚我在做错了吗?感谢。

1 个答案:

答案 0 :(得分:0)

我设法找到了我的问题的解决方案,所以我会发布在这里,以防其他人有类似的东西。在我的情况下,我使用3.1.0的servlet-api。一旦我降级到2.5,问题就消失了(显然会出现其他问题)。