【问题标题】:OAuth2 Issue with gData ContactsServicegData ContactsService 的 OAuth2 问题
【发布时间】:2013-11-11 13:59:53
【问题描述】:

我在尝试使用 OAuth2 对 java 客户端库 (gData 1.47.1) 进行身份验证并进行单次调用时收到异常。以下是例外:

java.lang.NullPointerException: No authentication header information
at com.google.gdata.util.AuthenticationException.initFromAuthHeader(AuthenticationException.java:96)
at com.google.gdata.util.AuthenticationException.<init>(AuthenticationException.java:67)
at com.google.gdata.client.http.HttpGDataRequest.handleErrorResponse(HttpGDataRequest.java:608)
at com.google.gdata.client.http.GoogleGDataRequest.handleErrorResponse(GoogleGDataRequest.java:564)
at com.google.gdata.client.http.HttpGDataRequest.checkResponse(HttpGDataRequest.java:560)
at com.google.gdata.client.http.HttpGDataRequest.execute(HttpGDataRequest.java:538)
at com.google.gdata.client.http.GoogleGDataRequest.execute(GoogleGDataRequest.java:536)
at com.google.gdata.client.Service.getFeed(Service.java:1135)
at com.google.gdata.client.Service.getFeed(Service.java:998)
at com.google.gdata.client.GoogleService.getFeed(GoogleService.java:645)
at com.google.gdata.client.Service.getFeed(Service.java:1017)
at com.test.Main.main(Main.java:79)

创建服务的代码:

String scope = "https://www.google.com/m8/feeds";

ContactsService service = new ContactsService("APP_NAME");
        service.setOAuth2Credentials(getCredential(scope, userEmail));


public GoogleCredential getCredential(ArrayList<String> scopes, String userEmail) throws Exception {
        GoogleCredential credential = new GoogleCredential.Builder().setTransport(HTTP_TRANSPORT).setJsonFactory(JSON_FACTORY)
                .setServiceAccountId(this.serviceAccountEmail).setServiceAccountScopes(scopes).setServiceAccountPrivateKeyFromP12File(f)
                .setServiceAccountUser(userEmail).build();
        return credential;
    }

拨打电话:

URL feedUrl = new URL("https://www.google.com/m8/feeds/contacts/default/full");
        ContactFeed resultFeed = service.getFeed(feedUrl, ContactFeed.class);

我还在我的应用程序控制台中为此项目启用了联系人 API,如文档中所述。任何建议将不胜感激。谢谢。

【问题讨论】:

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


    【解决方案1】:

    同样的事情发生在我身上。我通过在令牌接近到期时刷新令牌来修复它。

    if (credential != null && (credential.getExpiresInSeconds() == null || credential.getExpiresInSeconds() < 5)) {
        credential.refreshToken();
    }
    

    IMO,较新的 google-api-client-java 会自动执行此操作,而 Gdata 不会。

    【讨论】:

      猜你喜欢
      • 2015-08-11
      • 2012-01-03
      • 2012-06-12
      • 1970-01-01
      • 2011-06-25
      • 2010-12-02
      • 2013-12-13
      • 2012-08-08
      • 1970-01-01
      相关资源
      最近更新 更多