2016-12-15 79 views
0

我有一個簡單的Java應用程序嘗試訪問Google工作表,但是每當我運行它時,我都會遇到異常。該代碼是:通過App引擎訪問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: Could not initialize class org.mortbay.jetty.Server at com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver.getRedirectUri(LocalServerReceiver.java:98) at com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp.authorize(AuthorizationCodeInstalledApp.java:76) at com.google.appengine.demos.SheetsIntegration.authorize(SheetsIntegration.java:55) at com.google.appengine.demos.SheetsIntegration.getSheetsService(SheetsIntegration.java:60) at com.google.appengine.demos.SheetsIntegration.(SheetsIntegration.java:37)

SheetsIntegration.java:55是我嘗試創建憑據對象 - 我加了一個註釋這條線。

在我的POM,我有以下條目:

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

任何人都可以擺脫對我做錯了什麼在這裏一些輕?謝謝。

回答

0

我設法爲我的問題提出了一個解決方案,所以我會在這裏發佈以防其他人有類似的情況。在我的情況下,我使用的是servlet-api的3.1.0版本。一旦我降級到2.5,問題就消失了(顯然還會出現其他問題)。