• Sandeep Agrawal
  • NEWBIE
  • 0 Points
  • Member since 2012

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 3
    Questions
  • 12
    Replies

I need java code to post a image to chatter user using rest api.

 

I am using below code but it is not working for me:

Part[] parts = { 
									//new StringPart("desc", "Description"),
									new StringPart("body",stringBody),
									//new StringPart("fileName", fileName),
									//new StringPart("text", noteContent),																				
									new FilePart("feedItemFileUpload", imageFile),
								};
				    postMethod = new PostMethod(url);
					postMethod.setRequestEntity(new MultipartRequestEntity(parts, postMethod.getParams()));

postMethod.setRequestHeader("Authorization", "OAuth " + salesforceRestClient.getAuthToken());
				postMethod.setRequestHeader("Content-type", "multipart/form-data");
				org.apache.commons.httpclient.HttpClient client = new org.apache.commons.httpclient.HttpClient();
				int code =client.executeMethod(postMethod);


public static String generateJSONBodyForChatterFeed(String content, ArrayList<String> mentionIds, String imageDescription, String fileName, String imageTitle)
	{
		JSONArray msg = new JSONArray();
		String bodyString = null;
		JSONObject requestJSON=null;
		try 
		{			
			if (mentionIds != null)
			{
				for (int i = 0; i < mentionIds.size(); i++)
				{
					JSONObject mention = new JSONObject();				
					mention.put("type", "mention");
					mention.put("id", mentionIds.get(i));
					msg.put(mention);
				}				
			}
			if (content != null)
			{
				JSONObject text = new JSONObject();
				text.put("type", "text");
				
				text.put("text", " " + content);
				msg.put(text);
			}	
			JSONObject attachment = null;
			if (fileName != null && imageTitle != null)
			{
				attachment = new JSONObject();
				attachment.putOpt("desc", imageDescription);
				attachment.putOpt("fileName", fileName);
				attachment.putOpt("title", imageTitle);
			}
			requestJSON= new JSONObject();
			requestJSON.putOpt("body", new JSONObject().put("messageSegments", msg));
			if (attachment != null)
			{
				requestJSON.putOpt("attachment", attachment);
			}
			bodyString = requestJSON.toString();
			NotepriseLogger.logMessage("Json"+requestJSON);			
		}  
		catch (JSONException e) 
		{
			e.printStackTrace();
		} 
	
		return bodyString;
	}

 Please suggest some solution for it.

Thanks

I want to add image to the content which  I am  posting to chatter.

I am using chatter rest api but not able to do it..

 

Any kind of help will be greatful.

 

I am using below code for uploading image to chatter post using restapi.

