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
916 stars 180 forks source link

Getting Invalid credentials error with credentials that should be correct #376

Closed dwightwatson closed 1 year ago

dwightwatson commented 1 year ago

I'm attempting to use the number insights API. When I use it through the PHP client I get an exception Error: Invalid credentials. However when using these exact same credentials in a regular HTTP request it works fine.

Here is it using the PHP client - I can't see where I have made a mistake in this code:

$credentials = new Vonage\Client\Credentials\Basic(config('vonage.api_key'), config('vonage.api_secret'));

$vonage = new Vonage\Client($credentials);

$result = $vonage->insights()->standard($phoneNumber);

// Throws - Vonage\Client\Exception\Request  Error: Invalid credentials.

And here it is using Laravel's HTTP client - a very basic request with the same credentials:

$result = Http::get('https://api.nexmo.com/ni/standard/json', [
    'api_key' => config('vonage.api_key'),
    'api_secret' => config('vonage.api_secret'),
    'number' => $phoneNumber,
])->json();

This seems somewhat related to #373 but I can't pinpoint how (especially as that seemed to resolve itself). Happy to provide all user account and testing details in private if you have trouble replicating on your end.

(I have actually been able to replicate this with two entirely separate sets of Vonage credentials).

Expected Behavior

I would expect the insights API to return a response object. This used to work on v3 and only appears to have cropped up since upgrading to v4.

If there is another error I'd expect that to balloon up instead of an authentication error.

Current Behavior

And exception Vonage\Client\Exception\Request Error: Invalid credentials. is thrown as a result of calling the insights API.

SecondeJK commented 1 year ago

What I'm doing to do here is paste in my API key and secret directly into this code and make the call - if it's successful I can attribute this to config() helpers in Laravel and is definitely not an issue with the SDK. If it does still complain, I should be able to find the root cause.

dwightwatson commented 1 year ago

Just tested using the values directly (without the config() helper) and had the same result. Are you able to replicate it on your end?

SecondeJK commented 1 year ago

I've managed to replicate it, I think it's a bug where it needs to use a new auth handler I wrote recently. Fix should be incoming shortly.

SecondeJK commented 1 year ago

Actually this might be a slightly bigger bug regarding auth than I thought due to how IterableApiCollection works. Will update after finding out more.

SecondeJK commented 1 year ago

PR open #379

SecondeJK commented 1 year ago

Merged, thanks for reporting!

dwightwatson commented 1 year ago

Thanks for taking the time to get to the bottom of this!

dwightwatson commented 1 year ago

@SecondeJK could you please double-check that the sample code works for you after this fix?

I've upgraded to vonage/client-core 4.0.8 and I'm experiencing the same issue.

SecondeJK commented 1 year ago

Huh. Guess I need to write a more stringent test for it

SecondeJK commented 1 year ago

Argh. An easy one line fix, what a pain. I'll get a PR open for it.

stronglab commented 1 year ago

@SecondeJK Same trouble in Vonage\Numbers\ClientFactory

 public function __invoke(ContainerInterface $container): Client
    {
        /** @var APIResource $api */
        $api = $container->make(APIResource::class);
        $api
            ->setBaseUrl($api->getClient()->getRestUrl())
            ->setIsHAL(false)
            ->setAuthHandler(new BasicHandler());

        return new Client($api);
    }

need setAuthHandler(new BasicQueryHandler());

docs: https://developer.vonage.com/en/api/numbers

SecondeJK commented 1 year ago

I'm glad I wrote that new AuthHandler! This will go out with another PR that handles some SMS behaviour next week.

SecondeJK commented 1 year ago

This should now be fixed with 4.0.10 - please reopen if you still have issues