Salesforce integration with Python empowers businesses to streamline their operations by bridging the gap between Salesforce CRM and Python applications. Leveraging robust Python libraries like simple-salesforce or the Salesforce Python SDK, developers gain access to Salesforce's extensive functionality, allowing for seamless data synchronization, automation of workflows, and real-time data exchange. Through Salesforce APIs, Python applications can perform CRUD operations, execute queries, and manipulate Salesforce data effortlessly. This integration enables businesses to enhance their customer relationship management, optimize processes, and derive valuable insights from their Salesforce data, ultimately driving growth and efficiency.

This library has been tested with python 2 and 3.


Installation

Install the library using pip:

  pip install flask requests

This project aims to enable OAuth2 authentication to authenticate users without directly providing login credentials. The API, hosted on a separate server accessible via the URL 'https://127.0.0.1:8000/demo/', facilitates this process. Firstly, a Salesforce account is created with the user's credentials, followed by the creation of a Connected App within Salesforce. Upon successful creation, the Connected App provides a client_id and client_secret for API access, along with a specified Redirect URI. Upon invoking the API, a response containing the OAuth token code is received, allowing for subsequent access to user data.

Implementation

client_id     = "<YOUR_SALESFORCE_CLIENT_ID>"
redirect_URI  = "<YOUR_SALESFORCE_REDIRECT_URI>"
url           = f"https://test.salesforce.com/services/oauth2/authorize?\
              client_id={client_id}&redirect_uri={redirect_URI}&response_type=code"

To begin OAuth access, direct Salesforce users to your authorization URL. Users must be logged into Salesforce to authorize your app's access. Once authorized, they'll be redirected to the designated redirect_uri. The URL will contain a code query parameter, which you'll utilize to obtain an access token from Salesforce. Subsequently, employing the client_id, client_secret, redirect_uri, and code, you can authenticate and retrieve the access token.

params = {
  "client_id":      "<YOUR_SALESFORCE_CLIENT_ID>",
  "client_secret":  "<YOUR_SALESFORCE_CLIENT_SECRET>",
  "grant_type":     "authorization_code",
  "redirect_uri":   "<YOUR_SALESFORCE_REDIRECT_URI>",
  "code":           code
}
response = requests.post("https://test.salesforce.com/services/oauth2/token", params=params)
access_token = response.json().get("access_token")

Getting access token

After getting access_token, we can easily fetch user_info:

url =  "https://test.salesforce.com/services/oauth2/userinfo"
if access_token:
  user_info   = requests.get(url, headers={'Authorization': "OAuth {}".format(access_token)})
  first_name  = user_info.json().get('given_name')
  last_name   = user_info.json().get('family_name')
  username    = user_info.json().get('preferred_username')
  email       = user_info.json().get('email')
  user_id     = user_info.json().get('user_id')

To obtain access to account details, follow these steps:

  1. Navigate to the API URL, such as http://localhost:8000/api.
  2. Use Postman or the browser search bar to hit an API, triggering the generation of a code.
  3. This action automatically triggers an authorization API, which generates the code.
  4. The generated code is then used to obtain an access_token via OAuth2 authentication.
  5. Utilize the access_token to retrieve various account details, including account data, opportunities, contacts, and more.
  6. With all necessary credentials, save the integrated user's details securely in the database for future reference.

Client requirements are as follows:

  1. OAuth2 Authorization: Upon user login to the website, ensure that neither third-party apps nor the website server can access the user's password.
  2. Use Access Token: Employ access tokens to enhance authentication security, allowing users to access content securely on the website post-authentication.

Benefits:

  1. Seamless Authentication: Users can authenticate to partner applications using their Salesforce login credentials, enhancing user experience and convenience.
  2. Enhanced Security: Salesforce issues refresh tokens, allowing users to obtain new access tokens without sharing their password credentials. This ensures that sensitive information is not stored or shared with third-party websites or databases, enhancing security.
  3. Automated Token Refresh: With refresh tokens, users can automatically obtain new access tokens, eliminating the need for them to log in to Salesforce again. This improves workflow efficiency and reduces user friction.
  4. Expiry Mechanism: Automatically generated tokens have a predefined expiry duration, typically 60 days, enhancing security by ensuring that access remains valid for a limited period, mitigating the risk of unauthorized access.

Live Example

Want to see something cool? Click this button and watch the accent color on this page change.


License

Salesforce Integration is licensed under the MIT License.

0
0
0
0