# Disable account confirm emails when creating users via API

**URL:** https://meta.discourse.org/t/disable-account-confirm-emails-when-creating-users-via-api/68663
**Category:** Development
**Tags:** rest-api
**Created:** [2017年八月23日 23:18 UTC](https://meta.discourse.org/t/disable-account-confirm-emails-when-creating-users-via-api/68663 "2017-08-23T23:18:20Z")
**Posts on this page:** 8
**Page:** 2

<div class="post-metadata">

### Author: ![blake](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/blake/32/157322_2.png) [@blake](https://meta.discourse.org/u/blake)
#### Post date: [2020年一月28日 19:17 UTC](https://meta.discourse.org/t/disable-account-confirm-emails-when-creating-users-via-api/68663/33 "2020-01-28T19:17:25Z")

</div>

> [@dm.linov](#):
>
> 问题在于，对于步骤 2，我需要知道用户 ID，该 ID 本应出现在步骤 1 的响应中，但实际上并没有。

如果用户确实已创建，响应中应该会包含 `user_id`。但考虑到你在管理后台中找不到该用户，可能由于某种原因用户并未成功创建。

> [@dm.linov](#):
>
> 我打算从另一个平台（YouTrack，一个问题跟踪系统）迁移数据，并希望尽可能让我们的用户感受到平滑无缝的体验。

好的，明白了，你正在开发一个导入器。你不应该通过 API 调用来实现这一点，而是应该创建一个导入脚本，并直接在 Docker 容器内部运行它。

你可以参考 [discourse/script/import\_scripts at main · discourse/discourse · GitHub](https://github.com/discourse/discourse/tree/master/script/import_scripts) 中列出的众多示例。

---

<div class="post-metadata">

### Author: ![dm.linov](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/dm.linov/32/167719_2.png) [@dm.linov](https://meta.discourse.org/u/dm.linov)
#### Post date: [2020年一月28日 19:23 UTC](https://meta.discourse.org/t/disable-account-confirm-emails-when-creating-users-via-api/68663/34 "2020-01-28T19:23:50Z")

</div>

> [@blake](#):
>
> 你不应该通过 API 调用来执行此操作，而是应该创建一个导入脚本

我可以问问原因吗？  
（我有 Discourse 的管理员权限，但没有服务器/Docker 容器的权限，所以我需要请别人来做这件事。）

> [@blake](#):
>
> 它们可能由于某种原因未被创建

看起来确实如此！有什么已知的原因吗？是否与某些设置有关？

---

<div class="post-metadata">

### Author: ![blake](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/blake/32/157322_2.png) [@blake](https://meta.discourse.org/u/blake)
#### Post date: [2020年一月28日 19:43 UTC](https://meta.discourse.org/t/disable-account-confirm-emails-when-creating-users-via-api/68663/35 "2020-01-28T19:43:02Z")

</div>

> [@dm.linov](#):
>
> 我可以问一下原因吗？

如果是大规模导入，速度会快 100 倍，而且您可以直接访问 `ImportScripts::Base` 类的各种方法，这使得创建用户以及导入其他内容（如创建群组、分类、主题和帖子）变得超级简单：

> <https://github.com/discourse/discourse/blob/main/script/import_scripts/base.rb#L284>

> [@dm.linov](#):
>
> （我有 Discourse 的管理员权限，但没有服务器/Docker 容器的权限，所以我需要请别人来操作。）

如果需要，您可以编写脚本并让他们来运行。

> [@dm.linov](#):
>
> 看起来确实如此！是否有任何已知的原因？

目前没有发现与此相关的已知问题。以下是一个用于创建用户的 curl 请求示例：

```plaintext
curl -i -sS -X POST "http://localhost:3000/users" \
-H "Content-Type: multipart/form-data;" \
-H "Api-Key: ****" \
-H "Api-Username: system" \
-F "name=cfbcc77ae9f4230d2c25" \
-F "username=cfbcc77ae9f4230d2c25" \
-F "email=cfbcc77ae9f4230d2c25@example.com" \
-F "password=3f348653105f05505f5f1a3ff70ef984" \
-F "active=true" \
-F "user_fields[1]=cfbcc77ae9f4230d2c25" \
-F "user_fields[2]=828c0ec3a76"

```

返回结果为：

```plaintext
{"success":true,"active":false,"message":"\u003cp\u003e您快完成了！我们已向 \u003cb\u003ecfbcc77ae9f4230d2c25@example.com\u003c/b\u003e 发送了一封激活邮件。请按照邮件中的说明激活您的账户。\u003c/p\u003e\u003cp\u003e如果未收到邮件，请检查您的垃圾邮件文件夹。\u003c/p\u003e","user_id":4}

```

可能某些地方发生了变化，因为看起来邮件现在确实已发送，但用户仍然被创建为未激活状态。看起来 `active=true` 参数并未生效。

---

<div class="post-metadata">

### Author: ![dm.linov](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/dm.linov/32/167719_2.png) [@dm.linov](https://meta.discourse.org/u/dm.linov)
#### Post date: [2020年一月28日 20:03 UTC](https://meta.discourse.org/t/disable-account-confirm-emails-when-creating-users-via-api/68663/36 "2020-01-28T20:03:54Z")

</div>

> [@blake](#):
>
> 如果需要，你可以创建脚本并让他们运行它。

我也得承认，我对 Ruby 一无所知：🤦‍♂️

因此，我将尝试进一步调查 API 问题。我会尝试运行一个与你类似的调用。只需澄清几点：  
a) 端点是 `[mydomain]/users.json`，对吗？  
b) Content-Type 应该是 `"multipart/form-data"` 还是 `"application/json"`，[如 API 文档所述](https://docs.discourse.org/#tag/Users/paths/~1users/post)？  
c) `"user_fields"` 的用途是什么？

---

<div class="post-metadata">

### Author: ![blake](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/blake/32/157322_2.png) [@blake](https://meta.discourse.org/u/blake)
#### Post date: [2020年一月28日 20:22 UTC](https://meta.discourse.org/t/disable-account-confirm-emails-when-creating-users-via-api/68663/37 "2020-01-28T20:22:39Z")

</div>

是的，通常您希望为所有路由添加 .json 后缀。是的，您也可以将 content-type 设置为 `application/json`，并且 [user\_fields 是可选的](https://meta.discourse.org/t/how-to-create-and-configure-custom-user-fields/113192)，它只是我已有示例脚本的一部分。

---

<div class="post-metadata">

### Author: ![dm.linov](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/dm.linov/32/167719_2.png) [@dm.linov](https://meta.discourse.org/u/dm.linov)
#### Post date: [2020年一月29日 09:30 UTC](https://meta.discourse.org/t/disable-account-confirm-emails-when-creating-users-via-api/68663/38 "2020-01-29T09:30:24Z")

</div>

成功了！

使用完全相同的 curl 请求（仅替换我的数据）成功创建了一个 **已激活** 的用户。  
目前还不确定为什么我之前从 Postman 发送类似请求时未能成功。我会进一步调查这个问题，如果有重要发现会在此汇报。

感谢您的帮助！

---

<div class="post-metadata">

### Author: ![blake](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/blake/32/157322_2.png) [@blake](https://meta.discourse.org/u/blake)
#### Post date: [2020年八月5日 14:03 UTC](https://meta.discourse.org/t/disable-account-confirm-emails-when-creating-users-via-api/68663/39 "2020-08-05T14:03:31Z")

</div>

此问题已通过以下提交解决：

> <https://github.com/discourse/discourse/commit/fec68d3d25bfc80d084af52e17882ad886fd5cd0>
>
> This commit is for a frequently requested task on meta so that only 1
> API call i…s needed instead of 3!
> 
> In order to create a user via the api and not have them receive an
> activation email you can pass in the \`active=true\` flag. This prevents
> sending an email, but it is only half of the solution and puts the db in
> a weird state where it has an active user with an unconfirmed email.
> 
> This commit fixes that and ensures that if the \`active=true\` flag is set
> the user's email is also marked as confirmed.
> 
> This change only applies to admins using the API.
> 
> Related topics on meta:
> 
> - https://meta.discourse.org/t/-/68663
> - https://meta.discourse.org/t/-/33133
> - https://meta.discourse.org/t/-/36133

---

<div class="post-metadata">

### Author: ![system](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/system/32/443519_2.png) [@system](https://meta.discourse.org/u/system)
#### Post date: [2020年九月4日 14:03 UTC](https://meta.discourse.org/t/disable-account-confirm-emails-when-creating-users-via-api/68663/40 "2020-09-04T14:03:42Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.

[上一頁](https://meta.discourse.org/t/disable-account-confirm-emails-when-creating-users-via-api/68663.md?page=1)
