I am dragging my hair out over this one right now. I have followed the following example:
http://developer.force.com/cookbook/recipe/interact-with-the-forcecom-rest-api-from-php
but here the user is being sent to the login form and needs to login. Instead I would like to post that data in my code and not have the user login but my app do that automatically.
If anyone could post an example on how to do that with oAuth I would really appreciate it as I am not eager on using that bloated SOAP implementation.
Cheers guys!
It seems after some more tinkering my attempts have been a success::
$loginurl = "https://login.salesforce.com/services/oauth2/token";
$params = "grant_type=password"
. "&client_id=" . CLIENT_ID
. "&client_secret=" . CLIENT_SECRET
. "&username=" . USER_NAME
. "&password=" . PASSWORD;
$curl = curl_init($loginurl);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
$json_response = curl_exec($curl);
$status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ( $status != 200 ) {
die("Error: call to URL failed with status $status, response $json_response, curl_error " . curl_error($curl) . ", curl_errno " . curl_errno($curl));
}
curl_close($curl);
echo $json_response;
Now all that is left to do is store the access_token & instance_url from that response into a session var and work away on our objects.
I hope the above helps someone else with similar issues.
Answer for 2018:
https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/intro_understanding_username_password_oauth_flow.htm
Make an POST request with:
grant_type
: Must be password
for this authentication flow.client_id
: The Consumer Key from the connected app definition.client_secret
: The Consumer Secret from the connected app definition. Required unless the Require Secret for Web Server Flow setting is not enabled in the connected app definition.username
: End-user’s username.password
: End-user’s password.Example:
grant_type=password&client_id=3MVG9lKcPoNINVBIPJjdw1J9LLM82Hn
FVVX19KY1uA5mu0QqEWhqKpoW3svG3XHrXDiCQjK1mdgAvhCscA9GE&client_secret=
1955279925675241571&username=testuser%40salesforce.com&password=mypassword123456
See above link for more details.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With