现在还有其他人在遇到这个问题吗?我这两天一直在尝试使用 API,但每次都遇到类似的错误。我不确定是自己漏掉了某个步骤,还是有什么地方发生了变化。
我为 system 用户创建了一个新的 API 密钥。我也尝试为所有用户创建 API 密钥,然后使用我的管理员账户。
使用 discourse_api Ruby gem 时出现以下错误:
DiscourseApi::UnauthenticatedError ({"errors"=>["您无权查看请求的资源。API 用户名或密钥无效。"], "error_type"=>"invalid_access"})
我还尝试了另一篇帖子中的这段代码:
require 'net/http'
require 'uri'
INSTANCE_URL = 'https://forum.example.com/admin/users/list/all.json?email=user@example.com'
API_USERNAME = 'system'
API_KEY = 'a_new_api_key_here'
def send_request
url = URI.parse(INSTANCE_URL)
request = Net::HTTP::Get.new(url.path)
request.set_form_data({'api_username' => API_USERNAME, 'api_key' => API_KEY})
http = Net::HTTP.new(url.host, url.port)
response = http.request(request)
if response.code == '200'
puts "成功!"
else
puts "错误"
puts response.code
end
end
send_request # 400
我还尝试将 api_username 和 api_key 作为表单数据与原始请求一起使用 API,但收到了 404 错误。
我正在尝试编写一个脚本,读取电子邮件地址列表,获取用户名,然后将他们取消订阅所有 Discourse 邮件。