【问题标题】:Java, Google Sheets API: AuthSub token has wrong scopeJava、Google Sheets API:AuthSub 令牌的范围错误
【发布时间】:2015-08-20 15:04:58
【问题描述】:

我一直在关注有关 Google Sheets 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 Drive 中检索文件名时,一切顺利。


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

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

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

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

【问题讨论】:

  • 根据page,这是一条通用错误消息,适用于访问范围与您所要求的不匹配的任何情况,无论是 OAuth 还是 AuthSub。所以原因可能是因为使用了已弃用的范围。 “docs.google.com/feeds”用于文档列表 API,已于 2015 年 4 月 20 日弃用。检查此page 以替换 Drive 范围。检查是否可以通过更改范围来消除此错误。
  • @KRR 不,它不会影响它并返回相同的错误。

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


【解决方案1】:

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

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

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

$HOME/.credentials

所以,我需要做的就是删除该目录,以便它可以再次创建它。之后它将进行回调并要求您在 Google Console 中授予您的应用程序的权限,现在它可以正常工作了。或者您可以通过以下方法更改 userId:

凭证 mCredential = new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver())
                        .authorize("用户");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 2012-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多