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
Sabina Ismailova 1Sabina Ismailova 1 

rest api INVALID_SESSION_ID error

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!
James LoghryJames Loghry
If you debug line 15 (String authToken = userTicket.Data) do you get a valid refresh token back? 

Also, which endpoint are you using?

I would take that information and plug it into a test REST client.  I like to use the "REST Console" plugin for Chrome to test my REST callouts.

Using the test client you can determine if it's an issue with the way you're calling the service, or if it's an issue with setting up the call in your code.
Sabina Ismailova 1Sabina Ismailova 1
I tried to test a POST request from Mozilla REST Client with this token and it's 200 response. When I'm trying to do the same in my api - I get 401 response.
My endpoint is line 1+2 - https://na17.salesforce.com/services/data/v28.0/sobjects/Account
I've also tried to send request to another version, but nothing changed.