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
Bill JonesBill Jones 

Creating a Record with REST API

Hello,

I'm new to Salesforce. I have a ASP.NET MVC app written in C# that I have authenticated with Salesforce via OAuth. I have authenticated with Salesforce via the "Obtaining an Access Token in a Web Application (Web Server Flow)" described here (https://developer.salesforce.com/page/Digging_Deeper_into_OAuth_2.0_on_Force.com). I have successfully received an access token. I am now trying to use that access token to create a record on an object in Salesforce. Currently, I have the following code:
// Build my object in C# and serialize it to JSON via JSON.NET
var myObject = new CustomObject();
myObject.ObjectId = 1;
myObject.Description = "This is a test sent at " + DateTime.Now.ToString();
var json = JsonConvert.SerializeObject(myObject);

// Send the data to Salesforce
var request = (HttpWebRequest)(HttpWebRequest.Create("https://na1.salesforce.com/services/data/v20.0/sobjects/MyObject/"));
request.Method = "POST";
request.ContentType = "application/json";

// Add the access token for authentication information
// The accessToken variable is passed into the method. I have verified that accessToken matches what is sent back during OAuth
request.Headers.Add("Authorization", "Bearer " + accessToken);

// Send the object to Salesforce
var response = request.GetResponse();
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
  var data = reader.ReadToEnd();
}

...

The code above attempts to pass my C# object to my Salesforce object via the API documentation (https://www.salesforce.com/us/developer/docs/api_rest/). I am relying on the "Using REST Resources -> Using REST API Resources -> Working with Records -> Create a Record" section. When the code executes, an exception is thrown. The exception is a 401 - Unauthorized error. This makes me thing that I'm passing the access token incorrectly. However, it looks correct to me.

Can someone please tell me what I'm doing wrong?

Thank you!
 
Best Answer chosen by Bill Jones
BalajiRanganathanBalajiRanganathan
verify this line
var request = (HttpWebRequest)(HttpWebRequest.Create(instanceUrl +"/services/data/v20.0/sobjects/MyObject/")); 
you might have to change MyObject to acutall object API name where you are trying to create record.
Also note that custom objects ends with __c

All Answers

BalajiRanganathanBalajiRanganathan
Try setting OAuth instead of Bearer
request.Headers.Add("Authorization", "OAuth " + accessToken);
Bill JonesBill Jones
@balaji r - I tried that approach. Unfortunately, I got the same result.
Bill JonesBill Jones
I should also mention that look at the response, I notice the following JSON is returned:
[{"message":"Session expired or invalid","errorCode":"INVALID_SESSION_ID"}]

 
BalajiRanganathanBalajiRanganathan

Verify your url while getting OAuth and in your main logic.
Are you in Sandbox or Prod?
https://na1.salesforce.com looks like a prod url.

if you are in sandbox, use test.salesforce.com for login tocken and your sanbox url
Bill JonesBill Jones
Can you please elaborate? To get the access request code, I'm using the following URL:
https://login.salesforce.com/services/oauth2/authorize?response_type=code&client_id=[myConsumerKey]&redirect_uri=http://localhost:5004/home/AuthCallback

When I attempt to create a record, i'm using a URL like this:
https://na16.salesforce.com/services/data/v20.0/sobjects/MyObject/

I do not care if I'm using the sandbox or prod version at this point. Im just trying to get it to run. I've made some updates, and now I'm getting a 404. My new code looks like this:
// Build my object in C# and serialize it to JSON via JSON.NET
var myObject = new CustomObject();
myObject.ObjectId = 1;
myObject.Description = "This is a test sent at " + DateTime.Now.ToString();
var json = JsonConvert.SerializeObject(myObject);

// Send the data to Salesforce
var request = (HttpWebRequest)(HttpWebRequest.Create(instanceUrl + "/services/data/v20.0/sobjects/MyObject/"));
request.Method = "POST";
request.ContentType = "application/json";

// Attach the access token and JSON to the request to Salesforce.
insertRequest.Headers.Add("Authorization: OAuth " + accessToken);
using (var requestWriter = new StreamWriter(request.GetRequestStream()))
{
  requestWriter.Write(json);
  requestWriter.Flush();
  requestWriter.Close();
}

// Send the object to Salesforce
var response = request.GetResponse();
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
  var data = reader.ReadToEnd();
}

...
instanceUrl and accessToken are returned from Salesforce when I authorize my app. They come from the access_token and instance_url properties of the JSON object returned from
https://login.salesforce.com/services/oauth2/token?code=[requestCode]&grant_type=authorization_code&client_id=[ConsumerKey]&client_secret=[ConsumerSecret]&redirect_uri=
http://localhost:5004/home/AuthCallback
Thank you for your help. I feel like I'm SO close. I'm just not sure what I'm doing wrong.

Thank you.
BalajiRanganathanBalajiRanganathan
verify this line
var request = (HttpWebRequest)(HttpWebRequest.Create(instanceUrl +"/services/data/v20.0/sobjects/MyObject/")); 
you might have to change MyObject to acutall object API name where you are trying to create record.
Also note that custom objects ends with __c
This was selected as the best answer