【发布时间】:2018-09-29 14:43:25
【问题描述】:
我正在尝试使用法国公共就业服务中心 (Pôle Emploi) 提供的名为“Offres d'emploi v2”(职位空缺)的 API。 API 描述为here。在here 描述的过程中,使用 API 需要令牌和通过 OAuth v2 进行的身份验证。
我正在使用 R 3.5.0 和 httr 1.3.1。首先,我指定请求正文。 eeid 和 eesec 是我注册时 Pôle Emploi 提供的标识符和密钥。
require(jsonlite)
require(httr)
request_body <- list(
grant_type = "client_credentials",
client_id = eeid,
client_secret = eesec,
scope = paste(
"api_offresdemploiv2",
"o2dsoffre",
paste0("application_",eeid,"%20api_offresdemploiv2"), sep = " "))
然后,我运行 POST 请求:
result_auth <- POST(
"https://entreprise.pole-emploi.fr/connexion/oauth2/access_token",
realm = "/partenaire",
body = request_body,
add_headers('Content-Type'='application/x-www-form-urlencoded')
)
result_auth
content(result_auth)
返回有关内容类型的错误:
> result_auth
Response [https://entreprise.pole-emploi.fr/connexion/oauth2/access_token]
Date: 2018-09-29 14:33
Status: 400
Content-Type: application/json; charset=UTF-8
Size: 70 B
> content(result_auth)
$error
[1] "invalid_request"
$error_description
[1] "Invalid Content Type"
我还尝试将add_headers('Content-Type'='application/x-www-form-urlencoded') 行替换为content_type("application/x-www-form-urlencoded"),但我收到相同的错误消息。
我显然在这里做错了什么,但是什么?谢谢你的帮助。
【问题讨论】:
-
不要手动添加该标题。只需将
encode = "form"添加为POST参数即可。 -
@hrbrmstr 成功了,非常感谢!
-
@Roland。如果 hrbrmstr 太忙而无法起草更完整的答案,那么您可以编写自己的答案并将其标记为已回答。这是完全可以接受的行为,并且为寻找类似问题的人增加了网站的价值。 (您可能应该首先进行搜索以确保它不是重复的。
-
@42 谢谢。我想知道这是否是好的行为,确实。我会做的。