if (imageFile != null && fileName != null && imageTitle != null)
				{
					String url = salesforceRestClient.getClientInfo().instanceUrl + "/services/data/" + SF_API_VERSION + "/chatter/feeds/news/me/feed-items";
					HttpClient httpclient = new DefaultHttpClient();
					HttpPost httppost ;
					MultipartEntity reqEntity ;
					httppost = new HttpPost(url);
					reqEntity = new MultipartEntity(
							HttpMultipartMode.BROWSER_COMPATIBLE);
					FileBody bin = new FileBody(imageFile);
					reqEntity.addPart("feedItemFileUpload", bin);
					reqEntity.addPart("fileName", new StringBody(fileName, "text/html", Charset.defaultCharset()));
					reqEntity.addPart("text", new StringBody(noteContent, "text/html", Charset.defaultCharset()));
					reqEntity.addPart("feedItemFileUpload", new FileBody(imageFile, fileName, "application/octet-stream", Charset.defaultCharset().toString()));
					httppost.setEntity(reqEntity);
					httppost.setHeader("Authorization", "OAuth " + salesforceRestClient.getAuthToken());
					publishResponse = salesforceRestClient.sendSync(RestMethod.POST, url,reqEntity);
					NotepriseLogger.logMessage("responseBody"+publishResponse);
//					HttpResponse response = httpclient.execute(httppost);
//					HttpEntity resEntity = response.getEntity();

 Thanks

 

 

I have successfully able to post image to my own chatter wall but when I am posting it to user wall, it is giving me error. I am not able to take http request log some how.

 

Below is the code which I am using:

PostMethod postMethod = null;  

String stringBody = generateJSONBodyForChatterFeed(noteContent, selectedIds, null, fileName, imageTitle);      
String url = salesforceRestClient.getClientInfo().instanceUrl + "/services/data/" + SF_API_VERSION + "/chatter/feeds/news/me/feed-items";

Part[] parts = {
                                        new StringPart("desc", "Description"),
                                        new StringPart("fileName", fileName),
                                        new StringPart("text", noteContent),                                                                                
                                        new FilePart("feedItemFileUpload", imageFile),
                                    };
                        postMethod = new PostMethod(url);
                        postMethod.setRequestEntity(new MultipartRequestEntity(parts, postMethod.getParams()));

postMethod.setRequestHeader("Authorization", "OAuth " + salesforceRestClient.getAuthToken());                                
                    org.apache.commons.httpclient.HttpClient client = new org.apache.commons.httpclient.HttpClient();

 


 

Here is generateJSONBodyForChatterFeed method code:

 

public static String generateJSONBodyForChatterFeed(String content, ArrayList<String> mentionIds, String imageDescription, String fileName, String imageTitle)
    {
        JSONArray msg = new JSONArray();
        String bodyString = null;
        JSONObject requestJSON=null;
        try
        {            
            if (mentionIds != null)
            {
                for (int i = 0; i < mentionIds.size(); i++)
                {
                    JSONObject mention = new JSONObject();                
                    mention.put("type", "mention");
                    mention.put("id", mentionIds.get(i));
                    msg.put(mention);
                }                
            }
            if (content != null)
            {
                JSONObject text = new JSONObject();
                text.put("type", "text");
                
                text.put("text", " " + content);
                msg.put(text);
            }    
            JSONObject attachment = null;
            if (fileName != null && imageTitle != null)
            {
                attachment = new JSONObject();
                attachment.putOpt("desc", imageDescription);
                attachment.putOpt("fileName", fileName);
                attachment.putOpt("title", imageTitle);
            }
            requestJSON= new JSONObject();
            requestJSON.putOpt("body", new JSONObject().put("messageSegments", msg));
            if (attachment != null)
            {
                requestJSON.putOpt("attachment", attachment);
            }
            bodyString = requestJSON.toString();
            NotepriseLogger.logMessage("Json"+requestJSON);            
        }  
        catch (JSONException e)
        {
            e.printStackTrace();
        }
    
        return bodyString;
    }

 

 

Please let me know what mistake I am doing here. When I add bosy part , it is just skipping that and posting the image to my own wall instead of user.

 

Thanks

I am trying to post on chatter wall,chatter user, chatter group.

 

When I posting on chatter wall, I am sending 1000 character but it is taking less than 1000 character and posting on chatter wall.

 

When I am posting on chatter user and selecting the multiple user than it is posting random character there and giving below error:

08-30 18:31:17.867: V/Noteprise(3238): Response for user[{"message":"Text is too large.","errorCode":"STRING_TOO_LONG"}]
08-30 18:31:17.867: V/Noteprise(3238): [{"message":"Text is too large.","errorCode":"STRING_TOO_LONG"}]

  I am using chatter rest api with latest salesforce Android Sdk.

 

How to calculate the length of character which we can post on the basic of number of user. There may be multiple user which I can select and post chatter .

 

On what factor content length depends like user name or something else?

 

I am using below code :

public static RestResponse publishNoteWithUserMentions(RestClient salesforceRestClient, String noteContent, String SF_API_VERSION, ArrayList<String> selectedIds)
	{
		RestResponse publishResponse = null;
		if (salesforceRestClient != null)
		{
			try 
			{				
				StringEntity stringEntity = new StringEntity(generateJSONBodyForChatterFeed(noteContent, selectedIds));
				stringEntity.setContentType("application/json");
				String url = "/services/data/" + SF_API_VERSION + "/chatter/feeds/news/me/feed-items";
				NotepriseLogger.logMessage(url);
				publishResponse = salesforceRestClient.sendSync(RestMethod.POST, url, stringEntity);
			} 
			catch (UnsupportedEncodingException e) 
			{
				NotepriseLogger.logError("UnsupportedEncodingException while publishing chatter feed.", NotepriseLogger.ERROR, e);
			} 
			catch (IOException e) 
			{
				NotepriseLogger.logError("IOException while publishing chatter feed.", NotepriseLogger.ERROR, e);
			}	
		}
		return publishResponse;
	}
	
	public static String generateJSONBodyForChatterFeed(String content, ArrayList<String> mentionIds)
	{
		JSONArray msg = new JSONArray();
		String bodyString = null;
		try 
		{			
			if (mentionIds != null)
			{
				for (int i = 0; i < mentionIds.size(); i++)
				{
					JSONObject mention = new JSONObject();				
					mention.put("type", "mention");
					mention.put("id", mentionIds.get(i));
					msg.put(mention);
				}				
			}
			if (content != null)
			{
				content = URLEncoder.encode(content, "UTF-8");
				JSONObject text = new JSONObject();
				text.put("type", "text");
				text.put("text", " " + content);
				msg.put(text);
			}			
			JSONObject body = new JSONObject();
			body.putOpt("body", new JSONObject().put("messageSegments", msg));
			bodyString = body.toString();				
			NotepriseLogger.logMessage(bodyString);			
		}  
		catch (JSONException e) 
		{
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return bodyString;
	}

 I will appreciate any kind of help.

Thanks

I need java code to post a image to chatter user using rest api.

 

I am using below code but it is not working for me:

Part[] parts = { 
									//new StringPart("desc", "Description"),
									new StringPart("body",stringBody),
									//new StringPart("fileName", fileName),
									//new StringPart("text", noteContent),																				
									new FilePart("feedItemFileUpload", imageFile),
								};
				    postMethod = new PostMethod(url);
					postMethod.setRequestEntity(new MultipartRequestEntity(parts, postMethod.getParams()));

postMethod.setRequestHeader("Authorization", "OAuth " + salesforceRestClient.getAuthToken());
				postMethod.setRequestHeader("Content-type", "multipart/form-data");
				org.apache.commons.httpclient.HttpClient client = new org.apache.commons.httpclient.HttpClient();
				int code =client.executeMethod(postMethod);


public static String generateJSONBodyForChatterFeed(String content, ArrayList<String> mentionIds, String imageDescription, String fileName, String imageTitle)
	{
		JSONArray msg = new JSONArray();
		String bodyString = null;
		JSONObject requestJSON=null;
		try 
		{			
			if (mentionIds != null)
			{
				for (int i = 0; i < mentionIds.size(); i++)
				{
					JSONObject mention = new JSONObject();				
					mention.put("type", "mention");
					mention.put("id", mentionIds.get(i));
					msg.put(mention);
				}				
			}
			if (content != null)
			{
				JSONObject text = new JSONObject();
				text.put("type", "text");
				
				text.put("text", " " + content);
				msg.put(text);
			}	
			JSONObject attachment = null;
			if (fileName != null && imageTitle != null)
			{
				attachment = new JSONObject();
				attachment.putOpt("desc", imageDescription);
				attachment.putOpt("fileName", fileName);
				attachment.putOpt("title", imageTitle);
			}
			requestJSON= new JSONObject();
			requestJSON.putOpt("body", new JSONObject().put("messageSegments", msg));
			if (attachment != null)
			{
				requestJSON.putOpt("attachment", attachment);
			}
			bodyString = requestJSON.toString();
			NotepriseLogger.logMessage("Json"+requestJSON);			
		}  
		catch (JSONException e) 
		{
			e.printStackTrace();
		} 
	
		return bodyString;
	}

 Please suggest some solution for it.

Thanks

I want to add image to the content which  I am  posting to chatter.

I am using chatter rest api but not able to do it..

 

Any kind of help will be greatful.

 

I am using below code for uploading image to chatter post using restapi.

if (imageFile != null && fileName != null && imageTitle != null)
				{
					String url = salesforceRestClient.getClientInfo().instanceUrl + "/services/data/" + SF_API_VERSION + "/chatter/feeds/news/me/feed-items";
					HttpClient httpclient = new DefaultHttpClient();
					HttpPost httppost ;
					MultipartEntity reqEntity ;
					httppost = new HttpPost(url);
					reqEntity = new MultipartEntity(
							HttpMultipartMode.BROWSER_COMPATIBLE);
					FileBody bin = new FileBody(imageFile);
					reqEntity.addPart("feedItemFileUpload", bin);
					reqEntity.addPart("fileName", new StringBody(fileName, "text/html", Charset.defaultCharset()));
					reqEntity.addPart("text", new StringBody(noteContent, "text/html", Charset.defaultCharset()));
					reqEntity.addPart("feedItemFileUpload", new FileBody(imageFile, fileName, "application/octet-stream", Charset.defaultCharset().toString()));
					httppost.setEntity(reqEntity);
					httppost.setHeader("Authorization", "OAuth " + salesforceRestClient.getAuthToken());
					publishResponse = salesforceRestClient.sendSync(RestMethod.POST, url,reqEntity);
					NotepriseLogger.logMessage("responseBody"+publishResponse);
//					HttpResponse response = httpclient.execute(httppost);
//					HttpEntity resEntity = response.getEntity();

 Thanks

 

 

I have successfully able to post image to my own chatter wall but when I am posting it to user wall, it is giving me error. I am not able to take http request log some how.

 

Below is the code which I am using:

PostMethod postMethod = null;  

String stringBody = generateJSONBodyForChatterFeed(noteContent, selectedIds, null, fileName, imageTitle);      
String url = salesforceRestClient.getClientInfo().instanceUrl + "/services/data/" + SF_API_VERSION + "/chatter/feeds/news/me/feed-items";

Part[] parts = {
                                        new StringPart("desc", "Description"),
                                        new StringPart("fileName", fileName),
                                        new StringPart("text", noteContent),                                                                                
                                        new FilePart("feedItemFileUpload", imageFile),
                                    };
                        postMethod = new PostMethod(url);
                        postMethod.setRequestEntity(new MultipartRequestEntity(parts, postMethod.getParams()));

postMethod.setRequestHeader("Authorization", "OAuth " + salesforceRestClient.getAuthToken());                                
                    org.apache.commons.httpclient.HttpClient client = new org.apache.commons.httpclient.HttpClient();

 


 

Here is generateJSONBodyForChatterFeed method code:

 

public static String generateJSONBodyForChatterFeed(String content, ArrayList<String> mentionIds, String imageDescription, String fileName, String imageTitle)
    {
        JSONArray msg = new JSONArray();
        String bodyString = null;
        JSONObject requestJSON=null;
        try
        {            
            if (mentionIds != null)
            {
                for (int i = 0; i < mentionIds.size(); i++)
                {
                    JSONObject mention = new JSONObject();                
                    mention.put("type", "mention");
                    mention.put("id", mentionIds.get(i));
                    msg.put(mention);
                }                
            }
            if (content != null)
            {
                JSONObject text = new JSONObject();
                text.put("type", "text");
                
                text.put("text", " " + content);
                msg.put(text);
            }    
            JSONObject attachment = null;
            if (fileName != null && imageTitle != null)
            {
                attachment = new JSONObject();
                attachment.putOpt("desc", imageDescription);
                attachment.putOpt("fileName", fileName);
                attachment.putOpt("title", imageTitle);
            }
            requestJSON= new JSONObject();
            requestJSON.putOpt("body", new JSONObject().put("messageSegments", msg));
            if (attachment != null)
            {
                requestJSON.putOpt("attachment", attachment);
            }
            bodyString = requestJSON.toString();
            NotepriseLogger.logMessage("Json"+requestJSON);            
        }  
        catch (JSONException e)
        {
            e.printStackTrace();
        }
    
        return bodyString;
    }

 

 

Please let me know what mistake I am doing here. When I add bosy part , it is just skipping that and posting the image to my own wall instead of user.

 

Thanks

Hello ,

 

I have a requrement wherein a Blob field in a custom object (storing the user photo) needs to be displayed as chatter user profile photo.

Currently , the User smallphotourl field is used to display the chatter user profile photo. How can this be updated to use my custom photo?

 

Its a critical requirement. Response is appreciated!

 

Thanks

anjs

 

Titanium下でChatter REST APIを使用して写真を投稿するプログラムを作成しようと

しておりますが、投稿時にエラーとなってしまい原因が分からずに困っています。

 

どなたか原因のお分かりになる方はいらっしゃいませんでしょうか。

 

 

リクエストはChatter REST API Developer's GuideのChapter 4 Chatter API Resources

のページの最後の例(PDF版では37〜38ページ)に基づいています。


まず、以下の通りリクエストヘッダーをセットします。

※※※以下リクエストヘッダー※※※

var xhr = Ti.Network.createHTTPClient();

xhr.setRequestHeader('Authorization','OAuth ' + _header);

xhr.setRequestHeader('X-PrettyPrint','true');

xhr.setRequestHeader('Accept','application/json');

xhr.setRequestHeader('Content-Type','multipart/form-data, boundary=-----------------abcdefg0123456789wwwwxxxxyyyyzzzz');

※※※以上リクエストヘッダー※※※



次にxhr.send()を使用して以下の文字列を送信しています。


※※※以下リクエスト内容※※※

-----------------abcdefg0123456789wwwwxxxxyyyyzzzz

Content-Disposition: form-data; name="testdata"

Content-Type: text/plain


this is a #testdata

-----------------abcdefg0123456789wwwwxxxxyyyyzzzz

Content-Disposition: form-data; name="testdata"

Content-Type: application/json


{

    "body" : {

        "messageSegments" : [{

            "type": "Text",

            "text" : "Good quarter everyone!"

        }]

    },

    "attachment" : {

        "desc" : "Q2 progress report",

        "fileName" : "2011Q2ProgressReport.txt"

    }

}

-----------------abcdefg0123456789wwwwxxxxyyyyzzzz

※※※以上リクエスト内容※※※



その結果、サーバーからは以下のレスポンスが返ってきます。


※※※以下レスポンス※※※

[ {

    "message" : "Creating a text post requires text",

    "errorCode" : "MISSING_ARGUMENT"

} ]

※※※以上レスポンス※※※


以上です。

よろしくお願いいたします。