带有httr的POST请求后的无效内容类型

罗兰

我正在尝试使用可从法国公共就业服务局(PôleEmploi)获得的名为“ Offres d'emploi v2”(职位空缺)的API。此处描述API 此处描述的过程中,使用API​​需要令牌和通过OAuth v2的身份验证

我正在使用R 3.5.0和httr 1.3.1。首先,我指定请求正文。eeideesec是标识,当我注册-用极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

请注意,eeideesec是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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

带有身份验证令牌的httr POST请求

POST请求后没有内容

POST请求有效内容的内容类型

在带有SuiteScript 2.0的NetSuite中,无法通过内容类型为multipart / form-data的HTTP POST请求发送文件

空手道POST请求返回了REST POST事务的无效内容类型消息

使用HTTR的POST请求

带有POST请求的415(不受支持的媒体类型)

由于签名无效,带有OAuth1a的POST请求和请求正文失败

httr请求,在r中带有多个标头

带有python请求库的无效网址

具有多部分形式的角后请求具有错误的内容类型

在带有 redux 形式的 react js 中不使用 axios 发送 post 请求(在 post 请求中不发送任何内容)

如何发送带有内容类型x-www-form-urlencoded android改造的发布请求

dart:io中具有json内容类型的Http POST请求

为什么对于标准内容类型的POST请求,CORS中没有预检

服务器上具有内容类型验证的POST请求的CSRF检测

Netlify开发人员表单请求的内容类型无效

带有 python 请求的 dropbox 中的无效请求

带有NodeJS的Azure函数:在POST请求后检索documentDB生成的ID

cURL XML POST有效但Python请求无效-第三方专家评级API(带有示例)

带有基本 POST 请求的 OAuth 令牌

带有POST请求的AutoMapper的实际用法

Swift中带有键值对的POST请求

带有Json正文的HttpClient Post请求

带有JSON的HTTP POST请求

带有请求的Python HTML POST

带有附件的 Azure 函数 POST 请求

带有Android的HTTP POST请求崩溃

带有HTML的POST请求的威胁结果