Are you sure the rich text field is visible to the site profile? It's not included in the regular list, you have to go to the site settings and access the profile from there where you can then check whether the field level security is good. I assume it is since you have the URL , but then I don't know why you can't access the image. Have you tried using straight tags instead of using the Visualforce image tag?
We use rich text fields for our website and that displays images from the same server without problems; the image below is accessed via this URL:
http://www.spkeasey.com/servlet/rtaImage?eid=a0Fi0000000BX4l&feoid=00Ni0000000GnlW&refid=0EMi00000008S3k

I suspect that you might need to modify the domain part of your image URL to use that of your site, as it is the URL will be for internal access and the site user wouldn't have access to it. You can get the site base URL using: {!$Site.CustomWebAddress}
, for example I get the URL of static resources on our site like so:
{!$Site.CustomWebAddress}{!$Resource.ResourceName}"
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();
Best Answer
Its better to save base64 encoded value of
Screenshot__c
field in new field created inmyCustomObject__c
object so that you can easily get encoded value and pass to visualforce page.For encoding Screenshot__c use below code and do a mass update all records:
After that update your query as
Update visualforce code as
Or another approach you can implement is store your screenshot__c as attachment for myCustomObject__c record and implement as discussed here.
Display Base64 data on a Visualforce page
Render blob image in visualforce page
Edit:
You cannot query attachment body field in join queries. You need another soql to get that.
Get Attachments in separate query in a Map.
In vf page: