[英]Issue with OAuth2 authentication with google spreadsheet
I am using java library for oauth2 authentication for accessing google spreadsheet. 我正在使用Java库进行oauth2身份验证,以访问Google电子表格。
I am using below code for OAuth2 authentication: 我正在使用以下代码进行OAuth2身份验证:
credential = new GoogleCredential.Builder().setTransport(HTTP_TRANSPORT)
.setJsonFactory(JSON_FACTORY)
.setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
.setTokenServerEncodedUrl("https://accounts.google.com/o/oauth2/token")
.setServiceAccountScopes("https://www.googleapis.com/auth/drive", "https://spreadsheets.google.com/feeds", "https://docs.google.com/feeds")
.setServiceAccountPrivateKeyFromP12File(new File("xxxxx-privatekey.p12")).build();
After getting "credential", using below code to read spreadsheet: 获得“凭据”后,使用以下代码读取电子表格:
SpreadsheetService service = new SpreadsheetService(
"MySpreadsheetIntegration");
service.setOAuth2Credentials(credential);
URL SPREADSHEET_FEED_URL = new URL("https://spreadsheets.google.com/feeds/spreadsheets/private/full");
SpreadsheetFeed feed = service.getFeed(SPREADSHEET_FEED_URL, SpreadsheetFeed.class);
System.out.println(feed.getTotalResults());
Executing above code give me back total result 0. 执行上面的代码将总结果返回给我0。
If I use: 如果我使用:
service.setUserCredentials("email", "password");
in place of oauth2 authentication, it gives me back correct results. 代替oauth2身份验证,它可以带给我正确的结果。 Not sure what is wrong with the OAuth2 authentication.
不确定OAuth2身份验证出了什么问题。 Also when I print "access token" from "credential" object it prints a valid access token.
同样,当我从“凭据”对象打印“访问令牌”时,它也会打印有效的访问令牌。
I use: 我用:
spreadsheetService = new SpreadsheetService("cellmaster.com.au-v0.2");
spreadsheetService.setHeader("Authorization", "Bearer " + accessToken);
rather than: 而不是:
spreadsheetService = new SpreadsheetService("cellmaster.com.au-v0.2");
spreadsheetService.setOAuth2Credentials(credential);
also I had to add code for the refresh token. 我还必须为刷新令牌添加代码。 As the access token soon expires.
由于访问令牌即将过期。 But the refresh token works as you would expect.
但是刷新令牌可以按您期望的那样工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.