# 如何根据用户当前位置投放内部广告？

**URL:** https://meta.discourse.org/t/is-there-a-way-to-serve-in-house-ads-based-on-the-current-users-location/280263
**Category:** Development
**Tags:** rest-api
**Created:** [2023年九月27日 06:39 UTC](https://meta.discourse.org/t/is-there-a-way-to-serve-in-house-ads-based-on-the-current-users-location/280263 "2023-09-27T06:39:32Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![hyphalos](https://avatars.discourse-cdn.com/v4/letter/h/3ab097/32.png) [@hyphalos](https://meta.discourse.org/u/hyphalos)
#### Post date: [2023年九月27日 06:39 UTC](https://meta.discourse.org/t/is-there-a-way-to-serve-in-house-ads-based-on-the-current-users-location/280263/1 "2023-09-27T06:39:32Z")

</div>

你好！

我想确认一下，是否有 Discourse 的方法可以根据当前用户的所在地来投放内部广告？我了解我们有：[House Ads in the official Ad Plugin](https://meta.discourse.org/t/house-ads-in-the-official-ad-plugin/116715) 用于投放广告，但我只是想知道是否可以通过 Discourse 来获取当前用户的位置。

如果 Discourse 没有这种方法（例如需要第三方工具），我知道我们也有注册时的 IP 地址，是否可以通过这些 IP 地址来确定用户的位置？

谢谢！

---

<div class="post-metadata">

### Author: ![Canapin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/canapin/32/119591_2.png) [@Canapin](https://meta.discourse.org/u/Canapin)
#### Post date: [2023年九月27日 07:03 UTC](https://meta.discourse.org/t/is-there-a-way-to-serve-in-house-ads-based-on-the-current-users-location/280263/2 "2023-09-27T07:03:58Z")

</div>

你好 hyphalos！ :blob_wave:

> [@hyphalos](#):
>
> 有什么方法可以用来确定用户的位置吗？

无论是在现有广告插件还是假设的广告插件的上下文中，Discourse 在管理员视图的用户配置文件中都提供了一个内置功能：

 ![image](https://global.discourse-cdn.com/meta/original/4X/f/4/c/f4c8feed79238b38f8fb62decc53d708492504f4.png)

该端点是：`https://your-discourse.com/admin/users/ip-info.json?ip={user_ip}`，通过 GET 请求。

---

<div class="post-metadata">

### Author: ![hyphalos](https://avatars.discourse-cdn.com/v4/letter/h/3ab097/32.png) [@hyphalos](https://meta.discourse.org/u/hyphalos)
#### Post date: [2023年九月27日 07:37 UTC](https://meta.discourse.org/t/is-there-a-way-to-serve-in-house-ads-based-on-the-current-users-location/280263/3 "2023-09-27T07:37:06Z")

</div>

> [@Canapin](#):
>
> [https://your-discourse.com/admin/users/ip-info?ip={user\_ip}](https://your-discourse.com/admin/users/ip-info?ip=%7Buser_ip%7D)

您好 @Canapin，

感谢您的快速回复！

我尝试为此编写了一个简短的 Python 脚本，但它只返回一个网页而不是 JSON。当我将 `.json` 添加到 GET URL 时，它只返回 `{}`。但是，当我使用用户 ID (`https://your-discourse.com/admin/users/{user_id}/ip-info.json?ip={user_ip}`) 发出请求时，确实会出现带有 IP 地址的用户信息。

我甚至尝试从浏览器访问该 URL，但只收到“哎呀！该页面不存在或已设为私有。”的提示。

顺便说一句，我们的 Discourse 目前是稳定版本（v3.1.1）。

我想问一下，我是否需要 MaxMind 许可证才能使其正常工作？如果不是，是否有可能提供的端点不正确？我从 `/admin/users/{user_id}.json` 收到了响应，但出于某种原因，我没有从 `/admin/users/ip-info?ip={user_ip}` 获得正确的响应。我只从我们的 Discourse 网站收到一个空白的 HTML 网页。

此外，是否有 Discourse 提供的有用端点列表链接，例如您分享的那个？

谢谢！

---

<div class="post-metadata">

### Author: ![Canapin](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/canapin/32/119591_2.png) [@Canapin](https://meta.discourse.org/u/Canapin)
#### Post date: [2023年九月27日 08:15 UTC](https://meta.discourse.org/t/is-there-a-way-to-serve-in-house-ads-based-on-the-current-users-location/280263/4 "2023-09-27T08:15:47Z")

</div>

> [@hyphalos](#):
>
> 但是，当我使用用户 ID (`https://your-discourse.com/admin/users/{user_id}/ip-info.json?ip={user_ip}`) 发出请求时，用户的 IP 地址信息确实出现了。

那是因为它只返回 `https://your-discourse.com/admin/users/{user_id}.json` 的内容。它忽略了 `ip-info` 部分，这里不是一个有效的端点。

是的，我没考虑到这一点 🤦，但是的，我认为你可能需要一个 Maxmind 密钥才能返回位置信息。

所以，总结一下：

1. 如果你想获取用户的 IP，你需要向 `https://your-discourse.com/admin/users/{user_id}.json` 发送一个 GET 请求（ **需要管理员凭据** ）。它将返回 IP 地址以及其他用户信息。

2. 如果你想定位 IP 地址，你需要一个 Maxmind 密钥（添加到 `app.yml` 中），然后向此端点发送一个 GET 请求：`https://your-discourse.com/admin/users/ip-info.json?ip={user_ip}`。

> [@hyphalos](#):
>
> 此外，是否有 Discourse 提供的有用端点列表链接，例如你分享的那个？

这里有 API 文档：[https://docs.discourse.org/](https://docs.discourse.org/)  
但并非所有端点都列在这里。Discourse 的 API 非常容易逆向工程：[Reverse engineer the Discourse API](https://meta.discourse.org/t/reverse-engineer-the-discourse-api/20576?u=canapin%E3%80%82%E8%BF%99%E5%B0%B1%E6%98%AF%E6%88%91%E8%8E%B7%E5%8F%96%E4%BF%A1%E6%81%AF%E7%9A%84%E6%96%B9%E5%BC%8F%E3%80%82)

---

<div class="post-metadata">

### Author: ![hyphalos](https://avatars.discourse-cdn.com/v4/letter/h/3ab097/32.png) [@hyphalos](https://meta.discourse.org/u/hyphalos)
#### Post date: [2023年九月27日 08:55 UTC](https://meta.discourse.org/t/is-there-a-way-to-serve-in-house-ads-based-on-the-current-users-location/280263/5 "2023-09-27T08:55:27Z")

</div>

感谢 @Canapin，我认为这正是我需要的。我会看看是否能搞定一个 MaxMind 许可证，然后继续。如果我获得了许可证并重新尝试，我会在此更新此帖。谢谢！

---

<div class="post-metadata">

### Author: ![hyphalos](https://avatars.discourse-cdn.com/v4/letter/h/3ab097/32.png) [@hyphalos](https://meta.discourse.org/u/hyphalos)
#### Post date: [2023年九月29日 04:00 UTC](https://meta.discourse.org/t/is-there-a-way-to-serve-in-house-ads-based-on-the-current-users-location/280263/6 "2023-09-29T04:00:50Z")

</div>

您好 @Canapin，

我已经获得了一个免费的 MaxMind 许可证，并且 API `https://your-discourse.com/admin/users/ip-info.json?ip={user_ip} .` 运行得非常好，谢谢！

我还有一个额外的问题，因为这需要用户 ID 来获取 IP，然后才能调用上面的 API，那么匿名用户该怎么办？如果我们也想为他们提供广告服务，但他们没有用户 ID，该怎么办？这是否仍然可行，还是仅限于已登录用户？

---

<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: [2023年十月29日 04:01 UTC](https://meta.discourse.org/t/is-there-a-way-to-serve-in-house-ads-based-on-the-current-users-location/280263/7 "2023-10-29T04:01:29Z")

</div>

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