【问题标题】:TokenResponseException: 401 Unauthorized When Trying to access Google Contacts ApiTokenResponseException: 401 Unauthorized 尝试访问 Google Contacts Api 时
【发布时间】:2017-10-30 02:44:27
【问题描述】:

我正在尝试连接到 google Contacts api 以访问我保存在 google 中的联系人/人员,但它会引发 TokenResponseException:401 Unauthorized。我对 Google Oauth2.0 有点陌生。我已经根据需要将服务帐户密钥文件下载到我的项目根目录。

下面是代码:

import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.gdata.client.contacts.ContactsService;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.Arrays;

public class Connector {
private static ContactsService contactService = null;
    private static HttpTransport httpTransport;

    private static final String APPLICATION_NAME = "example";
    private static final String SERVICE_ACCOUNT_EMAIL = "example_mail@example-166608.iam.gserviceaccount.com";
    private static final java.util.List<String> SCOPE = Arrays.asList("https://www.google.com/m8/feeds/");

    private Connector() {
        // explicit private no-args constructor
    }

    public static void main(String[] args) {
        System.out.println(getInstance());
    }

    public static ContactsService getInstance() {
        if (contactService == null) {
            try {
                contactService = connect();
            } catch (GeneralSecurityException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return contactService;
    }

    private static ContactsService connect() throws GeneralSecurityException, IOException {
        httpTransport = GoogleNetHttpTransport.newTrustedTransport();

        java.io.File p12File = new java.io.File("example-e8135faedf4e.p12");

        // @formatter:off
        GoogleCredential credential = new GoogleCredential.Builder()
                .setTransport(httpTransport)
                .setJsonFactory(JacksonFactory.getDefaultInstance())
                .setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
                .setServiceAccountScopes(SCOPE)
                .setServiceAccountPrivateKeyFromP12File(p12File)
                .setServiceAccountUser("example@gmail.com")
                .build();
        // @formatter:on

        if (!credential.refreshToken()) {
            throw new RuntimeException("Failed OAuth to refresh the token");
        }

        ContactsService myService = new ContactsService(APPLICATION_NAME);
        myService.setOAuth2Credentials(credential);

        return myService;
    }
}

但是会抛出以下异常:

com.google.api.client.auth.oauth2.TokenResponseException: 401 Unauthorized
null
在 com.google.api.client.auth.oauth2.TokenResponseException.from( TokenResponseException.java:105) 在 com.google.api.client.auth.oauth2.TokenRequest.executeUnparsed(TokenRequest.java:287) 在 com.google.api.client.auth.oauth2.TokenRequest.execute(TokenRequest.java:307) 在 com.google.api.client.googleapis.auth.oauth2.GoogleCredential.executeRefreshToken(GoogleCredential.java:384) 在 com.google.api.client.auth.oauth2.Credential.refreshToken(Credential.java:489) 在 Connector.connect(Connector.java:58) 在 Connector.getInstance(Connector.java:31) 在 Connector.main(Connector.java:25) 构建成功(总时间:9 秒)

【问题讨论】:

    标签: java oauth-2.0 google-contacts-api


    【解决方案1】:

    基于此related threadTokenResponseException 可能是由无效的客户端 ID、客户端密码或范围以及刷新令牌过度使用等多种原因引起的。 here 还指出,“401 Unauthorized”异常的另一个可能来源是离开 credential.refreshToken()。调用是将访问代码写入引用所必需的。

    可能也有帮助的其他参考:401 response when athenticating via Service Account and OAuth2 to GoogleDrive

    【讨论】:

    • 我看到这么晚了,但感谢您的帮助。我能够解决问题,让我尽快发布最终代码
    • @MichaelGatumaSelvatico:你的解决方案是什么。
    • @MichaelGatumaSelvatico 也为他的最后定理找到了一个简单的证明,但他没有写出来。这导致数十名数学家疯狂了 3 个世纪。
    【解决方案2】:

    我发现您的代码与快速入门示例中的代码差不多。如果这至少有效一次,请检查您是否有任何令牌保存在令牌文件夹下。

    【讨论】:

      猜你喜欢
      • 2017-04-14
      • 2017-09-04
      • 2016-12-14
      • 2020-03-29
      • 2019-03-16
      • 2017-03-22
      • 1970-01-01
      • 2021-06-02
      • 1970-01-01
      相关资源
      最近更新 更多