jeudi 8 août 2019

Laravel curl request resulted in a `403 Forbidden` response

I am working on a laravel based project and I need to import time entries from Toggl account. Toggl seems to have correct API in place but I am not able to pull records via API for some reason.

I am using the below-mentioned library to access Toggl API. https://medium.com/laravel-5-the-right-way/using-guzzlehttp-with-laravel-1dbea1f633da

I am getting below error on every request to API. GuzzleHttp \ Exception \ ClientException (403) Client error: GET https://toggl.com/api/v8/time_entries?user_agent=deepak%40gmail.com&workspace_id=257895&with_related_data=1 resulted in a 403 Forbidden response

I have tried to fix it by adding or removing (below-mentioned) header elements that are sent with each request. But it still does not work.

use GuzzleHttp\Exception\GuzzleException; use GuzzleHttp\Client;

    $client = new Client;
    $credentials = base64_encode('e5e8238ca0fee3ca726e486733c87456:api_token');
    $url = 'https://toggl.com/api/v8/time_entries';

    $response = $client->get($url, 
    [
        'referer' => true,
        'headers' => [
            'Authorization: Basic ' => $credentials,
            'Content-Type: application/json',
            'User-Agent' => 'Mozilla /5.0 (Compatible MSIE 9.0;Windows NT 6.1;WOW64; Trident/5.0)',
            'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
            'Accept-Encoding' => 'gzip, deflate, br'
        ],

        'query' => [
            'user_agent'=>'deepak@gmail.com',
            'workspace_id'=>'8756952',
            'with_related_data' => true

        ],
        "http_errors" => false,
    ]);
    dd($response);

It should retrieve the records from Toggl and print a JASON on the browser.

Interestingly, I am able to pull records through the command-line interface using the command below. curl -v -u e5e8238ca0fee3ca726e486733c85468:api_token -X GET https://www.toggl.com/api/v8/time_entries

Please know that in the above code snippet, I have changed the API key for obvious reasons.

Thanks



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire