TokenResponseException:401试图访问Google通讯录时未经授权Api

时间:2017-05-29 18:18:35

标签: java oauth-2.0 google-contacts

我正在尝试连接到Google通讯录API以访问我保存在谷歌中的联系人/人,但它会抛出 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未经授权的 null
在com.google.api.client.auth.oauth2.TokenResponseException。从(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)at   Connector.getInstance(Connector.java:31)at   Connector.main(Connector.java:25)BUILD SUCCESSFUL(总时间:9   秒)

2 个答案:

答案 0 :(得分:0)

基于此ClusterabilityTokenResponseException可能由许多内容引起,例如无效的客户端ID,客户端密钥或范围以及刷新令牌过度使用。还指出related thread“401未授权”例外的另一个可能来源是离开credential.refreshToken()。调用是将访问代码写入引用的必要条件。

可能也有帮助的其他参考:here

答案 1 :(得分:0)

我看到您的代码与快速入门示例中的代码非常相似。如果这至少有效一次,请检查您是否在令牌文件夹下保存了任何令牌。