Discussions
401 Unauthorized in /v1/recruiting/applications
13 hours ago by Dominik Seibert
Hi there,
I think I am facing the same or a very similar issue to this one: https://developer.personio.de/discuss/6391fcef55b389004f270845
The behaviour is exactly the same. If I omit required fields, I receive an 400 error, otherwise it results in a 401.
Here is an excerpt of the script I created:
// retrieve personio api token
$response = wp_remote_post('https://api.personio.de/v1/auth', [
'headers' => [
'Accept' => 'application/json',
'Content-Type' => 'application/json',
'X-Company-ID' => $company_id,
],
'body' => \json_encode([
'client_id' => $client_id,
'client_secret' => $client_secret,
]),
]);
if (is_wp_error($response)) {
error_log('Personio Auth API error: ' . $response->get_error_message());
return;
}
$raw_response = wp_remote_retrieve_body( $response );
$body = \json_decode($raw_response, true);
if (!$body || !isset($body['data']) || !isset($body['data']['token'])) {
error_log('Personio Auth API error: Response could not be decoded');
return;
}
$api_token = $body['data']['token'];
// transform post data
$data = $record->get( 'fields' );
$body = [
'first_name' => $data['first_name'] ?? '',
'last_name' => $data['last_name'] ?? '',
'email' => $data['email'] ?? '',
'job_position_id' => $job_position_id,
'recruiting_channel_id' => $recruiting_channel_id,
'application_date' => date('Y-m-d'),
'message' => $data['motivation'] ?? '',
'attributes' => [
[
'id' => 'custom_attribute_4382494', // id of custom attribute "Telefonnummer"
'value' => $data['phone'] ?? '',
],
],
];
// send data to personio
$response = wp_remote_post('https://api.personio.de/v1/recruiting/applications', [
'headers' => [
'Authorization' => \sprintf('Bearer %s', $api_token),
'Content-Type' => 'application/json',
'Accept' => 'application/json',
'X-Company-ID' => $company_id,
],
'body' => \json_encode($body),
]);
if (is_wp_error($response)) {
error_log('Personio API error: ' . $response->get_error_message());
return;
}
error_log('Personio API response: ' . print_r($response, true));
Feel free to reach out to me if you have any further questions.
Thanks in advance and kind regards,
Dominik
