简体   繁体   English

获取 SalesForce 中 Connected App 的 refresh_token

[英]Get refresh_token for Connected App in SalesForce

I set up a Connected App, a Python application to programmatically access Salesforce objects on behalf of a user (offline access).我设置了一个连接的应用程序,一个 Python 应用程序,以代表用户以编程方式访问 Salesforce 对象(离线访问)。

The app works and I can generate an access_token :该应用程序有效,我可以生成一个access_token

$ curl https://login.salesforce.com/services/oauth2/token -d "grant_type=password" -d "client_id=MY_APP_CLIENT_ID" -d "client_secret=MY_APP_SECRET" -d "username=my@user.com" -d "password=my_password"

{"access_token":"00D09000000KDIX!AQoAQNi1234","instance_url":"https://my_instance.salesforce.com","id":"https://login.salesforce.com/id/12345/12345","token_type":"Bearer","issued_at":"1606401330889","signature":"abc/def"}

So far so good.到目前为止,一切都很好。

Now I wanted to switch to a web-server-based flow that uses refresh tokens, but I'm stumped.现在我想切换到使用刷新令牌的基于 Web 服务器的流程,但我很难过。 Where do I get the initial refresh_token to send alongside grant_type=refresh_token ?我在哪里可以获得与grant_type=refresh_token refresh_token The docs seem to assume I already have a refresh_token and just want to generate another access_token based off that, which is not the case.文档似乎假设我已经有一个refresh_token并且只想根据它生成另一个access_token ,但事实并非如此。

What are the actual steps and necessary calls, end-to-end?端到端的实际步骤和必要的调用是什么?

List of docs that I found and read, but made me no wiser:我找到并阅读的文档列表,但让我没有更聪明:

Here's the Salesforce documentation on the Web Server OAuth flow.这是Web 服务器OAuth 流程上的 Salesforce 文档。 It runs like this;它是这样运行的; note that user interaction is involved, so curl by itself won't be enough to demonstrate the flow clearly.请注意,涉及用户交互,因此curl本身不足以清楚地展示流程。

  • You direct the user to the Salesforce login UI, in their web browser, to get yourself an an authorization code:您在 web 浏览器中将用户引导至 Salesforce 登录 UI,以获取授权码:

     https://login.salesforce.com/services/oauth2/authorize?client_id=<YOUR CONNECTED APP CLIENT ID>&redirect_uri=<CALLBACK URL ON YOUR SERVER>&response_type=code
  • The user interacts with the authorization page to approve your application.用户与授权页面交互以批准您的应用程序。

  • The user is then redirected to the callback URL in your application that you provided in the call (note: this also has to be set up as a callback in your Connected App definition), eg,然后,用户将被重定向到您在调用中提供的应用程序中的回调 URL(注意:这也必须在您的 Connected App 定义中设置为回调),例如,

     https://YOUR_SERVER.com/oauth2/callback?code=<AUTHORIZATION CODE>

    Your app can present UI here if you want but the point is to ingest the authorization code.如果您愿意,您的应用可以在此处显示 UI,但重点是获取授权代码。

    The callback URL can be on localhost.回调 URL可以在本地主机上。 That's how, for example, the Salesforce CLI implements authorization of orgs;例如,Salesforce CLI 就是这样实现组织授权的; it spins up a local web server to receive the callback.它启动本地 web 服务器以接收回调。

  • At this point, the user interaction is done.至此,用户交互完成。 Your application makes a POST request to Salesforce's /services/oauth2/token endpoint to exchange the authorization code you received for an access token.您的应用程序向 Salesforce 的/services/oauth2/token端点发出POST请求,以将您收到的授权代码交换为访问令牌。

    If your Connected App is set up with the refresh_token scope, you'll also get back at that time a refresh token that you can store and use to obtain new access tokens in the future, using the refresh token flow you already identified.如果您的 Connected App 使用refresh_token scope 设置,您还将在那时取回一个刷新令牌,您可以使用您已经确定的刷新令牌流来存储和使用它来获取新的访问令牌。

For a headless application, it can be easier to go straight to JWT (if that's your ultimate goal).对于无头应用程序,将 go 直接转换为 JWT 会更容易(如果这是您的最终目标)。 I have an example of how to pair JWT authentication with the simple_salesforce Python library.我有一个如何将 JWT 身份验证与simple_salesforce Python 库配对的示例 It takes a little bit of initial setup to populate the certificate on the Connected App and assign Preapproved Profiles (or better, Permission Sets), but once the setup is done it's very smooth and never requires any user interaction.在连接的应用程序上填充证书并分配预先批准的配置文件(或更好的权限集)需要一些初始设置,但一旦设置完成,它就非常顺利,不需要任何用户交互。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM