java – 线程“main”中的异常com.google.api.client.auth.oauth2.TokenResponseException:401 Unauthorized

前端之家收集整理的这篇文章主要介绍了java – 线程“main”中的异常com.google.api.client.auth.oauth2.TokenResponseException:401 Unauthorized前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我试过这里给出的例子.

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)

我已经给了它所有必要的许可.

我正在使用表api版本v4

Update –

如果我在示例中传递电子邮件ID而不是用户,则它会给我这个响应.

变化 –

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 – 无法解析范围:类数据!A2:A4“

400 Bad Request Resolution

最佳答案
首先,请确保您正确遵循quickstart guide中的步骤,尤其是在开发人员控制台中启用Sheets API.

现在针对Error 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调用.这将返回一些有用的信息.

如果令牌无效,您可以按照以下步骤操作.

>从数据存储区或数据库删除访问令牌.
>使用刷新令牌获取新的访问令牌(如果您使用刷新令牌)
>尝试再次进行API调用.如果它有效,那你很好!如果不 …
>根据tokenInfo API检查访问令牌
>如果它仍然无效,请做一个完整的reauth

有关更多信息,请查看此相关的SO question.

原文链接:https://www.f2er.com/java/437337.html

猜你在找的Java相关文章