Java,Google表格API:AuthSub令牌的范围错误

时间:2015-08-20 15:04:58

标签: java google-drive-api google-sheets

我正在关注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云端硬盘检索文件名时,一切顺利。

我使用的范围和Feed网址:

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

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

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

1 个答案:

答案 0 :(得分:0)

解决了它。不知何故,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");