Salesforce REST API GET request fine POST unauthorized
Hey .NET redditors! I have a problem that might just need another set of eyes. I'm doing REST API to Salesforce and I can get objects just fine, but when I post I get this error "[{"message":"Session expired or invalid","errorCode":"INVALID_SESSION_ID"}]
Here's the GET that works great. GetClientContent gets my Dictionary of client data for securely connecting to the Salesforce connected app.
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; string oauthToken, serviceUrl; HttpClient authClient = new HttpClient(); HttpContent content = GetClientContent(); string tokenString = ConfigurationManager.AppSettings["tokenUrl"]; HttpResponseMessage message = await authClient.PostAsync(tokenString, content); string responseString = await message.Content.ReadAsStringAsync(); JObject obj = JObject.Parse(responseString); oauthToken = (string)obj["access_token"]; serviceUrl = (string)obj["instance_url"]; string service = ConfigurationManager.AppSettings.Get("RestService") + "sobjects/Case/describe/"; string fullRequest = serviceUrl + service; HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, fullRequest); request.Headers.Add("Authorization", "Bearer " + oauthToken); request.Headers.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json")); HttpClient queryClient = new HttpClient(); HttpResponseMessage response = await queryClient.SendAsync(request); string result = await response.Content.ReadAsStringAsync();
"result" comes back with my blob of the case object described in the service.
So I should be able to do this same process with a POST and submit a new case into they system? This is what isn't working. "ru" is my properly formatted JSON object.
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; HttpClient authClient = new HttpClient(); HttpContent authContent = GetClientContent(); string tokenString = ConfigurationManager.AppSettings["tokenUrl"]; HttpResponseMessage authMessage = await authClient.PostAsync(tokenString, authContent); string responseString = await authMessage.Content.ReadAsStringAsync(); JObject requestObj = JObject.Parse(responseString); string oauthToken = (string)requestObj["access_token"]; string serviceUrl = (string)requestObj["instance_url"]; string service = ConfigurationManager.AppSettings.Get("RestService") + "sobjects/Case/"; string fullRequest = serviceUrl + service; 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")); var caseJSON = JsonConvert.SerializeObject(ru); request.Content = new StringContent(caseJSON, Encoding.UTF8, "application/json"); HttpClient postClient = new HttpClient(); HttpResponseMessage result = await postClient.PostAsync(new Uri(fullRequest), request.Content); string caseResult = await result.Content.ReadAsStringAsync(); return caseResult;
Any suggestions or helpful discussion will receive gold.
0 comments:
Post a Comment