0

I integrated salesforce one of my project for store user contact in Salesforce.

first, i was authenticate using below API link:

https://login.salesforce.com/services/oauth2/authorize?response_type=token&client_id=xxxxxxxxxx&redirect_uri=https://www.example.com/success&state=mystate

it successfully redirects callback URL and given access_token. show below success page url.

https://www.example.com/success#access_token=xxxxxxx&instance_url=https%3A%2F%2Fap5.salesforce.com&id=https%3A%2F%2Flogin.salesforce.com%2Fid%2F00D7F000004981JUAQ%2F0057F000001zY4ZQAU&issued_at=1514990502115&signature=ctEUOf7n%2BY9hKCTywHOXDpWURcu6tlokdfEBtqCDIZs%3D&state=mystate&scope=id+api+web+full+visualforce+openid&token_type=Bearer

at this step perfectly working. but using this access_token call add contact api it's always show session expired error.

Call Contact API code :

    $headers = array(
                'Authorization' => 'Bearer token',
                'Content-Type' => 'application/json'
            );

            // set url
           curl_setopt($ch, CURLOPT_URL, "https://ap5.salesforce.com/services/data/v38.0/sobjects/Contact/");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_HEADER, true);
//        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
        curl_setopt($ch, CURLOPT_POSTFIELDS, "{'name':'sujal'}");
        curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization' => ' Bearer 00D7F000004981J%21ARQAQFZvTg0hvzPNDGP9cLFhIzHVUkTPOpJpfY9F6rO8imeK7ogMyiniPL.A_CRSenZ0Vi0IwN0zJWf6aItZAylGPdKkG57R', 'Acccept' => 'application/json', 'Content-length' => 100));

        // $output contains the output string
        $output = curl_exec($ch);


            echo "<pre>";
            print_r($output);
            exit;

Above code Response:

[
   {
      message: "Session expired or invalid",
      errorCode: "INVALID_SESSION_ID"
   }
]

What's wrong in above code?

2

The instance_url in the callback shows that your org is on ap5.salesforce.com, but you're trying to use the session id on na3.salesforce.com.

It's unclear from your code whether you are actually sending the token in the Authorization header. Are you extracting access_token from the hash and setting $headers such that $headers['Authorization'] is set to Bearer 00Dxxxxxxxxxxxx!xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx?

There are a few other potentials problems with your code. You're setting the Content-Type to application/json, but you set CURLOPT_POSTFIELDS to an array, which should be used with multipart/form-data. You also set CURLOPT_CUSTOMREQUEST to GET, but you want to perform a POST. You probably want to set CURLOPT_POSTFIELDS to a json string, and not use CURLOPT_CUSTOMREQUEST.

  • Hey xn, thank for reply. i'm trying to change na3 to ap5 but it's same error. – Sujal Patel Jan 4 '18 at 5:31
  • You have an extra space before "Bearer". You don't need to set the Accept header. Content-Length should be the actual length of the json string. – xn. Jan 5 '18 at 14:54
0

don't hard code the URL. try to get it from the login response only. your instance url will vary from org to org.

even if you use the REST login API of salesforce it will return the org instance url along with access_token and you can use that only.

  • Hey sagar, i was try but same error [{"message":"Session expired or invalid","errorCode":"INVALID_SESSION_ID"}]. same instance_url and api url at this time. – Sujal Patel Jan 4 '18 at 14:25
  • than something is missing or wrong. you can try on postman or anything other REST API explorer. if it runs successfully than try in your code – Sagar Hinsu Jan 5 '18 at 6:38
  • try passing Content-Length also along with your request. – Sagar Hinsu Jan 5 '18 at 7:06
  • in code I dont see any issue. if you have your salesforce org credentials than try doing it in [link]workbench.developerforce.com/restExplorer.php for me it is showing that your json in wrong. may be you can try – Sagar Hinsu Jan 5 '18 at 7:20
  • i'm trying your given api and it's show me. API_DISABLED_FOR_ORG: API is not enabled for this Organization or Partner – Sujal Patel Jan 5 '18 at 7:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.