Jav*_*ert 5 java google-sheets-api
我试过这里给出的例子.
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)
Run Code Online (Sandbox Code Playgroud)
我已经给了它所有必要的许可.
我正在使用表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;
}
Run Code Online (Sandbox Code Playgroud)
回应 -
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
Run Code Online (Sandbox Code Playgroud)
我从这个链接得到了进一步的帮助来解决 400 - Unable to parse range: Class Data!A2:A4"
小智 3
首先,请确保您正确遵循了快速入门指南中的步骤,尤其是在开发人员控制台中启用 Sheets API。
\n\n现在针对Error TokenResponseException: 401 Unauthorized,基于此线程,使用访问令牌进行 API 调用时出现该错误的常见原因是:
\n\n访问令牌过期(最常见)
开发人员意外禁用了 API(不常见)
用户撤销令牌(罕见)
有时,HTTP 4xx 的响应正文中存在更多解释。例如,在 Java 客户端中,您应该记录错误,因为它将有助于排除故障:
\n\ntry { \n // Make your Google API call\n} catch (GoogleJsonResponseException e) {\n GoogleJsonError error = e.getDetails();\n // Print out the message and errors\n}\nRun Code Online (Sandbox Code Playgroud)\n\n每当您收到 HTTP 4xx 并记录该响应时,您都可以使用现有代码并在此处进行 API 调用。这个\xe2\x80\x99会返回一些有用的信息。
\n\n如果令牌无效,您可以按照此步骤操作。
\n\n欲了解更多信息,你可以检查这个相关的SO问题。
\n| 归档时间: |
|
| 查看次数: |
4505 次 |
| 最近记录: |