Ichizy
Ichizy

Reputation: 61

Salesforce rest api INVALID_SESSION_ID

I'm trying to connect my asp.net REST api to salesforce. I'm succesfully going through authentification, but when I start to send POST requests, I'm getting an error

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

Here is my POST request:

    //SFServerUrl = "https://na17.salesforce.com/services/";
    //url = "data/v28.0/sobjects/Account";

            ASCIIEncoding ascii = new ASCIIEncoding();
            byte[] postBytes = ascii.GetBytes(postBody);
            HttpWebRequest request = WebRequest.Create(Globals.SFServerUrl + url) as HttpWebRequest;
            request.Method = "POST";
            request.ContentType = "application/json";
            request.ContentLength = postBytes.Length;
            Stream postStream = request.GetRequestStream();
            postStream.Write(postBytes, 0, postBytes.Length);

            HttpCookie cookie = HttpContext.Current.Request.Cookies[Globals.SFCookie];
            var ticket = FormsAuthentication.Decrypt(cookie.Value);
            string authToken = ticket.UserData;
            request.Headers.Add("Authorization", "Bearer " + authToken);
            postStream.Close();

            HttpWebResponse response = request.GetResponse() as HttpWebResponse;
            StringBuilder sb = new StringBuilder();
            byte[] buf = new byte[8192];
            Stream resStream = response.GetResponseStream();
            string tempString = null;
            int count = 0;
            do
            {
                count = resStream.Read(buf, 0, buf.Length);
                if (count != 0)
                {
                    tempString = Encoding.ASCII.GetString(buf, 0, count);
                    sb.Append(tempString);
                }
            }
            while (count > 0);
            return new Tuple<bool, string>(true, sb.ToString());

When I'm trying to send GET request - I recieve 200 response. Also, I've tried to send a POST Request with the same token from Simple Rest Client and it get's 200 response. I tried to change my "Authorization : Bearer" Header to "Authorization : Oauth", but nothing changed. I also tried to catch this error, get refresh token and send a request again with refreshed token, but nothing changed. Please, help me with this.

Upvotes: 1

Views: 4542

Answers (2)

Ichizy
Ichizy

Reputation: 61

The problem was that I added Headers after Content. When I switched these lines of code everything worked.

Upvotes: 1

Daniel Ballinger
Daniel Ballinger

Reputation: 13537

Using workbench I was able to POST the following JSON to /services/data/v29.0/sobjects/Account and create a new Account.

{
    "Name" : "Express Logistics and Transport"
}

Raw Response

HTTP/1.1 201 Created
Date: Sun, 07 Sep 2014 21:32:06 GMT
Set-Cookie: BrowserId=_HC-bzpTQABC1237vFu2hA;Path=/;Domain=.salesforce.com;Expires=Thu, 06-Nov-2014 21:32:06 GMT
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Sforce-Limit-Info: api-usage=209/15000
Location: /services/data/v29.0/sobjects/Account/0010000000000001AAA
Content-Type: application/json;charset=UTF-8
Content-Encoding: gzip
Transfer-Encoding: chunked

{
"id" : "0010000000000001AAA",
"success" : true,
"errors" : [ ]
}

Things to check:

  1. Your URL. It appears to be missing the leading /services
  2. Is SFServerUrl the same Salesforce pod/server that the Session Id was issued to? If the Session Id came from another pod then it would be invalid on na17.
  3. How did you create the Session Id? If you used OAuth, what scopes did you request?
  4. Is the Session Id coming out of the cookie valid?
  5. Has something else using the same Session Id called logout and invalidated the session?

Incidentally, the Salesforce StackExchange site is a great place to ask Salesforce specific questions.

See also:

Upvotes: 1

Related Questions