我正在尝试使用 Google 电子表格,但遇到了一个我无法理解的问题。
下面是我解释自己之前的代码:
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。
当我运行代码时,Try Catch 进入 Catch 并出现以下错误:
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,但它仍然给出我上面写的错误。
我无法理解这个错误: 它从何而来? 为什么会发生这种情况? 我该如何修复它?
如果有人能帮助我,那就太好了。如果有的话,我会回来提供更多信息。
最佳答案
这个问题已被自豪地解决:
好吧,我的错误 - Feed。
我正在使用 OAth2,当您重定向用户以便检索访问 token 时,它附加了范围,这意味着您将向应用程序授予哪种权限。我遗漏了以下最后两个有关提要的内容:
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");
如 https://developers.google.com/google-apps/spreadsheets/?hl=pt-BR 上所示在“使用 OAuth 2.0 授权请求”下。
另一件事需要指出的是,一些用户似乎存在一些问题:
service.setOAuth2Credentials(credential);
因此解决此问题的一种方法是在调用它之前刷新 token ,如下所示:
credential.refreshToken();
srv.setOAuth2Credentials(credential);
我希望看到这篇文章的人都觉得上面的信息有用! :)
祝你有美好的一天,编码员!
关于java - Google Spreadsheet getFeed 错误和理解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16198518/