# Discourse\_api URL 未找到错误

**URL:** https://meta.discourse.org/t/discourse-api-url-not-found-error/168071
**Category:** Development
**Tags:** rest-api
**Created:** [2020年十月23日 12:58 UTC](https://meta.discourse.org/t/discourse-api-url-not-found-error/168071 "2020-10-23T12:58:00Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![DiscourseMetrics](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/discoursemetrics/32/55756_2.png) [@DiscourseMetrics](https://meta.discourse.org/u/DiscourseMetrics)
#### Post date: [2020年十月23日 12:58 UTC](https://meta.discourse.org/t/discourse-api-url-not-found-error/168071/1 "2020-10-23T12:58:00Z")

</div>

大家好，

我正在尝试使用 discourse\_api gem：

```
client = DiscourseApi::Client.new("https://<mysite>.com")
client.api_key = "<my global key>"
client.api_username = "system"

```

作为基本设置，然后

`client.latest_topics`

能正确返回数据，但是……

`client.get_dashboard_stats`

却返回了一个 URL“未找到”错误。我猜测当某个路由需要密钥时会出问题，因为 `client.latest_topics` 是正常工作的。

我已经反复确认密钥是正确的，甚至重新生成过一个密钥并尝试使用，同时也勾选了全局访问选项。URL 也是正确的。还有其他需要检查的地方吗？🙂

祝好！

---

<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年十月23日 14:13 UTC](https://meta.discourse.org/t/discourse-api-url-not-found-error/168071/2 "2020-10-23T14:13:30Z")

</div>

这很可能是一个 API 密钥认证问题。

如果您在无痕浏览器窗口中打开未登录的站点，并访问该路由 `https://yoursite/admin/dashboard.json`，将返回：

```plaintext
{"errors":["The requested URL or resource could not be found."],"error_type":"not_found"}

```

但如果您已登录站点并访问同一 URL，则应看到 JSON 结果。

另外，请检查 API 密钥是设置为“所有用户”还是“单个用户”。如果设置为单个用户，请确保其与发起 API 请求的用户匹配。

话虽如此，看起来 API gem 中的两个仪表板端点需要更新。`get_dashboard_stats` 仍然可以工作，但返回的信息不如以前多，而 `get_dashboard_stats_totals` 似乎也需要更新。

---

<div class="post-metadata">

### Author: ![DiscourseMetrics](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/discoursemetrics/32/55756_2.png) [@DiscourseMetrics](https://meta.discourse.org/u/DiscourseMetrics)
#### Post date: [2020年十月23日 21:58 UTC](https://meta.discourse.org/t/discourse-api-url-not-found-error/168071/3 "2020-10-23T21:58:39Z")

</div>

我同意这很可能与密钥有关，但我已尝试使用“所有用户”和“单用户”设置（使用管理员用户名），仍然收到您发布的“未找到”错误。

密钥是正确的，URL 也是正确的，我还尝试了所有不同的设置以及其他在 API 示例中列出的端点。所有请求都返回“请求的 URL 或资源未找到”。我想不出还有什么其他办法了：smiley: 😅 我猜对于全局密钥，用户权限范围在这里不适用？

---

<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年十月23日 22:11 UTC](https://meta.discourse.org/t/discourse-api-url-not-found-error/168071/4 "2020-10-23T22:11:16Z")

</div>

你能运行以下命令

`gem list discourse_api`

来查看你正在使用的 discourse\_api gem 的版本吗？

---

<div class="post-metadata">

### Author: ![DiscourseMetrics](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/discoursemetrics/32/55756_2.png) [@DiscourseMetrics](https://meta.discourse.org/u/DiscourseMetrics)
#### Post date: [2020年十月23日 22:29 UTC](https://meta.discourse.org/t/discourse-api-url-not-found-error/168071/5 "2020-10-23T22:29:44Z")

</div>

0.10.0 —— 这大概就是原因，它试图使用 URL 参数而不是 HTTP 头：slight\_smile 谢谢！

---

<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年十一月22日 22:29 UTC](https://meta.discourse.org/t/discourse-api-url-not-found-error/168071/6 "2020-11-22T22:29:52Z")

</div>

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