Chatter API Post Update via Java

import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.multipart.*;

public class ChatterIntegrate {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception{
		// TODO Auto-generated method stub
		
	    String oauthToken = "00DA0000000ao2F!ASAAQN3kdIrLpcdqrtUBEpKBJg_TK72qTx7urEh.AflgORiF05b_JVEDR1wjBFi9Y229Sni0MhJ1xRm3SJ8KsaM7M.O3_0ic";
	    String url = "https://na10.salesforce.com/services/data/v24.0/chatter/feeds/user-profile/me/feed-items";
	    String text = "My first post to Chatter via Java";
	    Part[] parts = {
	        new StringPart("text", text),
	    };

	    final PostMethod postMethod = new PostMethod(url);
	                
	    try {
	      postMethod.setRequestEntity(new MultipartRequestEntity(parts, postMethod.getParams()));
	      postMethod.setRequestHeader("Authorization", "OAuth " + oauthToken);
	      postMethod.addRequestHeader("X-PrettyPrint", "1");
	      HttpClient httpClient = new HttpClient();
	      httpClient.getParams().setSoTimeout(60000);
	      System.out.println(postMethod.getResponseBodyAsString());
	    } finally {
	      postMethod.releaseConnection();
	    }

	}
}
About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 254 other followers

%d bloggers like this: