I am trying to use the API named "Offres d'emploi v2" (Job vacancies) available from the French Public Employment Service (Pôle Emploi). The API is described here. Using the API requires a token, and an authentification via OAuth v2, in a process described here.
I'm using R 3.5.0 and httr 1.3.1. First, I specify the request body. eeid
and eesec
are the identifier and the secret key delivered by Pôle Emploi when I registered.
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 = " "))
Then, I run the POST request:
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)
which returns an error about the content type:
> 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"
I also tried to replace the line add_headers('Content-Type'='application/x-www-form-urlencoded')
with content_type("application/x-www-form-urlencoded")
, but I get the same error message.
I'm obviously doing something wrong here, but what? Thanks for your help.
Here is an answer following directly the comment by @hrbrmstr. Many thanks to him.
Instead of specifying the content type as a header, one should use the encode = "form"
option in the POST
function.
Note that eeid
and eesec
are the identifier and the secret key delivered by Pôle Emploi at registration. The complete script looks like this.
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"
)