Skip to main content
removed thank you zig
Source Link
Saariko
  • 5.7k
  • 14
  • 81
  • 169

Thanks

Thanks

Thanks

Thanks

Tweeted twitter.com/#!/StackSalesforce/status/247667078293757952
added 2849 characters in body
Source Link

I am trying below code to post image on user chatter wall

if (imageFile != null && fileName != null && imageTitle != null)
            {
                            
                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 StringPart("body", generateJSONBodyForChatterFeed(noteContent, selectedIds, null, fileName, imageTitle)),                                       
                                    new FilePart("feedItemFileUpload", imageFile),
                                };
                //String url = salesforceRestClient.getClientInfo().instanceUrl + "/services/data/" + SF_API_VERSION + "/chatter/feeds/news/me/feed-items";
                NotepriseLogger.logMessage(url);
                PostMethod postMethod = new PostMethod(url);
                postMethod.setRequestEntity(new MultipartRequestEntity(parts, postMethod.getParams()));
                postMethod.setRequestHeader("Authorization", "OAuth " + salesforceRestClient.getAuthToken());
                //postMethod.setr(generateJSONBodyForChatterFeed(noteContent, null, null, null, null));
                //postMethod.setRequestHeader("Content-type", "multipart/form-data");
                //postMethod.addRequestHeader("X-PrettyPrint", "1");                    
                org.apache.commons.httpclient.HttpClient client = new org.apache.commons.httpclient.HttpClient();
                client.executeMethod(postMethod);
                String responseBody = postMethod.getResponseBodyAsString();
                NotepriseLogger.logMessage(responseBody);

generateJSONBodyForChatterFeed() method returning json String to me.

Here is the code of this method:

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)
        {
            JSONObject text = new JSONObject();
            text.put("type", "text");
            //content = URLEncoder.encode(content, "UTF-8");
            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);
        }
        JSONObject requestJSON = new JSONObject();
        requestJSON.putOpt("body", new JSONObject().put("messageSegments", msg));
        if (attachment != null)
        {
            requestJSON.putOpt("attachment", attachment);
        }
        bodyString = requestJSON.toString();
        NotepriseLogger.logMessage(bodyString);

    

Thanks

I am trying below code to post image on user chatter wall

if (imageFile != null && fileName != null && imageTitle != null)
            {
                            
                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 StringPart("body", generateJSONBodyForChatterFeed(noteContent, selectedIds, null, fileName, imageTitle)),                                       
                                    new FilePart("feedItemFileUpload", imageFile),
                                };
                //String url = salesforceRestClient.getClientInfo().instanceUrl + "/services/data/" + SF_API_VERSION + "/chatter/feeds/news/me/feed-items";
                NotepriseLogger.logMessage(url);
                PostMethod postMethod = new PostMethod(url);
                postMethod.setRequestEntity(new MultipartRequestEntity(parts, postMethod.getParams()));
                postMethod.setRequestHeader("Authorization", "OAuth " + salesforceRestClient.getAuthToken());
                //postMethod.setr(generateJSONBodyForChatterFeed(noteContent, null, null, null, null));
                //postMethod.setRequestHeader("Content-type", "multipart/form-data");
                //postMethod.addRequestHeader("X-PrettyPrint", "1");                    
                org.apache.commons.httpclient.HttpClient client = new org.apache.commons.httpclient.HttpClient();
                client.executeMethod(postMethod);
                String responseBody = postMethod.getResponseBodyAsString();
                NotepriseLogger.logMessage(responseBody);

generateJSONBodyForChatterFeed() method returning json String to me.

Here is the code of this method:

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)
        {
            JSONObject text = new JSONObject();
            text.put("type", "text");
            //content = URLEncoder.encode(content, "UTF-8");
            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);
        }
        JSONObject requestJSON = new JSONObject();
        requestJSON.putOpt("body", new JSONObject().put("messageSegments", msg));
        if (attachment != null)
        {
            requestJSON.putOpt("attachment", attachment);
        }
        bodyString = requestJSON.toString();
        NotepriseLogger.logMessage(bodyString);

    

