2

我正在尝试通过 Google Contacts 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();

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

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

马可

4

1 回答 1

0

我想只为 2-legged OAuth 启用了读取访问权限,但没有启用写入访问权限(这也是配置 api 发生的情况 - 我们一直在尝试从 Googe 获得写入访问权限最长时间,但他们从未提交过去做)。如果我是正确的,那么这很可能是更新失败的原因。(异常指向缺乏适当的身份验证)。

于 2012-10-10T15:55:42.960 回答