通过 API 获取用户的电子邮件地址

是否可以通过 API 获取用户的当前电子邮件地址?

我们的使用场景如下:

我们允许用户通过我们的网站管理他们的 Discourse 信息。通常情况下,Discourse 仅用作邮件列表,几乎所有用户都不会访问实际的 Discourse 站点。我希望能够向用户显示 Discourse 当前使用的电子邮件地址,以便他们查看是否需要更改。

除电子邮件外,我能够获取所有其他字段的信息。

过去存在一个 API 端点 users/{username}/emails,但该端点似乎已不再可用。

Hi,

GET /users/:username.json

will display the email in two cases:

  • the user id of the API KEY holder is the same than the user id related to the requested username
  • the API KEY holder is staff and the requested user is staged

Read more about this in code:

AFAIK this codepath has not changed recently.

3 个赞

Yeah, but that doesn’t help me. All of the users are already active and the user API key is not going to match.

This request does work for me:

curl "http://example.com/users/joffreyjaffeux/emails.json" \
     -H 'Api-Username: joffreyjaffeux' \
     -H 'Api-Key: xxx' \
     -H 'Content-Type: application/json; charset=utf-8' \
3 个赞

But, does it work if the URL is http://example.com/users/paulapostolos/emails.json ?

Yes it should work for yourself and other users as long as this is true:

  def can_check_emails?(user)
    is_admin? || (is_staff? && SiteSetting.moderators_view_emails)
  end

Note that doing so will trigger a staff action log.

4 个赞

That worked…I had some old code that used to work (version 1.4 ish) that had the request as a PUT and without the .json at the end. I modified it to a GET and changed the URL and now it works.

Thanks for the help!

3 个赞