How can I retrieve the Salesforce security token?
I am a new Salesforce administrator at a company that can use Salesforce for customer relationship management. I have been tasked with integrating Salesforce with an external application for data synchronization. However, it requires the Salesforce security token for authentication of API requests. Describe the steps for me how can I assist the users in retrieving their Salesforce security token?
In the context of Salesforce, here are the steps given:-
Login to the Salesforce
You can instruct the users to log into their Salesforce account.
Access personal settings
Once you have logged in, the users should click on their profile pictures in the top right corner and then you can select settings from the drop-down menu.
Navigate to the security token section
Now in the left-hand sidebar, the user can expand the “my personal information” section and then they can click on Resets my security token.
Reset security token
The users should click the “reset security token” button. After this Salesforce would send an email which would contain the new security token to the registered email address of the users.
Retrieve the token from email
Now the users can retrieve the token from their registered email.
Here is the Java-based example given below:-
Import org.apache.http.client.methods.CloseableHttpResponse;
Import org.apache.http.client.methods.HttpGet;
Import org.apache.http.impl.client.CloseableHttpClient;
Import org.apache.http.impl.client.HttpClients;
Import org.apache.http.util.EntityUtils;
Import org.json.JSONObject;
Import java.io.IOException;
Public class SalesforceConnector {
Private static final String LOGIN_URL = https://login.salesforce.com/services/oauth2/token;
Private static final String CLIENT_ID = “your_client_id”;
Private static final String CLIENT_SECRET = “your_client_secret”;
Private static final String USERNAME = “your_username”;
Private static final String PASSWORD = “your_password”;
Private static final String SECURITY_TOKEN = “your_security_token”;
Public static void main(String[] args) {
Try {
String accessToken = getAccessToken();
fetchDataFromSalesforce(accessToken);
} catch (IOException e) {
e.printStackTrace();
}
}
Private static String getAccessToken() throws IOException {
String url = LOGIN_URL
+ “?grant_type=password”
+ “&client_id=” + CLIENT_ID
+ “&client_secret=” + CLIENT_SECRET
+ “&username=” + USERNAME
+ “&password=” + PASSWORD + SECURITY_TOKEN;
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
CloseableHttpResponse response = httpClient.execute(httpPost);
String responseString = EntityUtils.toString(response.getEntity());
JSONObject jsonObject = new JSONObject(responseString);
Return jsonObject.getString(“access_token”);
}
Private static void fetchDataFromSalesforce(String accessToken) throws IOException {
String instanceUrl = https://your_instance.salesforce.com;
String query = “/services/data/v52.0/query?q=SELECT+Id,Name+FROM+Account”;
String url = instanceUrl + query;
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(url);
httpGet.setHeader(“Authorization”, “Bearer “ + accessToken);
CloseableHttpResponse response = httpClient.execute(httpGet);
String responseString = EntityUtils.toString(response.getEntity());
System.out.println(responseString);
}
}
Here is the Python based example given below:-
Import requests
Import simplejson as json
LOGIN_URL = https://login.salesforce.com/services/oauth2/token
CLIENT_ID = “your_client_id”
CLIENT_SECRET = “your_client_secret”
USERNAME = “your_username”
PASSWORD = “your_password”
SECURITY_TOKEN = “your_security_token”
Def get_access_token():
Payload = {
‘grant_type’: ‘password’,
‘client_id’: CLIENT_ID,
‘client_secret’: CLIENT_SECRET,
‘username’: USERNAME,
‘password’: PASSWORD + SECURITY_TOKEN
}
Response = requests.post(LOGIN_URL, data=payload)
Response.raise_for_status()
Access_token = response.json().get(“access_token”)
Instance_url = response.json().get(“instance_url”)
Return access_token, instance_url
Def fetch_data_from_salesforce(access_token, instance_url):
Query = “SELECT Id, Name FROM Account”
url = f”{instance_url}/services/data/v52.0/query”
headers = {
‘Authorization’: f’Bearer {access_token}’,
‘Content-Type’: ‘application/json’
}
Params = {
‘q’: query
}
Response = requests.get(url, headers=headers, params=params)
Response.raise_for_status()
Data = response.json()
Return data
If __name__ == “__main__”:
Try:
Access_token, instance_url = get_access_token()
Data = fetch_data_from_salesforce(access_token, instance_url)
Print(json.dumps(data, indent=4))
Except requests.exceptions.RequestException as e:
Print(f”An error occurred: {e}”)