我正在尝试使用可从法国公共就业服务局(PôleEmploi)获得的名为“ Offres d'emploi v2”(职位空缺)的API。此处描述了API 。在此处描述的过程中,使用API需要令牌和通过OAuth v2的身份验证。
我正在使用R 3.5.0和httr 1.3.1。首先,我指定请求正文。eeid
并eesec
是标识,当我注册-用极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")
,但我得到了同样的错误消息。
我显然在这里做错了,但是呢?谢谢你的帮助。
这是@hrbrmstr发表的评论之后的答案。非常感谢他。
与其指定内容类型为标题,不如使用函数中的encode = "form"
选项POST
。
请注意,eeid
和eesec
是PôleEmploi在注册时提供的标识符和密钥。完整的脚本如下所示。
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), sep = " "))
result_auth <- POST(
"https://entreprise.pole-emploi.fr/connexion/oauth2/access_token",
query = list(realm = "/partenaire"),
body = request_body,
encode = "form"
)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句