Open link in a new tab

Content

Expand project menu

PHP login failing - 401 error

I'm attempting to build a PHP app in Lavarel to read a listing of my Work Packages . 
I run a self hosted instance of OpenProject 12.5.4, and can read my packages as json using a curl command below

curl -u apikey:my_api_key http://my-server/openproject/api/v3/work_packages

but if I try this function in PHP below, it returns a '401 Unauthorized' response error, and I cannot figure out why.

private function fetchTasks()
{
    $client = new Client();
    $apiKey = 'my_api_key';
    $url = 'http://my-server/openproject/api/v3/work_packages';

    $response = $client->request('GET', $url, [
        'headers' => [
            'Authorization' => 'Basic ' . base64_encode($apiKey . ':'),
            'X-Requested-With' => 'XMLHttpRequest',
        ],
    ]);

    return json_decode($response->getBody(), true);
}

Can anyone assist please?

Message actions

I figured it out from the documentation
I needed to put username "apikey" followed by my_api_key
like this

private function fetchTasks()
{
    $client = new Client();
    $apiKey = 'my_api_key';
    $url = 'http://my-server/openproject/api/v3/work_packages';

    $response = $client->request('GET', $url, [
        'auth' => ['apikey', 'my_api_key'],
        'headers' => [
            'X-Requested-With' => 'XMLHttpRequest',
        ],
    ]);

    return json_decode($response->getBody(), true);
}