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
917 stars 181 forks source link

Missing api_key error on Verify API (2FA) #391

Closed romeoonisim closed 1 year ago

romeoonisim commented 1 year ago

On a blank PHP project, using the example code provided, I cannot make the Verify API work on the latest version, on 4.0, I get "Missing api_key" as an error.

Expected Behavior

I would expect the example code to work as expected out of the box

Current Behavior

<br />
<b>Fatal error</b>:  Uncaught Vonage\Client\Exception\Request: Missing api_key in /Users/romeoonisim/Sites/test/vonagetest/vendor/vonage/client-core/src/Verify/ExceptionErrorHandler.php:56
Stack trace:
#0 /Users/romeoonisim/Sites/test/vonagetest/vendor/vonage/client-core/src/Client/APIResource.php(297): Vonage\Verify\ExceptionErrorHandler-&gt;__invoke(Object(GuzzleHttp\Psr7\Response), Object(Laminas\Diactoros\Request))
#1 /Users/romeoonisim/Sites/test/vonagetest/vendor/vonage/client-core/src/Client/APIResource.php(128): Vonage\Client\APIResource-&gt;getException(Object(GuzzleHttp\Psr7\Response), Object(Laminas\Diactoros\Request))
#2 /Users/romeoonisim/Sites/test/vonagetest/vendor/vonage/client-core/src/Verify/Client.php(89): Vonage\Client\APIResource-&gt;create(Array, '/json')
#3 /Users/romeoonisim/Sites/test/vonagetest/index.php(8): Vonage\Verify\Client-&gt;start(Object(Vonage\Verify\Verification))
#4 /Users/romeoonisim/.composer/vendor/laravel/valet/server.php(234): require('/Users/romeooni...')
#5 {main}
  thrown in <b>/Users/romeoonisim/Sites/test/vonagetest/vendor/vonage/client-core/src/Verify/ExceptionErrorHandler.php</b> on line <b>56</b><br />

Possible Solution

It seems that the Verify/Client.php is not attaching the api_key and api_secret as body parameters, instead, it creates an Authorization header with the two values encoded in base64. Here are some of the documentation links: https://developer.vonage.com/en/api/verify#verifyCheck https://developer.vonage.com/en/getting-started/concepts/authentication

Steps to Reproduce (for bugs)

  1. Create a new php project
  2. Type in console composer require vonage/client
  3. Create a index.php with the following code:
    
    <?php
    require_once 'vendor/autoload.php';

$client = new Vonage\Client(new Vonage\Client\Credentials\Basic(API_KEY, API_SECRET));

$request = new \Vonage\Verify\Request(PHONE_NUMBER, 'Testing'); $response = $client->verify()->start($request);

echo 'Started verification with an id of: '.$response->getRequestId();


4. Replace API_KEY, API_SECRET, PHONE_NUMBER
5. Execute code

## Context
<!--- How has this issue affected you? What are you trying to accomplish? -->
<!--- Providing context helps us come up with a solution that is most useful in the real world -->
This issue prevents me from upgrading to Laravel 9 on the project I'm working on

## Your Environment
<!--- Include as many relevant details about the environment you experienced the bug in -->
* Version used: 4.0
* Environment name and version PHP 8.0.2 on nginx 1.23.3):
* Operating System and version:
System Version: macOS 12.1 (21C52) Apple M1
Kernel Version: Darwin 21.2.0
SecondeJK commented 1 year ago

This will be a fairly quick win, expect it to be rolled out with the release of Verify v2

ngoctiennet963 commented 1 year ago

Missing api_key code 2

SecondeJK commented 1 year ago

Yep, incoming PR shortly. It turns out the TokenBodyHandler for authentication this way never worked.

ngoctiennet963 commented 1 year ago

Yep, incoming PR shortly. It turns out the TokenBodyHandler for authentication this way never worked.

Thanks, I'll follow up

romeoonisim commented 1 year ago

@ngoctiennet963 if you are blocked, maybe my response from https://github.com/Vonage/vonage-laravel/issues/16 might help you until the PR will be available.

SecondeJK commented 1 year ago

Released under 4.1.2, thanks for reporting ✅

kvenn commented 10 months ago

I can't tell if I'm doing something wrong, but I can't find the version 4.1.2 in composer. Would appreciate any help, thank you!!

"vonage/client": "4.1.2"

Problem 1

[EDIT] I replaced vonage/client with the following and now it works.

    "guzzlehttp/guzzle": "^7.8.1",
    "vonage/client-core": "^4.6.1"

[EDIT 2] I continued getting api_key errors for sms->send. I switched to signing with signature (instead of basic) and that worked. I couldn't get basic to work.