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

**URL:** <https://www.isharkfly.com/t/discourse-github/13562>\
**Category:** DevOps\
**Tags:** discourse\
**Created:** [July 15, 2021, 2:46pm UTC](https://www.isharkfly.com/t/discourse-github/13562 "2021-07-15T14:46:56Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![honeymoose](https://www.isharkfly.com/user_avatar/www.isharkfly.com/honeymoose/32/20796_2.png) [@honeymoose](https://www.isharkfly.com/u/honeymoose)\
**Post date:** [July 15, 2021, 2:46pm UTC](https://www.isharkfly.com/t/discourse-github/13562/1 "2021-07-15T14:46:57Z")

</div>

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

## 配置GitHub

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

![github-discourse-01](https://www.isharkfly.com/images/transparent.png)

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

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

![github-discourse-02](https://www.isharkfly.com/images/transparent.png)

## 上传 Logo（可选）

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

## 获得参数

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

![github-discourse-03](https://www.isharkfly.com/images/transparent.png)

**Client ID** 和 **Client Secret** 的参数。你需要将这 2 个参数的内容返回到你的 Discourse 站点中，然后分别输入 `github_client_id` 和 `github_client_secret` 字段。

## 启用服务

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

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

![4github-discourse-03](https://www.isharkfly.com/images/transparent.png)

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

![4github-discourse-04](https://www.isharkfly.com/images/transparent.png)

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

---

<div class="post-metadata">

**Author:** ![whitewatercn](https://www.isharkfly.com/user_avatar/www.isharkfly.com/whitewatercn/32/2131_2.png) [@whitewatercn](https://www.isharkfly.com/u/whitewatercn)\
**Post date:** [August 20, 2023, 11:37am UTC](https://www.isharkfly.com/t/discourse-github/13562/2 "2023-08-20T11:37:45Z")

</div>

我配置好以后用自己的账号尝试登录失败 🫠  
client\_id client\_secret都是是复制黏贴，不知道问题出在哪里  
 ![image](https://www.isharkfly.com/images/transparent.png)

![image](https://www.isharkfly.com/images/transparent.png)  
 ![image](https://www.isharkfly.com/images/transparent.png)

---

<div class="post-metadata">

**Author:** ![honeymoose](https://www.isharkfly.com/user_avatar/www.isharkfly.com/honeymoose/32/20796_2.png) [@honeymoose](https://www.isharkfly.com/u/honeymoose)\
**Post date:** [August 20, 2023, 2:43pm UTC](https://www.isharkfly.com/t/discourse-github/13562/3 "2023-08-20T14:43:35Z")

</div>

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

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

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

---

<div class="post-metadata">

**Author:** ![whitewatercn](https://www.isharkfly.com/user_avatar/www.isharkfly.com/whitewatercn/32/2131_2.png) [@whitewatercn](https://www.isharkfly.com/u/whitewatercn)\
**Post date:** [August 20, 2023, 3:15pm UTC](https://www.isharkfly.com/t/discourse-github/13562/4 "2023-08-20T15:15:40Z")

</div>

换了一个GitHub账号，不行😅

还是一样的报错。

---

<div class="post-metadata">

**Author:** ![honeymoose](https://www.isharkfly.com/user_avatar/www.isharkfly.com/honeymoose/32/20796_2.png) [@honeymoose](https://www.isharkfly.com/u/honeymoose)\
**Post date:** [August 20, 2023, 3:51pm UTC](https://www.isharkfly.com/t/discourse-github/13562/5 "2023-08-20T15:51:01Z")

</div>

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

![2023-08-20_11-46-59](https://www.isharkfly.com/images/transparent.png)

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

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

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

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

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

![2023-08-20_11-51-42](https://www.isharkfly.com/images/transparent.png)

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

---

<div class="post-metadata">

**Author:** ![whitewatercn](https://www.isharkfly.com/user_avatar/www.isharkfly.com/whitewatercn/32/2131_2.png) [@whitewatercn](https://www.isharkfly.com/u/whitewatercn)\
**Post date:** [August 21, 2023, 2:48am UTC](https://www.isharkfly.com/t/discourse-github/13562/6 "2023-08-21T02:48:41Z")

</div>

英文报错 `Sorry, there was an error authorizing your account. Please try again.`  
 ![image](https://www.isharkfly.com/images/transparent.png)

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

---

<div class="post-metadata">

**Author:** ![honeymoose](https://www.isharkfly.com/user_avatar/www.isharkfly.com/honeymoose/32/20796_2.png) [@honeymoose](https://www.isharkfly.com/u/honeymoose)\
**Post date:** [August 21, 2023, 8:59am UTC](https://www.isharkfly.com/t/discourse-github/13562/7 "2023-08-21T08:59:06Z")

</div>

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

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

> **[Sorry, there was an error authorizing your account](https://meta.discourse.org/t/sorry-there-was-an-error-authorizing-your-account/112229)**
>
> Hi there, I´m working for a big company and I´m using an internal oauth2 provider in conjunction with this plugin. In a different business unit there is already another discourse instance running, where the same setup works. I analyzed their...

中的内容。

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

![2023-08-21_04-57-57](https://www.isharkfly.com/images/transparent.png)

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

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

---

<div class="post-metadata">

**Author:** ![whitewatercn](https://www.isharkfly.com/user_avatar/www.isharkfly.com/whitewatercn/32/2131_2.png) [@whitewatercn](https://www.isharkfly.com/u/whitewatercn)\
**Post date:** [August 21, 2023, 9:54am UTC](https://www.isharkfly.com/t/discourse-github/13562/8 "2023-08-21T09:54:45Z")

</div>

我也是照着文档配置的，id和secret也是复制黏贴，应该问题不是在这，参考[Sorry, there was an error authorizing your account - #21 by Beam - SSO - Discourse Meta](https://meta.discourse.org/t/sorry-there-was-an-error-authorizing-your-account/112229/21) 看了下日志

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

```auto
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)

```

---

<div class="post-metadata">

**Author:** ![whitewatercn](https://www.isharkfly.com/user_avatar/www.isharkfly.com/whitewatercn/32/2131_2.png) [@whitewatercn](https://www.isharkfly.com/u/whitewatercn)\
**Post date:** [August 21, 2023, 10:04am UTC](https://www.isharkfly.com/t/discourse-github/13562/9 "2023-08-21T10:04:32Z")

</div>

搞定了！  
需要在`allowed user api auth redirects`设置里，把github的callback地址加进去，才能正常使用github，steam也是同理  
 ![image](https://www.isharkfly.com/images/transparent.png)

开心🥳  
 ![image](https://www.isharkfly.com/images/transparent.png)

---

<div class="post-metadata">

**Author:** ![honeymoose](https://www.isharkfly.com/user_avatar/www.isharkfly.com/honeymoose/32/20796_2.png) [@honeymoose](https://www.isharkfly.com/u/honeymoose)\
**Post date:** [August 21, 2023, 11:23am UTC](https://www.isharkfly.com/t/discourse-github/13562/10 "2023-08-21T11:23:09Z")

</div>

@whitewatercn

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

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

![2023-08-21_07-20-22](https://www.isharkfly.com/images/transparent.png)

根据官方论坛的讨论帖子：[How to set set\_real\_ip\_from for discourse - #9 by parisa - Support - Discourse Meta](https://meta.discourse.org/t/how-to-set-set-real-ip-from-for-discourse/74416/9)

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

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

![2023-08-21_07-21-47](https://www.isharkfly.com/images/transparent.png)

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

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

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