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
RestTestRestTest 

Reset Password and "disable" account using REST api?

Hi,

 

I have also 2 other questions.

Using rest apis, how do I change and/or reset a user password? (Do you have a code sample showing which property to set, if password is sent plain text, encoded, hashed etc...)

 

Also, how do I "disable" an account using the REST apis?

 

Thank you very much for your help.

All Answers

Pat PattersonPat Patterson

Hi RestTest,

 

Currently there is no way to change/reset user password from the REST API. It is possible, however, to disable a user account via the REST API - simply update the User object, setting the IsActive field to false. Here is a PHP sample that does this:

 

<?php
// I'm getting credentials etc from environment variables
// Hardcode them if you must... define("SF_SECURITY_TOKEN", getenv("SECURITY_TOKEN")); define("SF_USERNAME", getenv("USERNAME")); define("SF_PASSWORD", getenv("PASSWORD")); define("CLIENT_ID", getenv("CLIENT_ID")); define("CLIENT_SECRET", getenv("CLIENT_SECRET")); define("LOGIN_URI", getenv("LOGIN_SERVER")); function salesforce_login($username, $password) { $params = 'grant_type=password' . '&client_id=' . CLIENT_ID . '&client_secret=' . CLIENT_SECRET . '&username=' . urlencode($username) . '&password=' . urlencode($password); $token_url = LOGIN_URI . "/services/oauth2/token"; // $token_url = "http://www.postbin.org/106jphp"; $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); //curl_setopt($curl, CURLOPT_PROXY, "http://localhost:8888/"); //curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); $json_response = curl_exec($curl); $status = curl_getinfo($curl, CURLINFO_HTTP_CODE); if ( $status != 200 ) { echo 'token_url = ' . $token_url . "\n"; echo 'params = ' . $params . "\n"; echo 'response = ' . $json_response . "\n"; die("Error: call to URL $token_url failed with status $status\n"); } curl_close($curl); return json_decode($json_response, true); } echo "Logging in as ".SF_USERNAME."\n"; $response = salesforce_login(SF_USERNAME, SF_PASSWORD.SF_SECURITY_TOKEN); $instance_url = $response['instance_url']; $access_token = $response['access_token']; // Change the following ID to a user in your org $user = "00550000001oZ9v"; $url = "$instance_url/services/data/v20.0/sobjects/User/$user"; // Set IsActive to false to disable the user account, true to enable it $content = json_encode(array("IsActive" => false)); $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", "Content-type: application/json")); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "PATCH"); curl_setopt($curl, CURLOPT_POSTFIELDS, $content); $response = curl_exec($curl); $status = curl_getinfo($curl, CURLINFO_HTTP_CODE); if ( $status != 204 ) { die("Error: call to URL $url failed with status $status, response $response\n"); } echo "Successfully updated user $user\n"; curl_close($curl); ?>

 

Cheers,

 

Pat

RestTestRestTest

Thanks for this information.

If REST is not able to change/reset a password, do you know an alternate way to do it programmatically?

Pat PattersonPat Patterson

You can use setPassword() and resetPassword() in the SOAP API - see http://www.salesforce.com/us/developer/docs/api/Content/utility_calls.htm

RestTestRestTest

I have given up the REST api since they are incomplete (no Set or Reset Password) and I don't want to have to integrate 2 different API sets.

Therefore I switched to web services.

 

To disable a user, I'm calling the following function.

It returns without error, but it does not disable the account. (If I change another field, for example firstname, the field is correctly updated).

 

Do you have a sample showing how to disable a user?

 

internal void SuspendUser(string loginID)       

{           

    User newUser = new User();           

    newUser.Id = loginID;           

    newUser.IsActive = false;

    

   SaveResult[] results = _Binding.update(new sObject[] { newUser });           

   if (!results[0].success)           

   {               

        StringBuilder sb = new StringBuilder();               

        foreach (Error error in results[0].errors)               

        {                   

               sb.Append(string.Format("Code: {0}, Error: {1}", error.statusCode.ToString(), error.message));               

        }               

        throw new Exception(sb.ToString());           

    }       

}

 

Thanks.

RestTestRestTest

Great thanks, It works.

I could have spend weeks to find this !

Ismael AcevedoIsmael Acevedo
Can you help me ,the error that i get is Error: call to URL https://login.salesforce.com/services/oauth2/token failed with status 0