我尝试了这里给出的例子。
https://developers.google.com/sheets/quickstart/java
它给了我这个例外 -
Exception in thread "main" com.google.api.client.auth.oauth2.TokenResponseException: 401 Unauthorized
at com.google.api.client.auth.oauth2.TokenResponseException.from(TokenResponseException.java:105)
at com.google.api.client.auth.oauth2.TokenRequest.executeUnparsed(TokenRequest.java:287)
at com.google.api.client.auth.oauth2.TokenRequest.execute(TokenRequest.java:307)
at com.google.api.client.auth.oauth2.Credential.executeRefreshToken(Credential.java:570)
at com.google.api.client.auth.oauth2.Credential.refreshToken(Credential.java:489)
at com.google.api.client.auth.oauth2.Credential.intercept(Credential.java:217)
at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:868)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:419)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:352)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:469)
at SheetsQuickstart.main(SheetsQuickstart.java:106)
我已经给予了所有必要的许可。
我正在使用sheet api v4版本
更新 -
如果我在示例中传递email id
而不是user
,那么它会给我这个回复。
更改 -
public static Credential authorize() throws IOException {
// Load client secrets.
InputStream in =
SheetsQuickstart.class.getResourceAsStream("/client_secret.json");
GoogleClientSecrets clientSecrets =
GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));
// Build flow and trigger user authorization request.
GoogleAuthorizationCodeFlow flow =
new GoogleAuthorizationCodeFlow.Builder(
HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
.setDataStoreFactory(DATA_STORE_FACTORY)
.setAccessType("offline")
.build();
//Changed Part.
Credential credential = new AuthorizationCodeInstalledApp(
flow, new LocalServerReceiver()).authorize("test@gmail.com");
System.out.println(
"Credentials saved to " + DATA_STORE_DIR.getAbsolutePath());
return credential;
}
回应 -
Name, Major
Alexandra, English
Andrew, Math
Anna, English
Becky, Art
Benjamin, English
Carl, Art
Carrie, English
Dorothy, Math
Dylan, Math
Edward, English
Ellen, Physics
Fiona, Art
John, Physics
Jonathan, Math
Joseph, English
Josephine, Math
Karen, English
Kevin, Physics
Lisa, Art
Mary, Physics
Maureen, Physics
Nick, Art
Olivia, Physics
Pamela, Math
Patrick, Art
Robert, English
Sean, Physics
Stacy, Math
Thomas, Art
Will, Math
我从此链接获得了进一步的帮助以解决400 - Unable to parse range: Class Data!A2:A4"
答案 0 :(得分:3)
首先,请确保您正确遵循quickstart guide中的步骤,特别是在开发人员控制台中启用表格API。
现在针对错误TokenResponseException:401 Unauthorized ,基于此thread,在使用访问令牌进行API调用时导致该错误的常见原因是:
过期的访问令牌(最常见)
开发人员意外禁用了API(不常见)
用户撤销令牌(罕见)
有时,HTTP 4xx的响应正文中存在更多解释。例如,在Java客户端中,您应该记录错误,因为它有助于排除故障:
try {
// Make your Google API call
} catch (GoogleJsonResponseException e) {
GoogleJsonError error = e.getDetails();
// Print out the message and errors
}
每当您获得HTTP 4xx并记录该响应时,您可以在此处获取现有代码并进行API调用。这将返回一些有用的信息。
如果令牌无效,您可以按照以下步骤操作。
有关详细信息,请查看相关的SO question。