# 通过控制台设置 API 密钥

**URL:** https://meta.discourse.org/t/setting-the-api-key-via-console/107472
**Category:** Development
**Created:** [2019年一月24日 22:07 UTC](https://meta.discourse.org/t/setting-the-api-key-via-console/107472 "2019-01-24T22:07:34Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![csmu](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/csmu/32/124581_2.png) [@csmu](https://meta.discourse.org/u/csmu)
#### Post date: [2019年一月24日 22:07 UTC](https://meta.discourse.org/t/setting-the-api-key-via-console/107472/1 "2019-01-24T22:07:34Z")

</div>

Is there a way to set the main api key via the console?

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [2019年一月25日 00:48 UTC](https://meta.discourse.org/t/setting-the-api-key-via-console/107472/2 "2019-01-25T00:48:36Z")

</div>

```bash
YML=app
SSH_USER=root@host
API_KEY=`ssh $SSH_USER sudo docker exec -w /var/www/discourse -i $YML rake api_key:get`

```

---

<div class="post-metadata">

### Author: ![csmu](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/csmu/32/124581_2.png) [@csmu](https://meta.discourse.org/u/csmu)
#### Post date: [2019年一月25日 01:12 UTC](https://meta.discourse.org/t/setting-the-api-key-via-console/107472/3 "2019-01-25T01:12:53Z")

</div>

> [@pfaffman](#):
>
> rake api\_key:get

Does this get or set the api?

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [2019年一月25日 02:15 UTC](https://meta.discourse.org/t/setting-the-api-key-via-console/107472/4 "2019-01-25T02:15:36Z")

</div>

It does both. It sets it if it’s not set already.

---

<div class="post-metadata">

### Author: ![csmu](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/csmu/32/124581_2.png) [@csmu](https://meta.discourse.org/u/csmu)
#### Post date: [2019年一月25日 02:36 UTC](https://meta.discourse.org/t/setting-the-api-key-via-console/107472/5 "2019-01-25T02:36:21Z")

</div>

> [@pfaffman](#):
>
> It sets it if it’s not set already.

Is the api key being passed in and used or is `rake api_key:get` creating one if one does not exist?

Context is rebuilding an instance and keeping the old api key.

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [2019年一月25日 04:10 UTC](https://meta.discourse.org/t/setting-the-api-key-via-console/107472/6 "2019-01-25T04:10:11Z")

</div>

If the database persists then the API key persists.

---

<div class="post-metadata">

### Author: ![csmu](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/csmu/32/124581_2.png) [@csmu](https://meta.discourse.org/u/csmu)
#### Post date: [2019年一月25日 04:20 UTC](https://meta.discourse.org/t/setting-the-api-key-via-console/107472/7 "2019-01-25T04:20:41Z")

</div>

Cool. So it’s in the database somewhere.

Context is building from scratch and testing the importing routines.

Thanks Jay, cheers!

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [2019年一月25日 04:23 UTC](https://meta.discourse.org/t/setting-the-api-key-via-console/107472/8 "2019-01-25T04:23:55Z")

</div>

Have a look at the rake task in `lib/tasks/api_key`.

---

<div class="post-metadata">

### Author: ![Thamer](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/thamer/32/183183_2.png) [@Thamer](https://meta.discourse.org/u/Thamer)
#### Post date: [2020年七月7日 00:24 UTC](https://meta.discourse.org/t/setting-the-api-key-via-console/107472/9 "2020-07-07T00:24:34Z")

</div>

> [@pfaffman](#):
>
> ```plaintext
> YML=app
> SSH_USER=root@host
> API_KEY=`ssh $SSH_USER sudo docker exec -w /var/www/discourse -i $YML rake api_key:get`
> 
> ```

嗨 @pfaffman，我该如何在应用内执行这些步骤？我的意思是，在运行 ./launcher enter app 之后，该如何使用你提到的这些命令？

---

<div class="post-metadata">

### Author: ![justin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/justin/32/157614_2.png) [@justin](https://meta.discourse.org/u/justin)
#### Post date: [2020年七月7日 14:34 UTC](https://meta.discourse.org/t/setting-the-api-key-via-console/107472/10 "2020-07-07T14:34:09Z")

</div>

应该可以直接运行 `rake api_key:get` —— 试试看。我还没查看该命令，所以不确定是否需要任何参数。

---

<div class="post-metadata">

### Author: ![Thamer](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/thamer/32/183183_2.png) [@Thamer](https://meta.discourse.org/u/Thamer)
#### Post date: [2020年七月7日 18:23 UTC](https://meta.discourse.org/t/setting-the-api-key-via-console/107472/11 "2020-07-07T18:23:21Z")

</div>

感谢您的回复，我确实通过此解决方案解决了我的问题：[Locked out of admin account after disabling SSO Memberful - #5 by Thamer](https://meta.discourse.org/t/locked-out-of-admin-account-after-disabling-sso-memberful/156976/5)

关于命令、应用命令等内容，我在哪里可以了解更多？目前很难找到一些基本信息，而且在需要解决问题时也感到棘手。

---

<div class="post-metadata">

### Author: ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)
#### Post date: [2020年七月8日 12:24 UTC](https://meta.discourse.org/t/setting-the-api-key-via-console/107472/12 "2020-07-08T12:24:28Z")

</div>

你可以

```
./launcher enter app
 rake api_key:get

```

或许可以看看 [管理批量操作](https://meta.discourse.org/t/administrative-bulk-operations/118349)
