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
Angie WinterbottomAngie Winterbottom 

How to use REST API without a Salesforce user login

Hello,

I'm a beginner with Salesforce development, apologies for asking something that should be quite basic, but I just can't find this in the docs.

I build and maintain a website for an organisation. The organisation store their data in Salesforce (Classic).

What I'm trying to achieve is - I need the website to contact Salesforce at certain points to read and update data within Salesforce.
(Also, the website is written in C# ASP.NET, if that's relevant info)

So the model I need is - server to server communication, the website user needs to remain completely unaware of the presence of Salesforce.

Now, I was thinking - this should be achievable by using the REST API and client key / client secret... but the examples I've gone through either return a web page which requires the end user to log into Salesforce, or return JSON which just says "client identifier invalid".

Can someone please point me in the right direction? Thanks in advance!
Raj VakatiRaj Vakati
You can do it by using OUATH . 
https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/quickstart_oauth.htm
http://www.mstsolutions.com/blog/content/testing-salesforce-web-service-using-postman-rest-client
https://enterpriseforcearchitect.com/2012/08/02/salesforce-com-apex-rest-oauth/
https://developer.salesforce.com/forums/?id=906F000000099zbIAA
https://developer.salesforce.com/blogs/tag/oauth
Raj VakatiRaj Vakati
You can do it by using OAuth   . 

https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/quickstart_oauth.htm
http://www.mstsolutions.com/blog/content/testing-salesforce-web-service-using-postman-rest-client
https://enterpriseforcearchitect.com/2012/08/02/salesforce-com-apex-rest-oauth/
https://developer.salesforce.com/forums/?id=906F000000099zbIAA
https://developer.salesforce.com/blogs/tag/oauth
 
Sameer Aman 2Sameer Aman 2
define("CLIENT_ID", "PUT_YOUR_CONSUMER_KEY_HERE");
define("CLIENT_SECRET", "PUT_YOUR_CONSUMER_SECRET_HERE");
define("LOGIN_URI", "<a target="_blank" href="https://login.salesforce.com" rel="nofollow">https://login.salesforce.com</a>");

define("USERNAME", "user@example.com");
define("PASSWORD", "password");

function salesforce_login($username, $password) {
    $params = "grant_type=password&client_id=".CLIENT_ID.
            "&client_secret=".CLIENT_SECRET.
            "&username=$username&password=$password";
    
    $token_url = LOGIN_URI . "/services/oauth2/token";
    
    $curl = curl_init($token_url);
    curl_setopt($curl, CURLOPT_HEADER, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
    $json_response = curl_exec($curl);
    $status = curl_getinfo($curl, CURLINFO_HTTP_CODE);
    if ( $status != 200 ) {
        die("Error: call to URL $token_url failed with status $status");
    }
    
    return json_decode($json_response, true);
}

$response = salesforce_login(USERNAME, PASSWORD);
$access_token = $response["access_token"];
$instance_url = $response["instance_url"];
//call method show accounts


show_accounts($instance_url, $access_token);


//show accounts defined
function show_accounts($instance_url, $access_token) {
    $query = "SELECT Name, Id from Account LIMIT 100";
    $url = "$instance_url/services/data/v41.0/query?q=" . urlencode($query);

    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_HEADER, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_HTTPHEADER,
            array("Authorization: OAuth $access_token"));

    $json_response = curl_exec($curl);
    curl_close($curl);

    $response = json_decode($json_response, true);

    $total_size = $response['totalSize'];

    echo "$total_size record(s) returned<br/><br/>";
    foreach ((array) $response['records'] as $record) {
        echo $record['Id'] . ", " . $record['Email'] . "<br/>";
    }
    echo "<br/>";
}

Hi @Angie,
Just replace data with your original password , username , customer id and Client id
for customer and client id you can go to creat->app then scroll down and create new connected app then see customer id and client id