Java、Google 工作表 API:AuthSub 令牌范围错误

Java, Google Sheets API: AuthSub token has wrong scope

我正在关注 Google 表格 API 上的文档,但我无法使其正常工作。当我尝试访问电子表格时出现以下错误:

Exception in thread "main" com.google.gdata.client.GoogleService$SessionExpiredException: Token invalid - AuthSub token has wrong scope
<HTML>
<HEAD>
<TITLE>Token invalid - AuthSub token has wrong scope</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H1>Token invalid - AuthSub token has wrong scope</H1>
<H2>Error 401</H2>
</BODY>
</HTML>

使用代码:

SpreadsheetService sheetService = new SpreadsheetService("App-v1");

sheetService.setProtocolVersion(SpreadsheetService.Versions.V3);
sheetService.setOAuth2Credentials(DriveService.getCredential());

URL mFeedURL = new URL(SPREADSHEET_FEED);
SpreadsheetFeed feed = sheetService.getFeed(mFeedURL, SpreadsheetFeed.class);

但是当我从 Google 驱动器中检索文件名时,一切顺利。


我正在使用的范围和提要网址:

SPREADSHEET_FEED = "https://spreadsheets.google.com/feeds/spreadsheets/private/full"

SCOPES = Arrays.asList(DriveScopes.DRIVE_METADATA_READONLY,
                       "https://spreadsheets.google.com/feeds/"
                      );

这是有权限或令牌的东西,还是只是错误的实现?

解决了。 DataStoryFactory 对象以某种方式在以下代码段中引发了该错误:

GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
                        HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
                        .setDataStoreFactory(DATA_STORE_FACTORY)
                        .setAccessType("offline")
                        .build();

FileDataStoreFactory 对象正在使用放置在以下目录中的 "StoredCredential" 文件:

$HOME/.credentials

所以,我需要做的就是删除它可以重新创建的那个目录。之后它会进行回调并要求您在 Google 控制台中为您的应用程序授予权限,现在它会正常工作。或者您可以通过以下方法更改 userId:

Credential mCredential = new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver())
                        .authorize("user");