Thanks

appended answer 1343 as supplemental
Source Link

I tried below method as well and getting this error "09-11 16:36:42.510: W/HttpMethodBase(4399): Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is recommended. 09-11 16:36:42.900: V/Noteprise(4399): [ { 09-11 16:36:42.900: V/Noteprise(4399): "message" : "Creating a text post requires text", 09-11 16:36:42.900: V/Noteprise(4399): "errorCode" : "MISSING_ARGUMENT" 09-11 16:36:42.900: V/Noteprise(4399): } ]returncode400 "

String url = "https://ap1.salesforce.com/services/data/v25.0/chatter/" +
                              "feeds/user-profile/me/feed-items";
                String text = "I love posting files to Chatter!";
                
                String desc = "This is a test file that I'm posting.";
                
                Part[] parts = {
                        new StringPart("desc", desc),
                        new StringPart("fileName", fileName),
                        new StringPart("text", text),
                        new FilePart("feedItemFileUpload", imageFile),
                };

                final PostMethod postMethod = new PostMethod(url);
                                
                try {
                    postMethod.setRequestEntity(new MultipartRequestEntity(parts, 
                                                postMethod.getParams()));
                    postMethod.setRequestHeader("Authorization", "Bearer " + salesforceRestClient.getAuthToken());
                    postMethod.setRequestHeader("Content-type", "x-www-form-urlencoded");
                    postMethod.addRequestHeader("X-PrettyPrint", "1");
                    
                    org.apache.commons.httpclient.HttpClient httpClient = new org.apache.commons.httpclient.HttpClient();
                    httpClient.getParams().setSoTimeout(60000);
                    
                    int returnCode = httpClient.executeMethod(postMethod);
                    String responseBody = postMethod.getResponseBodyAsString();
                    NotepriseLogger.logMessage(responseBody+"returncode"+returnCode);
                  
                } finally {
                    postMethod.releaseConnection();
                }

I tried below method as well and getting this error "09-11 16:36:42.510: W/HttpMethodBase(4399): Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is recommended. 09-11 16:36:42.900: V/Noteprise(4399): [ { 09-11 16:36:42.900: V/Noteprise(4399): "message" : "Creating a text post requires text", 09-11 16:36:42.900: V/Noteprise(4399): "errorCode" : "MISSING_ARGUMENT" 09-11 16:36:42.900: V/Noteprise(4399): } ]returncode400 "

String url = "https://ap1.salesforce.com/services/data/v25.0/chatter/" +
                              "feeds/user-profile/me/feed-items";
                String text = "I love posting files to Chatter!";
                
                String desc = "This is a test file that I'm posting.";
                
                Part[] parts = {
                        new StringPart("desc", desc),
                        new StringPart("fileName", fileName),
                        new StringPart("text", text),
                        new FilePart("feedItemFileUpload", imageFile),
                };

                final PostMethod postMethod = new PostMethod(url);
                                
                try {
                    postMethod.setRequestEntity(new MultipartRequestEntity(parts, 
                                                postMethod.getParams()));
                    postMethod.setRequestHeader("Authorization", "Bearer " + salesforceRestClient.getAuthToken());
                    postMethod.setRequestHeader("Content-type", "x-www-form-urlencoded");
                    postMethod.addRequestHeader("X-PrettyPrint", "1");
                    
                    org.apache.commons.httpclient.HttpClient httpClient = new org.apache.commons.httpclient.HttpClient();
                    httpClient.getParams().setSoTimeout(60000);
                    
                    int returnCode = httpClient.executeMethod(postMethod);
                    String responseBody = postMethod.getResponseBodyAsString();
                    NotepriseLogger.logMessage(responseBody+"returncode"+returnCode);
                  
                } finally {
                    postMethod.releaseConnection();
                }
Source Link
Loading