Verificar um código de verificação

Verifique o código de verificação fornecido pelo usuário. Você deve sempre verificar o código de verificação após enviá-lo. Isso permite que a Vonage determine o número de solicitações bem-sucedidas e proteger contra o uso fraudulento da plataforma.

Quando necessário, substitua as seguintes variáveis no código de exemplo pelos seus próprios valores:

ChaveDescrição
REQUEST_ID

The ID of the Verify request (this is returned in the API response when you send a verification code)

JWT

Used to authenticate your request. See Authentication for more information, including how to generate a JWT.

VONAGE_APPLICATION_PRIVATE_KEY_PATH

Private key path.

VONAGE_APPLICATION_ID

The Vonage Application ID.

CODE

The code the user supplies as having been sent to them

Escreva o código

Adicione o seguinte ao arquivo ` check-verification-code.sh`:

curl -X POST "https://api.nexmo.com/v2/verify/$VERIFY_REQUEST_ID" \
  -H "Authorization: Bearer $JWT"\
  -H 'Content-Type: application/json' \
  -d $'{
    "code": "'$VERIFY_CODE'"
  }'

Ver código-fonte completo

Execute seu código

Salve este arquivo no seu computador e execute-o:

sh check-verification-code.sh

Pré-requisitos

npm install @vonage/server-sdk

Crie um arquivo chamado ` check-verification-code.js ` e insira o seguinte código:

const { Vonage } = require('@vonage/server-sdk');

const vonage = new Vonage ({
  applicationId: VONAGE_APPLICATION_ID,
  privateKey: VONAGE_APPLICATION_PRIVATE_KEY_PATH,
});

Ver código-fonte completo

Escreva o código

Adicione o seguinte ao arquivo ` check-verification-code.js`:

vonage.verify2.checkCode(VERIFY_REQUEST_ID, VERIFY_CODE)
  .then((status) => console.log(`The status is ${status}`),
  )
  .catch((err) => console.error(err));

Ver código-fonte completo

Execute seu código

Salve este arquivo no seu computador e execute-o:

node check-verification-code.js

Pré-requisitos

Adicione o seguinte ao arquivo ` build.gradle`:

implementation 'com.vonage:server-sdk-kotlin:2.1.1'

Crie um arquivo chamado ` CheckVerificationCode ` e adicione o código a seguir ao método ` main `:

val client = Vonage {
    applicationId(VONAGE_APPLICATION_ID)
    privateKeyPath(VONAGE_PRIVATE_KEY_PATH)
}

Ver código-fonte completo

Escreva o código

Adicione o seguinte ao método ` main ` do arquivo ` CheckVerificationCode `:

if (client.verify.request(VERIFY_REQUEST_ID).isValidVerificationCode(VERIFY_CODE)) {
    println("Code matches.")
}

Ver código-fonte completo

Execute seu código

Podemos usar o plugin “ aplicativo ” para o Gradle a fim de simplificar a execução do nosso aplicativo. Atualize seu arquivo ` build.gradle ` com o seguinte:

apply plugin: 'application'
mainClassName = project.hasProperty('main') ? project.getProperty('main') : ''

Execute o seguinte comando ` gradle ` para rodar seu aplicativo, substituindo ` com.vonage.quickstart.kt.verify ` pelo pacote que contém ` CheckVerificationCode`:

gradle run -Pmain=com.vonage.quickstart.kt.verify.CheckVerificationCode

Pré-requisitos

Adicione o seguinte ao arquivo ` build.gradle`:

implementation 'com.vonage:server-sdk:9.3.1'

Crie um arquivo chamado ` CheckVerificationCode ` e adicione o código a seguir ao método ` main `:

VonageClient client = VonageClient.builder()
		.applicationId(VONAGE_APPLICATION_ID)
		.privateKeyPath(VONAGE_PRIVATE_KEY_PATH)
		.build();

Ver código-fonte completo

Escreva o código

Adicione o seguinte ao método ` main ` do arquivo ` CheckVerificationCode `:

