PHP Guzzle with basic auth and bearer token

PHP Guzzle with basic auth and bearer token

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Combining Basic Auth and Bearer Tokens in Guzzle

Attempting to authenticate an API request that requires multiple authorization headers often leads to confusion, especially when standard library defaults don't immediately align with the specific requirements of the target service. The scenario presented involves connecting to infojobs-api, which explicitly demands a combination of HTTP Basic Authentication and an OAuth 2.0 Bearer token in a single Authorization header: Authorization: Basic <base64>,Bearer <token>.

The user's initial attempt using the Guzzle client failed with a 401 Unauthorized error, returning the message "Client credentials not valid." While the error code is generic, the root cause lies in how Guzzle handles the auth option versus how the API expects the headers to be constructed.

The Limitation of the auth Option

In Guzzle, the ['auth' => ['Basic', ...]] syntax provided by the library is designed for convenience. It automatically calculates the Base64 string and injects it into a single header named Authorization. However, this mechanism does not support merging multiple schemes into one header line or appending additional parameters like a Bearer token to the same header.

When Guzzle processes the request with the provided code, it likely sends:
Authorization: Basic <base64>

It ignores the second element of the array ['Bearer', $acceso->access_token] because the standard auth configuration expects exactly one scheme and its corresponding value. To satisfy an API that concatenates Basic ... , Bearer ..., you must bypass the helper methods and manually construct the header string.

Manual Header Construction

The solution is to stop relying on Guzzle's automatic header injection for this specific case and instead pass a custom request with a pre-computed header value. This approach offers precise control over the exact format expected by the server.

Here is how you can modify the code to match the API specification:

use GuzzleHttp\Client;

$client = new Client(['base_uri' => 'https://api.infojobs.net']);

// Prepare the Basic Auth string manually
$credentials = base64_encode(CLIENT_ID . ':' . CLIENT_SECRET);

// Construct the full Authorization header exactly as the API expects
$authorizationHeader = "Basic " . $credentials . ",Bearer " . $acceso->access_token;

$response = $client->request(
    'GET',
    'api/1/curriculum',
    [
        'headers' => [
            'Authorization' => $authorizationHeader,
        ],
    ]
);

$body = $response->getBody()->getContents();

By defining the $authorizationHeader variable first, you ensure that the Base64 encoded credentials and the Bearer token are concatenated with a comma and space separator, matching the example in the documentation. Passing this string within the headers array of the request options guarantees that Guzzle sends it exactly as intended without attempting to interpret it as a standard auth scheme.

Security Considerations for Manual Headers

While manual header construction solves the immediate connectivity issue, it introduces specific security considerations that developers must respect.

First, never log or echo the full $authorizationHeader string in development environments. Since this header contains both client secrets and access tokens, exposing it via debugging tools or error logs compromises the application's credentials. The original code snippet included 'debug' => true inside the request options; while helpful for troubleshooting, this setting can inadvertently expose sensitive data if not handled carefully.

Second, be mindful of token expiration. The Bearer token is stateless and time-bound. If the underlying OAuth flow expires, you must refresh the token and regenerate the header string before making subsequent requests. Because the code manually constructs the header, there is no automatic caching or renewal mechanism provided by Guzzle. You are responsible for checking the validity of $acceso->access_token before constructing the request array.

Finally, ensure that the Base64 encoding follows the API's expectations. The API documentation shows Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==. This is standard Base64 for username:password. If your CLIENT_ID and CLIENT_SECRET contain special characters, ensure they are concatenated strictly with a single colon (:) before encoding. Any deviation here will result in the "Client credentials not valid" error, as the server will fail to decode the payload back into the expected credential pair.

By taking manual control of the header construction, you align your implementation with the API's strict requirements while maintaining full visibility over the security implications of each request.

Our network