Vonage / vonage-php-sdk-core

Vonage REST API client for PHP. API support for SMS, Voice, Text-to-Speech, Numbers, Verify (2FA) and more.
https://developer.vonage.com/
Apache License 2.0
908 stars 181 forks source link

Unauthorized Exception while Sending a WhatsApp Message #365

Closed MajdTaweel closed 1 year ago

MajdTaweel commented 1 year ago

Expected Behavior

Sending a WhatsApp message through the messaging API should work.

Current Behavior

The following exception is raised: Vonage\Client\Exception\Request Unauthorised: Request header 'Authorization' missing.

Possible Solution

Comparing version 4.0.0 and 3.1.6, the following code were removed from the file src/Client.php starting from line 456. I could not see where this logic were moved or if it was replaced with something that provides the credentials for a key pair of application id and private key. By restoring these changes in my local vendor package, the client worked as before.

        if ($this->credentials instanceof Container) {
            if ($this->needsKeypairAuthentication($request)) {
                $handler = new KeypairHandler();
                $request = $handler($request, $this->getCredentials());
            } else {
                $request = self::authRequest($request, $this->credentials->get(Basic::class));
            }
        } elseif ($this->credentials instanceof Keypair) {
            $handler = new KeypairHandler();
            $request = $handler($request, $this->getCredentials());
        } elseif ($this->credentials instanceof SignatureSecret) {
            $request = self::signRequest($request, $this->credentials);
        } elseif ($this->credentials instanceof Basic) {
            $request = self::authRequest($request, $this->credentials);
        }

Steps to Reproduce (for bugs)

$keypair = new \Vonage\Client\Credentials\Keypair(
    PRIVATE_KEY,
    APPLICATION_ID,
);
$client = new \Vonage\Client($keypair);

$whatsAppText = new \Vonage\Messages\Channel\WhatsApp\WhatsAppText(
    FROM_NUMBER,
    TO_NUMBER,
    'this is a WA text from vonage'
);
$client->messages()->send($whatsAppText);

Your Environment

SecondeJK commented 1 year ago

This is likely to be a bug with the new AuthHandler system which now takes care of adding auth headers - this is why that chunk of code was removed. APIResource now handles client calls' auth. I'll see if I can reproduce the original problem, as that code that was deleted will almost certainly not be reinstated

MajdTaweel commented 1 year ago

This is likely to be a bug with the new AuthHandler system which now takes care of adding auth headers - this is why that chunk of code was removed. APIResource now handles client calls' auth. I'll see if I can reproduce the original problem, as that code that was deleted will almost certainly not be reinstated

Thanks for the clarification. For now I've applied a composer patch that adds the removed chunk of code back, since that works for me.

SecondeJK commented 1 year ago

Fixed with the release of 4.0.5, thank you!