我試圖通過Google Contacts API和Java應用更新我的Google Apps用戶的聯繫人。Google Contacts API - Java:無法更新聯繫人
我能夠獲得這個特定用戶的聯繫人,但是如果我想更新某個聯繫人,則更新失敗。
我的代碼:Eclipse中的
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 = "[email protected]";
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();
}
}
}
控制檯輸出:
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)
在控制檯輸出的第一行prooves觸點可以被讀取。 ContactsMain.java:60表示以下行:
entry.update();
有沒有人知道我在做什麼錯在這裏?在我想更新之前,是否必須將一些標記添加到「入口」對象?
非常感謝您的幫助。非常感謝你,
馬爾科
如果我把「myService.setUserCredentials(MY_USER,MY_PASSWORD);」之後的行「myService.setOAuthCredentials(oauthParameters,簽名者);」那麼它的工作。但我認爲,雙腿oauth背後的想法是,你不需要任何用戶密碼?這很令人困惑,因爲我可以在沒有用戶密碼的情況下閱讀聯繫人,但爲了更新,我需要密碼。在我看來:真奇怪... – devchzh