function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
tanujatanuja 

Chatter API File Upload Error 401

 We are using REST API to upload file to Chatter from .net application

/chatter/feeds/news/me

 

We are getting error 401 UnAuthorized..

 

Please advise

 

 

SuperfellSuperfell

Did you include the Authorization header in the request? 

tanujatanuja

We haave added Auth in Header

 

from Chatter REST API File Upload Error : Chatter REST API File Upload Bad request error

Follwing is our api..We were succesfuly in posting content,comments,links etc but file upload has error

 

Thomas CookThomas Cook

You have to make a form post to get this to work; you can't just make an HTTP GET passing the file name in the query string.

 

To illustrate, here's how you'd do it in curl (the options are put on different lines for readability):

 

 

curl --form feedItemFileUpload=@/home/me/test.txt
     --form fileName=contentFile
     --form "desc=This is a test file that I'm posting." 
     --form "text=I love posting files to Chatter!" 
     --header 'Authorization: OAuth 00Dx0000000X42V!AQgAQClp6UqGWNJUf4d3LQkVCysBIS6afWAGodgODJ.o1XQkE_fAnZIv5gzfRXxSf_D.28fsauDdZVE1nQU9lMqQSfOJTtb3' 
     https://na1.salesforce.com/services/data/v22.0/chatter/feeds/user-profile/me/feed-items

 

 

And here's how you'd do it in Java:

 

 

import java.io.File;

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

...

String oauthToken = "00Dx0000000X42V!AQgAQClp6UqGWNJUf4d3LQkVCysBIS6afWAGodgODJ.o1XQkE_fAnZIv5gzfRXxSf_D.28fsauDdZVE1nQU9lMqQSfOJTtb3";
String url = "https://na1.salesforce.com/services/data/v22.0/chatter/feeds/user-profile/me/feed-items";
String text = "I love posting files to Chatter!";
File contentFile = getFile();
String desc = "This is a test file that I'm posting.";
String fileName = "contentFile";
Part[] parts = {
        new StringPart("desc", desc),
        new StringPart("fileName", fileName),
        new StringPart("text", text),
        new FilePart("feedItemFileUpload", contentFile),
};

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);
    int returnCode = httpClient.executeMethod(postMethod);
    System.out.println(postMethod.getResponseBodyAsString());
    assertTrue("Expected return code of: " + HttpStatus.SC_CREATED, returnCode == HttpStatus.SC_CREATED);
} finally {
    postMethod.releaseConnection();
}

 

 

Thomas CookThomas Cook

Note in the curl example that the '@' in

 

 

curl --form feedItemFileUpload=@/home/me/test.txt 
...

 

is required -- see the curl doc at http://curl.haxx.se/docs/ for more info.