Google Spreadsheet getFeed错误和理解

问题描述:

我正在尝试使用Google Spreadsheets,但遇到了一个我无法理解的问题。Google Spreadsheet getFeed错误和理解

下面的代码之前,我解释一下我自己:

SpreadsheetService service = 
     new SpreadsheetService("MySpreadsheetIntegration-v1"); 

service.setOAuth2Credentials(credential); 
//service.setHeader("Authorization", "Bearer "+credential.getRefreshToken()); 

// TODO: Authorize the service object for a specific user (see other sections) 

// Define the URL to request. This should never change. 
URL SPREADSHEET_FEED_URL = new URL(
    "https://spreadsheets.google.com/feeds/spreadsheets/private/full"); 

// Make a request to the API and get all spreadsheets. 
SpreadsheetFeed feed = null; 
try { 
    feed = service.getFeed(SPREADSHEET_FEED_URL, SpreadsheetFeed.class); 
} catch (ServiceException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 
List<SpreadsheetEntry> spreadsheets = feed.getEntries(); 

// Iterate through all of the spreadsheets returned 
for (SpreadsheetEntry spreadsheet : spreadsheets) { 
    // Print the title of this spreadsheet to the screen 
    System.out.println(spreadsheet.getTitle().getPlainText()); 
} 

我试图做的是阅读的电子表格可在帐户的凭据变量指向。我在那里有AccessToken和RefreshToken。

当我运行的代码是,尝试捕捉进入捕捉与下面的错误会发生什么:

Exception in thread "main" java.lang.NullPointerException: No authentication header information 
at com.google.gdata.util.AuthenticationException.initFromAuthHeader(AuthenticationException.java:96) 
at com.google.gdata.util.AuthenticationException.<init>(AuthenticationException.java:67) 
at com.google.gdata.client.http.HttpGDataRequest.handleErrorResponse(HttpGDataRequest.java:608) 
at com.google.gdata.client.http.GoogleGDataRequest.handleErrorResponse(GoogleGDataRequest.java:564) 
at com.google.gdata.client.http.HttpGDataRequest.checkResponse(HttpGDataRequest.java:560) 
at com.google.gdata.client.http.HttpGDataRequest.execute(HttpGDataRequest.java:538) 
at com.google.gdata.client.http.GoogleGDataRequest.execute(GoogleGDataRequest.java:536) 
at com.google.gdata.client.Service.getFeed(Service.java:1135) 
at com.google.gdata.client.Service.getFeed(Service.java:998) 
at com.google.gdata.client.GoogleService.getFeed(GoogleService.java:645) 
at com.google.gdata.client.Service.getFeed(Service.java:1017) 
at DriveCommandLine.main(DriveCommandLine.java:69) 

我打过与服务变量功能,你可以看到我有一条线评论说。我用AccessToken而不是RefreshToken尝试过,它仍然给我上面写的错误。我不能理解这个错误: 它来自哪里? 为什么会发生? 我该如何解决它?

如果有人能帮助我,那将会很棒。如果我有任何问题,我会回来提供更多信息。

+0

你在代码中的某个地方使用'xoauth_requestor_id'吗? – 2013-04-24 17:42:54

+0

我不知道。我怎么知道? – FPJ 2013-04-24 17:43:33

+0

看看这个:https://groups.google.com/forum/?fromgroups=#!topic/google-documents-list-api/BdzYdNz6I3Q – 2013-04-24 17:44:43

这个问题一直自豪地解决

好了,所以我的错误 - 饲料。

我正在使用OAth2,当您重定向用户以便您可以检索Access令牌时,它会附加SCOPES,这意味着您将为该应用提供何种权限。我错过下面最后两指提要:

private static final List<String> SCOPES = Arrays.asList(
    "https://www.googleapis.com/auth/drive.file", 
    "https://www.googleapis.com/auth/userinfo.email", 
    "https://www.googleapis.com/auth/userinfo.profile", 
    "https://docs.google.com/feeds", 
    "https://spreadsheets.google.com/feeds"); 

由于根据“使用OAuth 2.0授权请求”上看到https://developers.google.com/google-apps/spreadsheets/?hl=pt-BR

另一个要指出的是,似乎有一些问题,一些用户关于:

credential.refreshToken(); 
srv.setOAuth2Credentials(credential); 

service.setOAuth2Credentials(credential); 

所以一个修复它是调用它像这样前刷新令牌方式

我希望遇到这个帖子的人会发现上面的信息有用! :)

有一个美好的一天,编码!