try {
	client.getVerify2Client().checkVerificationCode(VERIFY_REQUEST_UUID, VERIFY_CODE);
	System.out.println("SUCCESS - code matches!");
}
catch (VerifyResponseException ex) {
	switch (ex.getStatusCode()) {
		case 400: // Code does not match
		case 404: // Already verified or not found
		case 409: // Workflow does not support code
		case 410: // Incorrect code provided too many times
		case 429: // Rate limit exceeded
		default:  // Unknown or internal server error (500)
			ex.printStackTrace();
	}
}

Ver código-fonte completo

Execute seu código

Podemos usar o plugin “ aplicativo ” para o Gradle a fim de simplificar a execução do nosso aplicativo. Atualize seu arquivo ` build.gradle ` com o seguinte:

apply plugin: 'application'
mainClassName = project.hasProperty('main') ? project.getProperty('main') : ''

Execute o seguinte comando ` gradle ` para rodar seu aplicativo, substituindo ` com.vonage.quickstart.verify2 ` pelo pacote que contém ` CheckVerificationCode`:

gradle run -Pmain=com.vonage.quickstart.verify2.CheckVerificationCode

Pré-requisitos

Install-Package Vonage

Crie um arquivo chamado ` VerifyCodeRequest.cs ` e insira o seguinte código:

using Vonage;
using Vonage.Request;

Ver código-fonte completo

Adicione o seguinte ao arquivo ` VerifyCodeRequest.cs`:

var credentials = Credentials.FromAppIdAndPrivateKeyPath(VONAGE_APPLICATION_ID, VONAGE_APPLICATION_PRIVATE_KEY_PATH);
var client = new VonageClient(credentials);

Ver código-fonte completo

Escreva o código

Adicione o seguinte ao arquivo ` VerifyCodeRequest.cs`:

var request = Vonage.VerifyV2.VerifyCode.VerifyCodeRequest.Build()
    .WithRequestId(REQUEST_ID)
    .WithCode(CODE)
    .Create();
var response = await client.VerifyV2Client.VerifyCodeAsync(request);

Ver código-fonte completo

Pré-requisitos

composer require vonage/client

Crie um arquivo chamado ` send_code.php ` e insira o seguinte código:

$client = new Vonage\Client(
    new Vonage\Client\Credentials\Keypair(VONAGE_APPLICATION_PRIVATE_KEY_PATH, VONAGE_APPLICATION_ID),
);

Ver código-fonte completo

Escreva o código

Adicione o seguinte ao arquivo ` send_code.php`:

try {
    $client->verify2()->check(REQUEST_ID, CODE);
} catch (\Exception $e) {
    var_dump($e->getMessage());
}

Ver código-fonte completo

Execute seu código

Salve este arquivo no seu computador e execute-o:

php send_code.php

Pré-requisitos

pip install vonage python-dotenv

Escreva o código

Adicione o seguinte ao arquivo ` check-verification-code.py`:

from vonage import Auth, Vonage
from vonage_verify import CheckCodeResponse

client = Vonage(
    Auth(
        application_id=VONAGE_APPLICATION_ID,
        private_key=VONAGE_PRIVATE_KEY,
    )
)

response: CheckCodeResponse = client.verify.check_code(
    request_id=VERIFY_REQUEST_ID, code=VERIFY_CODE
)
print(response)

Ver código-fonte completo

Execute seu código

Salve este arquivo no seu computador e execute-o:

python verify/check-verification-code.py

Pré-requisitos

gem install vonage

Crie um arquivo chamado ` check.rb ` e insira o seguinte código:

client = Vonage::Client.new(
  application_id: VONAGE_APPLICATION_ID,
  private_key: VONAGE_PRIVATE_KEY
)

Ver código-fonte completo

Escreva o código

Adicione o seguinte ao arquivo ` check.rb`:

client.verify2.check_code(
  request_id: VERIFY_REQUEST_ID,
  code: VERIFY_CODE
)

Ver código-fonte completo

Execute seu código

Salve este arquivo no seu computador e execute-o:

ruby check.rb