Google通讯录API - Java:无法更新联系人

时间:2012-10-09 13:31:37

标签: google-api google-api-java-client

我正在尝试通过Google通讯录API和Java应用更新我的Google Apps用户的联系人。

我可以获取此特定用户的联系人,但如果我想更新某个联系人,则更新失败。

我的代码:

package com.haha;

import java.net.URL;

import com.google.gdata.client.*;
import com.google.gdata.client.Query.CustomParameter;
import com.google.gdata.client.authn.oauth.GoogleOAuthParameters;
import com.google.gdata.client.authn.oauth.OAuthHmacSha1Signer;
import com.google.gdata.client.authn.oauth.OAuthSigner;
import com.google.gdata.client.authn.oauth.OAuthParameters.OAuthType;
import com.google.gdata.client.contacts.*;
import com.google.gdata.data.contacts.ContactEntry;
import com.google.gdata.data.contacts.ContactFeed;

public class ContactsMain {

    private static final String APP_NAME = "haha.com";
    private static final String CONSUMER_KEY = "haha.com";
    private static final String CONSUMER_SECRET = "123456";
    private static final String SCOPE_CONTACTS = "https://www.google.com/m8/feeds/";
    private static final String FEED_URL_CONTACTS = "https://www.google.com/m8/feeds/contacts/default/base";
    private static final String MY_USER = "user@haha.com";
    private static final String MAX_RESULTS = "10000";

    public static void main(String[] args) {

        try {

            // Create the service
            ContactsService myService = new ContactsService(APP_NAME);

            // OAuth
            GoogleOAuthParameters oauthParameters = new GoogleOAuthParameters();
            oauthParameters.setOAuthConsumerKey(CONSUMER_KEY);
            oauthParameters.setOAuthConsumerSecret(CONSUMER_SECRET);
            oauthParameters.setScope(SCOPE_CONTACTS);
            oauthParameters.setOAuthType(OAuthType.TWO_LEGGED_OAUTH);
            OAuthSigner signer = new OAuthHmacSha1Signer();

            // Authenticate the service
            myService.setOAuthCredentials(oauthParameters, signer);

            // Build the query
            Query myQuery = new Query(new URL(FEED_URL_CONTACTS));
            myQuery.addCustomParameter(new CustomParameter("xoauth_requestor_id", MY_USER));
            myQuery.addCustomParameter(new CustomParameter("max-results", MAX_RESULTS));

            // Get all contacts
            ContactFeed resultFeed = myService.query(myQuery, ContactFeed.class);

            for (ContactEntry entry : resultFeed.getEntries()) {

                if (entry.hasName() && entry.getName().hasFamilyName()) {

                    if ((entry.getName().getFamilyName().getValue()).equals("Lastname")) {

                        System.out.println("Contact 'Lastname' was found");

                        entry.getName().getFamilyName().setValue("Lastname-EDIT");
                        entry.update();
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Eclipse的控制台输出:

Contact 'Lastname' was found
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.update(Service.java:1563)
    at com.google.gdata.client.Service.update(Service.java:1530)
    at com.google.gdata.client.GoogleService.update(GoogleService.java:597)
    at com.google.gdata.data.BaseEntry.update(BaseEntry.java:639)
    at com.haha.ContactsMain.main(ContactsMain.java:60)

控制台输出的第一行证明可以读取联系人。 ContactsMain.java:60表示以下行:

entry.update();

有人知道我在做错了什么吗?在我想要更新之前,是否必须在“条目”对象中添加一些令牌?

非常感谢每一位帮助。非常感谢,

1 个答案:

答案 0 :(得分:0)

我认为只对两条腿OAuth启用了读访问权限,但是没有写访问权限(这也是配置api所发生的事情 - 我们一直试图从Googe获取写访问时间最长,但是他们从不承诺这样做)。如果我是正确的,那么很可能为什么更新失败了。 (例外情况表明缺乏正确的身份验证)。