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
vino1.395228003587918E12vino1.395228003587918E12 

"[{\"message\":\"Session expired or invalid\",\"errorCode\":\"INVALID_SESSION_ID\"} in rest api

HttpClient updateclient = new HttpClient();


            string requestMessage = "<root><name>test</name></root>";
            HttpContent updatecontent = new StringContent(requestMessage, Encoding.UTF8, "application/xml");

            //string requestMessage = "{\"Name\":\"DevForce20\"}";
            //HttpContent updatecontent = new StringContent(requestMessage, Encoding.UTF8, "application/json");

string updateurl = serviceUrl + "/services/data/v25.0/sobjects/Account/";


            //create request message associated with POST verb
            HttpRequestMessage request1 = new HttpRequestMessage(HttpMethod.Put,updateurl);

            //add token to header
            request1.Headers.Add("Authorization", "Bearer " + oauthToken);
          
            //return xml to the caller
            request1.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xml"));
            request1.Content = updatecontent;
        
            HttpResponseMessage response1 = await updateclient.PutAsync(updateurl+"001O000000MPrtsIAD",request1.Content);

            string resultipp = await response1.Content.ReadAsStringAsync();
           

            Response.Write(resultipp);

            return resultipp;
I got this error "[{\"message\":\"Session expired or invalid\",\"errorCode\":\"INVALID_SESSION_ID\"} .
If any one help me to resolve this.
Ashish_SFDCAshish_SFDC
Hi Vino, 


How are you setting Salesforce::LOGIN_BASE? It should be the instance_url you got along with access_token (something like na3.salesforce.com, not login.salesforce.com). See this cookbook recipe for more details: http://developer.force.com/cookbook/recipe/interact-with-the-forcecom-rest-api-from-php

https://developer.salesforce.com/forums?id=906F000000099x7IAA

Also see below, 

http://salesforce.stackexchange.com/questions/21621/invalid-session-id-in-salesforce-restapi


Regards,
Ashish
vino1.395228003587918E12vino1.395228003587918E12
HI Ashish,
Yes i added the instanceurl.My above code is

HttpClient hc = new HttpClient();

            string consumerkey = "3MVG9Nvmjd9lcjRl5xQxpv_xyGmf8xD1xxozWzj3M2GOgmRau8SN1ElVxZ4Hqnl.5tcWCikg6.iqEVVRI3_R_";
            string consumersecret = "4228649568468639459";
            string concat = String.Concat(password, token);
            HttpContent content = new FormUrlEncodedContent(new Dictionary<string, string>
  {
     {"grant_type","password"},
     {"client_id",consumerkey},
     {"client_secret",consumersecret},
     {"username",username},
     {"password",concat}
   }
);

            HttpResponseMessage obj1 = await hc.PostAsync("https://test.salesforce.com/services/oauth2/token", content);
            string repstring = await obj1.Content.ReadAsStringAsync();

            JObject obj = JObject.Parse(repstring);
            string oauthToken = null;
            string serviceUrl = null;
            string session = null;
            oauthToken = (string)obj["access_token"];
            serviceUrl = (string)obj["instance_url"];
            session = (string)obj["signature"];

HttpClient updateclient = new HttpClient();


            string requestMessage = "<root><name>test</name></root>";
            HttpContent updatecontent = new StringContent(requestMessage, Encoding.UTF8, "application/xml");

            //string requestMessage = "{\"Name\":\"DevForce20\"}";
            //HttpContent updatecontent = new StringContent(requestMessage, Encoding.UTF8, "application/json");

string updateurl = serviceUrl + "/services/data/v25.0/sobjects/Account/";


            //create request message associated with POST verb
            HttpRequestMessage request1 = new HttpRequestMessage(HttpMethod.Put,updateurl);

            //add token to header
            request1.Headers.Add("Authorization", "Bearer " + oauthToken);
         
            //return xml to the caller
            request1.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xml"));
            request1.Content = updatecontent;
       
            HttpResponseMessage response1 = await updateclient.PutAsync(updateurl+"001O000000MPrtsIAD",request1.Content);

            string resultipp = await response1.Content.ReadAsStringAsync();
          

            Response.Write(resultipp);

            return resultipp;

Ashish_SFDCAshish_SFDC
Hi ,


The code looks good and did this solve your issue here?

Reply back if you have any further issues, we will try our best. 


Regards,
Ashish
vino1.395228003587918E12vino1.395228003587918E12
Hi,
No i didnt got the result...... Please give some ideas.........
Ashish_SFDCAshish_SFDC
Hi Vino, 


Are you able to save the code?

If you are able to save but failing at run time, Enable debug logs and monitor the logs. 


Regards,
Ashish
vino1.395228003587918E12vino1.395228003587918E12
Hi Ashish,
I am able to save the code.  
But i got the same error. 
Ashish_SFDCAshish_SFDC
Hi , 

How are you setting Salesforce::LOGIN_BASE? It should be the instance_url you got along with access_token (something like na3.salesforce.com, not login.salesforce.com). See this cookbook recipe for more details: http://developer.force.com/cookbook/recipe/interact-with-the-forcecom-rest-api-from-php


See the below thread for more information, 

https://developer.salesforce.com/forums?id=906F000000099x7IAA


Also see some more threads with Same issue, 

http://salesforce.stackexchange.com/questions/21621/invalid-session-id-in-salesforce-restapi

http://stackoverflow.com/questions/6095352/salesforce-rest-api-invalid-session-id-error


Regards,
Ashish
Andrew FandreAndrew Fandre
I had success using the SendAsync() method rather than PutAsync()

            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, fullRequest);
            request.Headers.Add("Authorization", "Bearer " + oauthToken);

            request.Headers.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
            request.Content = new StringContent(caseJSON, Encoding.UTF8, "application/json"); 
            HttpClient postClient = new HttpClient();

            HttpResponseMessage result = await postClient.SendAsync(request);
            string caseResult = await result.Content.ReadAsStringAsync();