Where did you get your accessToken from? If you used OAuth to establish the session then you will need to have the web
OAuth scope to use the sessionid for what is essentially a screen scraping web request. Having the api
scope would be sufficient for the Partner API, but for for mocking UI requests.
Other than that, as you have found, you need to put the sessionId/accessToken into a sid
cookie along with the request.
You can check your OAuth Scopes under
Setup > App Setup > Create > Apps > [App Name].
It should appear next to Selected OAuth Scopes.
When calling /services/oauth2/authorize
I'm using response_type=Token
. I'm also passing a scope query string parameter that includes web
. So I've specified the web scope on both the App and when initiating the OAuth process.
It appears you are using the Web Server OAuth Authentication Flow (based on the response_type=code). The scope parameter is applicable here as well.
scope
Specifies what data your application can access. See “Scope Parameter Values” in the online help for more information.
So you try modifying the authUrl line:
authUrl = environment + "/services/oauth2/authorize?response_type=code&client_id=" + clientId +
"&redirect_uri=" + URLEncoder.encode(redirectUri, "UTF-8") +
"&scope=full%20web";
Once you have a sessionId/accessToken, you can verify that it is valid for a web session in a browser using
https://<instanceUrl>/secur/frontdoor.jsp?sid=<accessToken>"
This should bounce you into a browser session with the sid cookie set. If it doesn't work, the session isn't valid for the web, which you will need to screenscrape an image via servlet/rtaImage
.
Final solution (By @GoldenAxe), that is only an example to check that the image fetching is working correctly, of-course it can be done much neatly.
In the WelcomeServlet.doGet:
String accessToken = (String) request.getSession().getAttribute(ACCESS_TOKEN);
String apiEndPoint = (String) request.getSession().getAttribute(INSTANCE_URL);
CookieManager cookieManager = new CookieManager();
cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
CookieHandler.setDefault(cookieManager);
URL url = null;
try
{
url = new URL(apiEndPoint + "/secur/frontdoor.jsp?sid=" + accessToken);
// open's a connection with the url specified and returns URLConnection object
URLConnection urlConnection = url.openConnection();
// get's the contents from this url specifies
urlConnection.getContent();
}
catch (MalformedURLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
String sid = "";
// returns the cookie store(bunch of cookies)
CookieStore cookieStore = cookieManager.getCookieStore();
// getting cookies which returns in the form of List of type HttpCookie
List<HttpCookie> listOfcookies = cookieStore.getCookies();
for (HttpCookie httpCookie : listOfcookies)
{
System.out.println("Cookie Name : " + httpCookie.getName() + " Cookie Value : " + httpCookie.getValue());
if (httpCookie.getName().compareTo("sid") == 0)
{
sid = httpCookie.getValue();
}
}
String urlString = "https://c.eu0.content.force.com/servlet/rtaImage?eid=ka3200000004MwH&feoid=00N20000008fSIK&refid=0EM20000000TzfR";
HttpClient client = new HttpClient();
GetMethod method = new GetMethod();
method.setRequestHeader("Cookie", "sid=" + sid);
method.setURI(new URI(urlString, true));
int returnCode = client.executeMethod(method);
if (returnCode != HttpStatus.SC_OK)
{
System.err.println("Unable to fetch image, status code: " + returnCode);
}
InputStream imageData = method.getResponseBodyAsStream();
OutputStream out = new BufferedOutputStream(new FileOutputStream("test-image.jpg"));
for (int i; (i = imageData.read()) != -1;)
{
out.write(i);
}
imageData.close();
out.close();
method.releaseConnection();
I'm fairly sure that the problem here is that when you try to access the image on the email you are using the Salesforce internal link (the one that requires authentication).
When rendering PDFs you don't get this issue because the images are embedded into the file, so no authentication is required to view them.
Similar issue faced here, with rendering MS Word documents.
You could also:
- Make the Visualforce publicly available, and give the guest user access to the data displayed (careful with this, you might want to provide some kind of authentication for your users to see the data).
- Host the images in another service, and refer to their links in your field. Since the Rich Text Area field is just plain HTML, you can modify the image link to point to, say, Imgur or a AWS S3 bucket instance.
Best Answer
Rich fields are stored as HTML in Salesforce, so to retrieve your image, you will have to parse the text and get the image from there. Luckily this is a fairly simple task to do.
Once queried, the field will look like this in the variable:
What you want is what is in the
src
attribute of theimg
tag. You can just copy the tag, or get the link and remake it in your page (using a repeat attribute or something).Here's a sample code on how you would do this (I used a Developer Edition organization with a object from Trailhead):