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
jbowesjbowes 

Find current user via REST api

I'm using OAuth and the REST API from a Rails app, and would like to query the User record for the currently logged in user. I don't see anything in the documentation about this, does anyone know if it's possible to either get info on the currently logged in user?

SuperfellSuperfell

Part of the oauth response is an Id field, this is a URL to the user's identity service, it contains a number of peices of info, if that's not enough, you can then take the userId from that response and fetch the full user resource.

rjayramrjayram

Hi,

 

     I am also trying to find the current logged in user using the REST API in a SalesForce Hybrid App. I have the session id. In my case, the id field in the OauthCredentials response shows up as undefined. I can only see the AccessToken and the instanceURL fields in the OAuth response. I just need the current logged in username. Can someone please help?

 

 

Thanks you.

jbowesjbowes

I ended up having to authenticate the user again using the session id in order to get the full oauth response with the User Id, and then in some cases querying that user yet again to get more values from the User record. It's not very efficient but I haven't seen a better way.

rjayramrjayram

Hi,

 

         Thank you for your reply. I was wondering if you have any source code to pass onto me. Did you re-authenticate using the Rest API or the forcetk client? This is kind of an urgent issue, so any help will be greatly appreciated.

 

Thanks,

Jayram

jbowesjbowes

I guess I used the databasedotcom Ruby gem for this. The readme in the github repo has an example of using its authenticate method with an oauth token that you got somewhere else, but you can use the session id the same way. It would look something like this:

 

client.authenticate :token => "session-id", :instance_url => "http://na1.salesforce.com"  #=> "my-oauth-token"
The client object will have a user_id attribute that you can use to get other info. You might want to look through the issues on the github repo before using in production. I imagine that there must be a better way; if you find it I'd love to hear about it.