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
VenkatGPVenkatGP 

SF Access Token

Hi,
 
I am trying to create a User on SalesForce programmatically using the SF RESTful API's. So for the same, I submit a http post request to the URL https://login.salesforce.com/services/oauth2/authorize. I also pass the required parameters like username, password, response_type, client_id and redirect_uri. I am able to successfully login, but SF responds with the "code" by re-directing to the callback URL configured in the Remote Access, which means my program does not directly receive the code by a response.
 
The approach I could think of is as follows:-
1. Whenever my program gets a request to create User on SF, it will try to retrieve the access token from my local cache.
2. If access token is not found in cache, then my program is put on hold. I would do a programmatic login as described above and have a RESTful API exposed on my server (which is accessible by SF). The callback URL is pointed to this RESTful API. So my RESTful API receives the code from SF and retrives the access token using SF's RESTful API. The retrieved access token is cached in my local cache which can be re-used for further RESTful API access until it times out.
3. The program has to wait and will resume on retrieving the access token and the User is created on SF using RESTful API.
 
Can someone please let me know if the same can be achieved in a better way or if SF already has some mechanism which I could use.
Thanks in advance.

 

Regards,
VenkatGP

Best Answer chosen by Admin (Salesforce Developers) 
Pat PattersonPat Patterson

Hi Venkat - it sounds like you might have something wrong in the OAuth request - it should look like this:

 

grant_type=password&client_id=<your_client_id>&client_secret=<your_client_secret>&username=<your_username>&password=<your_password>

See http://wiki.developerforce.com/index.php/Digging_Deeper_into_OAuth_2.0_on_Force.com#Obtaining_a_Token_in_an_Autonomous_Client_.28Username-Password_Flow.29

 

Cheers,

 

Pat

 

 

 

All Answers

Pat PattersonPat Patterson

Hi Venkat - it sounds like you might have something wrong in the OAuth request - it should look like this:

 

grant_type=password&client_id=<your_client_id>&client_secret=<your_client_secret>&username=<your_username>&password=<your_password>

See http://wiki.developerforce.com/index.php/Digging_Deeper_into_OAuth_2.0_on_Force.com#Obtaining_a_Token_in_an_Autonomous_Client_.28Username-Password_Flow.29

 

Cheers,

 

Pat

 

 

 

This was selected as the best answer
VenkatGPVenkatGP

Thanks Pat,

 

With your suggestion and the following changes, I am now able to retrive the access token:-

1. Changed my POST URI from https://login.salesforce.com/services/oauth2/authorize to https://login.salesforce.com/services/oauth2/token

2. Appended my password with Security token. I hadn't done this earlier.

 

Thank you once again.

 

Regards,

VenkatGP