Python Salesforce API Authentication

左心房为你撑大大i 提交于 2021-01-29 09:14:22

问题


I am trying to automate creating tickets in Salesforce. For this, I am using API with Python. I have got the Client ID and Client secret for my registered python Application. I have read many questions and as per the security purpose, I do not want to use the "user-password" flow for my script. Is there any way that I can only use "CLIENT ID" and "CLIENT SECRET" to get the access token where I can pass this access token in bearer header for other calls

import requests

params = {
    "grant_type": "client_credentials",
    "client_id": client_id, # Consumer Key
    "client_secret": client_secret, # Consumer Secret

}

r=requests.post("https://login.salesforce.com/services/oauth2/token", params=params)

access_token = r.json().get("access_token")

instance_url = r.json().get("instance_url")
print("Access Token:", access_token)

回答1:


You'll always need a SF user account. There's no way to just make a backend system talk to SF system. Salesforce treats everybody as user so you need to waste an account for "integration user" - but in return you can control access to tables, columns, functionalities just like you control real humans' access. This goes all the way down to the underlying Oracle database and database user privileges.

Whether you use OAuth2 flows (including client secrets) or maybe some certificate-based authentication - there will be always some element of "username and password" required. Best you can do is to make sure your app doesn't need to see & store the password, instead showing normal SF login prompt and on successful login user is redirected to your app to continue with known session id...

There might be something you'll be able to automate more if your app and SF use same Single Sign-On but broadly speaking... You have to either let users login to SF via your app or create the tickets as some dedicated admin user (and then you store this user's credentials in your app)



来源:https://stackoverflow.com/questions/58381676/python-salesforce-api-authentication

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!