2

我正在尝试使用 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 ,它仍然给我上面写的错误。

我无法理解这个错误:它来自哪里?为什么会这样?我该如何解决?

如果有人可以帮助我,那就太好了。如果我有任何信息,我会回来提供更多信息。

4

1 回答 1

2

这个问题已经很自豪地解决了:

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

我正在使用 OAth2,当您重定向用户时,您可以检索附加了 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");

https://developers.google.com/google-apps/spreadsheets/?hl=pt-BR在“使用 OAuth 2.0 授权请求”下所见。

需要指出的另一件事是,某些用户似乎存在以下问题:

service.setOAuth2Credentials(credential);

因此,修复它的一种方法是在调用之前刷新令牌,如下所示:

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

我希望遇到这篇文章的人会发现上面的信息很有用!:)

祝你有美好的一天,编码员!

于 2014-07-31T08:44:15.583 回答