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
Maciej StasielukMaciej Stasieluk 

Access contact photo with HTTP request using oauth access token

I have a valid access token (from oauth login flow) and Contact PhotoUrl value (from a REST API call).

Is there a way to combine this two and construct a HTTP browser request that could actually allow me to fetch the image?

As per documentation, PhotoUrl returns a path to be combined with the URL of a Salesforce instance (which I also have) to generate a URL to request the profile image associated with the contact. Generated URL returns an HTTP redirect (code 302).

Everything works as long as user is logged into salesforce in the same browser session. But once the session is out, this generated URL returns 401 error instead of 302 redirect. Because I have a valid oauth access token (from some out of band way) I could pass it with the request to fetch the image.

I found some undocumented property of this PhotoUrl - if you append ?oauth_token=<valid access token> to it you will actually make the redirect and get final image url, e.g. something like https://yourInstance.salesforce.com/profilephoto/005/T

But browser cannot fetch this image, because the session is still out. Simple https://yourInstance.salesforce.com/profilephoto/005/T?oauth_token=<valid access token> would do the trick, but all query params are getting stripped during 302 redirect, and as of my knowledge there is no way to get value after the redirect from the browser.

Does anyone have any idea how browser could fetch actual image using data I mention?
 

Best Answer chosen by Maciej Stasieluk
JLA.ovhJLA.ovh
Imagine you get the URL of your photo at this location when authenticated :
https://c.eu1.content.force.com/profilephoto/729D00000009Lfu/T
Your access token is a valid session ID. You can use it in the frontdoor URL to redirect to the image
use the "sid" parameter for your session ID and retURL for the targete profile picture
Use your own POD for this special URL as following example :
https://eu1.salesforce.com/secur/frontdoor.jsp?sid=00DD0000000xxxL!AQsxxxUyC.re5OnPBdHXUjeZHxxxjEWDbbXIvF6Hy2nwBRnmC2jIlrag22cdrcRwyGR0jm11AxhsYlijCjCNvixxxK4HA8io&retURL=https://c.eu1.content.force.com/profilephoto/729D00000009Lfu/T
In this example : eu1.salesforce.com is my POD, sid parameter is hosting a (altered) session ID and retURL is pointing to my profile picture.
If I copy/paste this in the browser when unauthenticated I can display the picture (and of course I am then authenticated with the related session)

 

All Answers

JLA.ovhJLA.ovh
Imagine you get the URL of your photo at this location when authenticated :
https://c.eu1.content.force.com/profilephoto/729D00000009Lfu/T
Your access token is a valid session ID. You can use it in the frontdoor URL to redirect to the image
use the "sid" parameter for your session ID and retURL for the targete profile picture
Use your own POD for this special URL as following example :
https://eu1.salesforce.com/secur/frontdoor.jsp?sid=00DD0000000xxxL!AQsxxxUyC.re5OnPBdHXUjeZHxxxjEWDbbXIvF6Hy2nwBRnmC2jIlrag22cdrcRwyGR0jm11AxhsYlijCjCNvixxxK4HA8io&retURL=https://c.eu1.content.force.com/profilephoto/729D00000009Lfu/T
In this example : eu1.salesforce.com is my POD, sid parameter is hosting a (altered) session ID and retURL is pointing to my profile picture.
If I copy/paste this in the browser when unauthenticated I can display the picture (and of course I am then authenticated with the related session)

 
This was selected as the best answer
Maciej StasielukMaciej Stasieluk
Its working like a charm, thank you!

Just want to add for anybody that will stumble upon this in the future: for this to work your access token must have "web" scope.
Ajay choudharyAjay choudhary
Hey! I am trying the same thing but the page is reloading again and again and asking for login
I tried in poershell and when I download the image it is containing useless html code, can you help me with it?