使用 OAuth2 基础支持插件设置 Salesforce 认证

使用 OAuth2 基本支持插件(自 v2025.11.0 起已包含在 Discourse 核心中),您可以设置 Salesforce 身份验证。

为此,请在 Salesforce 上创建一个连接的 OAuth2 应用:

  • 创建 → 应用 → 新建连接应用

  • 确保允许访问“访问基本信息”

  • 将“回调 URL”设置为 https://YOURFORUM.COM/auth/oauth2_basic/callback:请注意,您必须使用 HTTPS 运行 Discourse 论坛。

  • 记下客户端 ID 和密钥

在 Discourse 端,配置以下设置(位于 管理 > 登录 > OAuth2.0 下):

  • oauth2 enabled: true
  • oauth2 client id 和 secret,参照上一节
  • oauth2 authorize url: https://login.salesforce.com/services/oauth2/authorize
  • oauth2 token url: https://login.salesforce.com/services/oauth2/token
  • oauth2 fetch user details: true(这是默认值,但请确保已启用)
  • oauth2 user json url: https://login.salesforce.com/services/oauth2/userinfo
  • oauth2 json user id path: user_id
  • oauth2 json username path: preferred_username
  • oauth2 json name path: name
  • oauth2 json email path: email
  • oauth2 email verified: true
  • oauth2 authorize options: scope, display, immediate, state

如果您遇到问题,请确保启用 oauth2 debug auth 并检查您的 /logs

9 个赞

Thanks for this post, which was invaluable in setting up a recent project, which required Salesforce login.

In our case, the Salesforce app was a Community, which meant we had to change some of the details above. I’m posting on this old thread in case it helps anyone else. It nearly broke my brain for a whole day.

  • oauth2 authorize url: https://[COMMUNITY_NAME].force.com/services/oauth2/authorize
  • oauth2 token url: https://[COMMUNITY_NAME].force.com/services/oauth2/token
  • oauth2 callback user id path: id

The path changes were worked out gradually (and painfully) from Salesforce and other documentation:
https://auth0.com/docs/connections/social/salesforce
Help And Training Community

However we still had problems with 403 Forbidden errors, which were plain unstyled HTML and didn’t look very much like a Discourse error to me, which led to much debugging of Salesforce and gnashing of teeth. But the problem was in Discourse.

Forbidden

You don’t have permission to access this resource.

Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request.

Although the redirect to Callback URL seemed to be working, the browser console registered authentication failures. In the end it was the unset oauth2 callback user id path which was the cause of the authentication failure. Setting it to id fixed everything.

All other settings as in previous post.

2 个赞

感谢提供指南,我们已能成功通过 Salesforce 进行身份验证,但遇到一个问题:我们的 Salesforce 对象/字段似乎未能正确传递到 Discourse。在 Discourse 上成功完成 Salesforce 登录后,Discourse 仍将其视为新用户,要求输入用户名、邮箱和姓名,尽管这些信息本应来自 OAuth2 JSON 响应中的 name、email 和 username 字段。

能否请您协助说明在 OAuth2 插件中应使用的 Salesforce 对象/字段的 JSON 格式?我们已尝试过 object.field、object_field 以及仅使用 field 等形式,虽然未报错,但似乎没有任何数据从 Salesforce 通过 JSON 传递到 Discourse,导致系统无法识别该登录为非新用户。

更新一下,我们已找到问题的解决方案,需要在我们的端进行以下设置以映射 Salesforce 字段。

必须配置的设置如下:

### oauth2 获取用户详情 - **已勾选**
### oauth2 用户 JSON URL - **https://<yourfssite.com>/services/oauth2/userinfo**
### oauth2 用户 JSON URL 方法 - **get**
### oauth2 JSON 用户 ID 路径 - **user_id**
### oauth2 JSON 用户名路径 - **preferred_username**
### oauth2 JSON 姓名路径 - **name**
### oauth2 JSON 邮箱路径 - **email**

希望这能帮助其他正在寻找 OAuth2 与 Salesforce 连接解决方案的用户。

2 个赞

感谢提供信息 @sonny.mendoza —— 我已将其整合到本主题顶部的说明中,以便未来能帮助到其他用户 :slight_smile:

3 个赞