如何在 Discourse 中配置使用 GitHub 登录和创建用户

本文章用于指导你如何在 Discourse 中配置使用 GitHub 的用户名进行登录。

配置GitHub

访问 https://github.com/settings/developers ,然后在左侧的菜单栏中找到 OAuth Apps,随后选择 New OAuth App,并且根据提示输入相应的内容和字段。

github-discourse-01

需要注意的是你的回调地址为: /auth/github/callback 的后缀,针对你的网站的域名不同,回调地址的后缀也是不同的。

例如,网站 https://www.usrealestate.io/ 是你能够正常访问的地址,那么回调(Call Back)地址为:https://www.usrealestate.io/auth/github/callback

github-discourse-02

上传 Logo(可选)

这一步是可选的,你可以在这一步上传你网站的 Logo,那么用户在使用的时候就可以在 GitHub 登录的时候看到你网站使用的 logo 了。

获得参数

当你完成在 GitHub 上的数据配置后,你需要单击获得秘钥来获得一个秘钥参数。

github-discourse-03

Client IDClient Secret 的参数。你需要将这 2 个参数的内容返回到你的 Discourse 站点中,然后分别输入 github_client_idgithub_client_secret 字段。

启用服务

在 Discourse 站点中,启用 enable github logins 这个设置。

下图显示了需要配置的从 GitHub 上获得的配置信息。

4github-discourse-03

当你完成上面的操作后,你的站点应该就可以使用 GitHub 来进行登录了。

4github-discourse-04

通过前端来校验登录的配置已经成功了。

1 Like

我配置好以后用自己的账号尝试登录失败 :melting_face:
client_id client_secret都是是复制黏贴,不知道问题出在哪里
image

image
image

这里可能有个问题就是你登录用户的账户和持有这个 GitHub 授权这个用户的账户是同一个人。

你试试这样,使用一个 GitHub 存在的不同用户名,然后使用这个用户名在你的论坛网站上通过GitHub 注册。

看这样能不能注册上,如果能注册上就是这个问题了。

换了一个GitHub账号,不行😅

还是一样的报错。

我测试了下你的网站,确实是这个问题

2023-08-20_11-46-59

同时,我测试回调的时候应该是没有问题的。

我感觉应该是你 GitHub 那里的网站授权问题。

因为你的提示是中文的,我没有办法搜索到应为解决方案。

能不能给个英文提示给我吗?

还有,我尝试通过在你的网站上连接我的 GitHub 账号,提示同样的错误。

2023-08-20_11-51-42

我感觉你的问题出现在 GitHub 上面,应该是对你网站的授权问题。

英文报错 Sorry, there was an error authorizing your account. Please try again.
image

我还试了试steam登录,也是一样的报错

如果是这样的话,我感觉更多的是你的网站的问题了。

我大致搜索了下官方的论坛,请参考下官方论坛下面的文章:

中的内容。

我的 GitHub 的配置内容如下图。

2023-08-21_04-57-57

其他的地方我都是按照官方的配置来的,我不认为我改了其他的配置。

如果你 steam 也是同样报错的话,我在我的网站上配个 steam 试试。

我也是照着文档配置的,id和secret也是复制黏贴,应该问题不是在这,参考Sorry, there was an error authorizing your account - #21 by Beam - SSO - Discourse Meta 看了下日志

日志路径已经和这个帖子里不一样了,我在/var/discourse/shared/standalone/log/rails/production.log里用github检索,找到了以下内容,中间无关的我就删掉了

Started GET "/auth/github/callback" for 43.230.89.164 at 2023-08-21 03:01:01 +0000
(github) Setup endpoint detected, running now.
(github) Callback phase initiated.
(github) Authentication failure! csrf_detected: OmniAuth::Strategies::OAuth2::CallbackError, csrf_detected | CSRF detected

Started GET "/auth/failure?message=csrf_detected&origin=https%3A%2F%2Fforum.beginner.center%2Fu%2F%25E7%258E%258B%25E7%2599%25BD%25E6%25B0%25B4%2Fpreferences%2Faccount&strategy=github" for 74.125.150.2 at 2023-08-21 03:02:24 +0000
Processing by Users::OmniauthCallbacksController#failure as HTML
  Parameters: {"message"=>"csrf_detected", "origin"=>"https://forum.beginner.center/u/%E7%8E%8B%E7%99%BD%E6%B0%B4/preferences/account", "strategy"=>"github"}
  Rendered users/omniauth_callbacks/failure.html.erb within layouts/no_ember (Duration: 0.1ms | Allocations: 17)
  Rendered layout layouts/no_ember.html.erb (Duration: 7.8ms | Allocations: 2211)

