线程“main”中的异常com.google.api.client.auth.oauth2.TokenResponseException:401未经授权 [英] Exception in thread "main" com.google.api.client.auth.oauth2.TokenResponseException: 401 Unauthorized

查看:202
本文介绍了线程“main”中的异常com.google.api.client.auth.oauth2.TokenResponseException:401未经授权的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了这里给出的示例。

I tried the example given here.

https://developers.google.com/sheets/quickstart/java

它给我这个例外 -

Its giving me this exception -

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)

我已经给予了所有必要的许可。

I have given all the necessary permission to it.

我使用的是表格api版本v4

I am using sheet api version v4


更新 -

Update -

如果我在示例中传递电子邮件ID 而不是用户那么它给了我这个回复。

If i am passing email id in the example instead of user then its is giving me this response.

更改 -

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 - 无法解析范围:Class Data!A2:A4

I got the further help from this link to resolve 400 - Unable to parse range: Class Data!A2:A4"

400错误请求解决方案

解决方案

首先,请确保您正确遵循快速入门指南,特别是通过在开发者控制台中启用表格API。

First, make sure you follow properly the steps in the quickstart guide especially by enabling the Sheets API in your Developers Console.

现在针对错误TokenResponseException:401 Unauthorized ,基于从这个 thread ,使用访问令牌进行API调用时出现该错误的常见原因是:

Now for the Error TokenResponseException: 401 Unauthorized, based from this thread, the common causes for that error when making API calls with an access token are:


  • 过期访问令牌(最常见)

  • expired access token (most common)

开发人员意外禁用了API(不常见)

Developer accidentally disabled the APIs (uncommon)

用户撤销令牌(罕见)

有时,HTTP的响应正文中存在更多解释4XX。例如,在Java客户端中,您应该记录错误,因为它有助于排除故障:

Sometimes, more explanation exists in the response body of a HTTP 4xx. In the Java client, for example, you should log the error, because it will assist in troubleshooting:

try {   
       // Make your Google API call
} catch (GoogleJsonResponseException e) {
      GoogleJsonError error = e.getDetails();
      // Print out the message and errors
}

你可以拿走你的每当您获得HTTP 4xx并记录该响应时,现有代码并在此处进行API调用。这将返回一些有用的信息。

You could take your existing code and make an API call here whenever you get a HTTP 4xx and log that response. This’ll return some useful information.

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

If the token is invalid, you can follow this steps.


  1. 从数据存储区或数据库中删除访问令牌。

  2. 使用刷新令牌获取新的访问令牌(如果使用刷新令牌)

  3. 尝试再次进行API调用。如果它有效,那你很好!如果没有...

  4. 根据tokenInfo API检查访问令牌

  5. 如果它仍然无效,请执行完整的重复

  1. Remove the access token from your datastore or database.
  2. Use the refresh token to acquire a new access token (if you are using a refresh token)
  3. Try to make the API call again. If it works, you’re good! If not …
  4. Check the access token against the tokenInfo API
  5. If it’s still invalid, do a full reauth

有关详细信息,请查看此相关 SO问题

For more information, you can check this related SO question.

这篇关于线程“main”中的异常com.google.api.client.auth.oauth2.TokenResponseException:401未经授权的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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