无法使用 OAuth 获取 ExactOnline 的访问令牌

Can't get access token for ExactOnline with OAuth

我正在按照 OAuth 教程 here 获取访问代码,以便验证我对在线会计软件 Exact Online 的 API 请求。

然而,我卡在了第 3 步,我使用第 2 步中返回的授权码来获取访问令牌。

这是我正在尝试的:

require 'httparty'

EXACT_CLIENT_ID = '<REDACTED>'
EXACT_CLIENT_SECRET = '<REDACTED>'
EXACT_SERVER_BASE_URL = 'https://start.exactonline.nl'
EXACT_AUTH_CODE = '<REDACTED>'

response = HTTParty.post("#{EXACT_SERVER_BASE_URL}/api/oauth2/token", headers: {'Content-Type' => 'application/x-www-form-urlencoded'}, query: {code: EXACT_AUTH_CODE, redirect_uri: 'http://<REDACTED>.runscope.net/', grant_type: 'authorization_code', client_id: EXACT_CLIENT_ID, client_secret: EXACT_CLIENT_SECRET})

puts response
# => 'Bad request'

puts response.code
# => 400

我不明白为什么会这样。查看响应代码列表 here 时,代码表示:

The request could not be understood by the server due to malformed syntax. The client SHOULD NOT repeat the request without modifications.

我做错了什么?

更新#1:

我也试过:

response_2 = HTTParty.post("#{EXACT_SERVER_BASE_URL}/api/oauth2/token", :headers => {'Content-Type' => 'application/x-www-form-urlencoded'}, :body => {'code' => EXACT_AUTH_CODE, 'redirect_uri' => 'http://<REDACED>.runscope.net/', 'grant_type' => 'authorization_code', 'client_id' => EXACT_CLIENT_ID, 'client_secret' => EXACT_CLIENT_SECRET})

但反应是一样的

即使使用 HTTP POST 方法,您也通过将值提供给 .post 方法的 query: 参数来提供这些值作为查询参数。相反,您应该在 body: 参数中提供它们,请参阅:

还有你的语法。 body: :body 应该是固定的,所以它看起来像:

response_2 = HTTParty.post("#{EXACT_SERVER_BASE_URL}/api/oauth2/token", :headers => {'Content-Type' => 'application/x-www-form-urlencoded'}, :body => {'code' => EXACT_AUTH_CODE, 'redirect_uri' => 'http://<REDACED>.runscope.net/', 'grant_type' => 'authorization_code', 'client_id' => EXACT_CLIENT_ID, 'client_secret' => EXACT_CLIENT_SECRET})

最后但同样重要的是:代码值是一次性使用的,并且生命周期很短;确保使用新获得的。