Started POST "/auth/github" for 43.230.89.164 at 2023-08-21 03:05:25 +0000
(github) Setup endpoint detected, running now.
(github) Request phase initiated.

Started POST "/auth/github" for 73.142.146.165 at 2023-08-21 08:50:12 +0000
(github) Setup endpoint detected, running now.
(github) Request phase initiated.

Started PUT "/admin/site_settings/github_client_id" for 43.230.89.163 at 2023-08-21 09:34:01 +0000
Processing by Admin::SiteSettingsController#update as */*
  Parameters: {"github_client_id"=>"861e63e4e14bb859d302", "id"=>"github_client_id"}
  Rendered text template (Duration: 0.0ms | Allocations: 1)
Completed 200 OK in 19ms (Views: 0.7ms | ActiveRecord: 0.0ms | Allocations: 2532)

Started PUT "/admin/site_settings/github_client_secret" for 43.230.89.166 at 2023-08-21 09:34:12 +0000
Processing by Admin::SiteSettingsController#update as */*
  Parameters: {"github_client_secret"=>"[FILTERED]", "id"=>"github_client_secret"}
  Rendered text template (Duration: 0.0ms | Allocations: 1)
Completed 200 OK in 20ms (Views: 1.7ms | ActiveRecord: 0.0ms | Allocations: 2609)


Started POST "/auth/github" for 43.230.89.165 at 2023-08-21 09:37:45 +0000
(github) Setup endpoint detected, running now.
(github) Request phase initiated.
Started POST "/presence/update" for 43.230.89.166 at 2023-08-21 09:37:45 +0000
Processing by PresenceController#update as */*
  Parameters: {"client_id"=>"4e3d1cf0c7aa494d8c0e695c922884cf", "leave_channels"=>["/chat-user/core/3"], "authenticity_token"=>"RnEvk64MSwc5Pqh0O3NmaMKLBK9QBDgGkz4a7ZAf0KUw9xiWWnZb43u_2opRcqxysFMxmo9Y4Ku34ItKp9CwvA"}
Completed 200 OK in 9ms (Views: 0.1ms | ActiveRecord: 0.0ms | Allocations: 2026)
Started GET "/auth/github/callback" for 43.230.89.165 at 2023-08-21 09:37:51 +0000
(github) Setup endpoint detected, running now.
(github) Callback phase initiated.
(github) Authentication failure! csrf_detected: OmniAuth::Strategies::OAuth2::CallbackError, csrf_detected | CSRF detected
Started GET "/auth/failure" for 43.230.89.163 at 2023-08-21 09:37:52 +0000
Processing by Users::OmniauthCallbacksController#failure as HTML
  Rendered users/omniauth_callbacks/failure.html.erb within layouts/no_ember (Duration: 0.1ms | Allocations: 23)
  Rendered layout layouts/no_ember.html.erb (Duration: 8.6ms | Allocations: 2498)
Completed 200 OK in 210ms (Views: 9.6ms | ActiveRecord: 0.0ms | Allocations: 41545)
Started POST "/presence/update" for 43.230.89.165 at 2023-08-21 09:37:59 +0000
Processing by PresenceController#update as */*
  Parameters: {"client_id"=>"05d0471ed2e14ea6974311815b10d9a1", "present_channels"=>["/chat-user/core/3"]}
Completed 200 OK in 10ms (Views: 0.2ms | ActiveRecord: 0.0ms | Allocations: 1991)

搞定了!
需要在allowed user api auth redirects设置里,把github的callback地址加进去,才能正常使用github,steam也是同理
image

开心🥳
image

@whitewatercn

有点奇怪的配置,我觉得可能是因为你使用了 CDN 的原因。

在你的日志中,我发现了下面的错误信息。

2023-08-21_07-20-22

根据官方论坛的讨论帖子:How to set set_real_ip_from for discourse - #9 by parisa - Support - Discourse Meta

应该是回调的服务没有办法定位你的 Discourse 实例的 IP 地址。

同时,我还查看了我的服务器配置,我的服务器配置上面,没有添加回调地址。

2023-08-21_07-21-47

但,我也没有出现有回调错误的问题。

所以我感觉可能还是你的 CDN 那块,或者你安装的时候有没有一些特殊的配置呢?

如果你没有特殊的配置的话,很有可能就是 CDN ,你添加的全站加速的问题。

1 Like