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)

サービス変数の関数で遊んでみましたが、1 行コメントされていることがわかります。RefreshToken の代わりに AccessToken を試してみましたが、上記のエラーが表示されます。

このエラーが理解できません: どこから来たのですか? なぜそれが起こるのですか?どうすれば修正できますか?

誰かが私を助けることができれば、それは素晴らしいことです。また情報があればまた来ます。

4

1 に答える 1

2

この問題は誇らしげに解決されました:

わかりました、私の間違いです - フィード。

私は OAth2 を使用しており、ユーザーをリダイレクトしてアクセス トークンを取得できるようにすると、SCOPES が添付されています。これは、アプリにどのような種類のアクセス許可を与えるかを意味します。フィードを参照する次の最後の 2 つがありませんでした。

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 によるリクエストの承認」の下にあるように

指摘すべきもう1つのことは、一部のユーザーには次の点で問題があるように思われることです。

service.setOAuth2Credentials(credential);

したがって、それを修正する 1 つの方法は、次のように呼び出す前にトークンを更新することです。

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

この投稿に遭遇した人は誰でも、上記の情報が役立つことを願っています! :)

コーダーの皆さん、よい一日を!

于 2014-07-31T08:44:15.583 に答える