2

GoogleContactsAPIとJavaアプリを介してGoogleAppsユーザーの連絡先を更新しようとしています。

この特定のユーザーの連絡先を取得できますが、特定の連絡先を更新したい場合、更新は失敗します。

私のコード:

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 でも発生します。Google から最も長い間書き込みアクセスを取得しようとしてきましたが、コミットされませんでした。それをすること)。私が正しければ、それが更新が失敗する理由である可能性が非常に高いです。(例外は、適切な認証の欠如を示しています)。

于 2012-10-10T15:55:42.960 に答える