Java,Google Sheets API:AuthSub令牌的范围有误 [英] Java, Google Sheets API: AuthSub token has wrong scope

查看:260
本文介绍了Java,Google Sheets API:AuthSub令牌的范围有误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 线程maincom.google.gdata中出现异常。 client.GoogleService $ SessionExpiredException:令牌无效 -  AuthSub令牌的作用域有误
< HTML>
< HEAD>
< TITLE>令牌无效 - AuthSub令牌具有错误的范围< / TITLE>
< / HEAD>
< BODY BGCOLOR =#FFFFFFTEXT =#000000>
< H1>令牌无效 - AuthSub令牌的作用域错误< / H1>
< H2>错误401< / H2>
< / BODY>
< / HTML>

包含代码:

  SpreadsheetService sheetService =新的SpreadsheetService(App-v1); 

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

网址mFeedURL =新网址(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/
);

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

解决方案

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

$ $ p $ Credential mCredential = new AuthorizationCodeInstalledApp(flow,new LocalServerReceiver())
.authorize(用户);


I was following documentation on Google Sheets API, but I can't make it work. When I'm trying to access a spreadsheets I got following error:

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>

with code:

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);

but when I'm retrieving file names from Google Drive it all goes well.


The scopes and feed urls that I'm using:

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

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

Is that something with permissions or token or it's just the wrong implementation?

解决方案

Solved it. Somehow that error was raised by DataStoryFactory object in the following segment of code:

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

FileDataStoreFactory object was using "StoredCredential" file placed in the following directory:

$HOME/.credentials

So, all I needed to do is to delete that directory that it could create it again. After what it's gonna make a callback and ask you to grant permissions for your application in Google Console and now it's gonna work properly. Or you could just change userId in the following method:

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

这篇关于Java,Google Sheets API:AuthSub令牌的范围有误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