Explore Courses Blog Tutorials Interview Questions
0 votes
in Salesforce by (11.9k points)

I have been tasked with integrating our homegrown multi-client CRM with I am going to write a server-based service which will push information from each of our clients CRM datastores to Salesforce. I'd like to use the SFDC REST API, but could use the SOAP API if necessary.

I am struggling to understand the best security mechanism to use. As the solution will be server-based it is essential that no user interaction is required when connecting to SFDC. Our server needs to be able to establish a secure connection to SFDC without a user providing their login credentials.

So far I have experimented with the REST API, and OAuth2.0. I have setup a test SFDC account and configured our app within it, getting the consumer key, secret key and callback uri. This all works, and my callback page receives a security token. My callback page uses the supplied token as follows:

string rc = "";



    string uri = "";

    System.Net.WebRequest req = System.Net.WebRequest.Create(uri);

    req.Method = "GET";

    req.Headers.Add("Authorization: Bearer " + token);

    System.Net.WebResponse resp = req.GetResponse();

    System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream());

    rc = "code=" + code + ", response=" + sr.ReadToEnd().Trim();


catch (Exception ex)


    rc = "45435465 Token=" + token + ", err=" + ex.Message;


return rc;

Trouble is, every time I run this I receive a 401 (Unauthorised) reply from SFDC, even though I am passing the security token provided by SFDC.

Is the REST API the correct approach for 'unattended' access like this, and can anyone advise what I'm doing wrong in my code, or offer words of wisdom on how to get this working?

Many thanks.

1 Answer

0 votes
by (32.1k points)

So basically, you can see that the code returned to the callback page must then be used to achieve an access_token. For example, the user will be redirected to your page like this:

You must then create an HTTP POST to the token service here:

With the following url-encoded data:






The Salesforce token server will return with some JSON data as follows:









The returned access_token can be used to authorize requests to the REST API. When it expires a new one can be obtained using the refresh_token.

Browse Categories