【问题标题】:Invalid Content Type after a POST request with httr使用 httr 的 POST 请求后内容类型无效
【发布时间】: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。首先,我指定请求正文。 eeideesec 是我注册时 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 谢谢。我想知道这是否是好的行为,确实。我会做的。

标签: r api oauth-2.0 httr


【解决方案1】:

这是直接在@hrbrmstr 评论之后的答案。非常感谢他。

不应将内容类型指定为标头,而应在POST 函数中使用encode = "form" 选项。

请注意,eeideesec 是 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), sep = " "))

result_auth <- POST(
    "https://entreprise.pole-emploi.fr/connexion/oauth2/access_token",
    query = list(realm = "/partenaire"),
    body = request_body,
    encode = "form"
    )

【讨论】:

  • 嗨@Roland,我可以得到授权令牌,但像这样的简单查询将失败 GET("api.emploi-store.fr/partenaire/offresdemploi/v2/offres/search", add_headers(Authorization = paste("Bearer" , 键, sep = " ")), 查询 = "range-0-49")。有什么想法吗?
  • 这样的工作:search_body &lt;- list(publieeDepuis = 1, commune = "34172", domaine = "M11", distance = 1); search_url &lt;- "https://api.emploi-store.fr/partenaire/offresdemploi/v2/offres/search"; search_header &lt;- c("Authorization" = paste("Bearer",bearer_token)); result &lt;- GET( url = search_url, config = add_headers(search_header), body = search_body)
  • 谢谢!出于某种原因,在获取授权令牌时,我不得不将 paste0("application_",eeid,"%20api_offresdemploiv2") 更改为仅 paste0("application_",eeid) 。不知道为什么...
  • 你说得对,谢谢,我会编辑答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-09
  • 2021-03-06
  • 1970-01-01
  • 1970-01-01
  • 2020-10-21
  • 1970-01-01
相关资源
最近更新 更多