
シェア:
クリスはNexmoのデベロッパー・アドボケイトとして、デベロッパーがグローバル・コミュニケーション・プラットフォームを使えるようにサポートしている。カンファレンスに出席していないときは、世界中を歩き回っている。
Nexmo Verify API実装ガイド
所要時間:1 分
この実装ガイドでは、iOS または Android アプリで Verify API を使用するためのサーバーの設定方法を説明します。
AndroidやiOSデバイス用のアプリのようなクライアント側のデバイスに、開発者がAPIキーや秘密を保存することは望ましくありません。そのため、モバイルアプリでVerify API自体と直接統合する代わりに、あなたがコントロールできる独自のサーバー上でAPIとやりとりすることをお勧めします。
このチュートリアルでは、Nexmo Verify APIと対話するためのプロキシとして動作するNode.jsサーバーのセットアップ方法を学びます。このプロキシAPIサーバーをセットアップした後、私たちの iOSおよび Androidガイドを参照してください。
サーバーの設定
デモンストレーションとして、glitchに設置するサーバーの例を用意しました: https://glitch.com/~nexmo-verify.また ソースコードはGitHub.
アプリのソースコードはコメント付きで文書化されているが、以下のセクションで重要な部分を説明する。
Node.jsによるシンプルなExpressアプリ
このNode.jsアプリはシンプルな アプリです。アプリです。 body-parserを使ったシンプルなexpressアプリです。また、このアプリは nexmo-nodeNode.js用のNexmo REST APIクライアントです。
Vonage API Account
To complete this tutorial, you will need a Vonage API account. If you don’t have one already, you can sign up today and start building with free credit. Once you have an account, you can find your API Key and API Secret at the top of the Vonage API Dashboard.
APIキーとシークレットを取得したら、次のサイトに移動します。 server.jsに移動し、Nexmoクライアントを起動します:
const Nexmo = require('nexmo');
const nexmo = new Nexmo({
apiKey: API_KEY,
apiSecret: API_SECRET
});プロキシサーバーのロジックはすべて server.jsファイルにあります。エンドポイントごとに見ていこう。
検証リクエスト
Verifyプロセスを開始するために、モバイルアプリはプロキシサーバーに POSTをプロキシサーバーに送信します。 {"number": 14155550100}国コードを含めることを忘れないでください!プロキシサーバーはリクエストを次のように処理する:
app.post('/request', (req, res) => {
// A user registers with a mobile phone number
let phoneNumber = req.body.number;
console.log(phoneNumber);
nexmo.verify.request({number: phoneNumber, brand: 'Awesome Company'}, (err, result) => {
if(err) {
console.log(err);
//Oops! Something went wrong, respond with 500: Server Error
res.status(500).send(err);
} else {
console.log(result);
if(result && result.status == '0') {
//A status of 0 means success! Respond with 200: OK
res.status(200).send(result);
} else {
//A status other than 0 means that something is wrong with the request. Respond with 400: Bad Request
//The rest of the status values can be found here: https://developer.nexmo.com/api/verify#status-values
res.status(400).send(result);
}
}
});
});
ライブラリで検証プロセスを開始するのは簡単だ。 nexmo-nodeライブラリで検証プロセスを開始するのは簡単です。必要なのは、アプリが検証するユーザーの電話番号と、アプリが関連付けられているブランドだけです。ブランドは、電話番号を検証するユーザーに送信されるメッセージで使用されます。例えば、"Awesome Company "というブランドを使用すると、ユーザーが電話番号を検証する際に次のようなメッセージが送信されます:8571.5分間有効です。"
RESTfulなパラダイムに従いたいので、リクエストにエラーがあれば、レスポンスの本文にエラーと一緒に 500を返します。リクエストが成功した場合は、リクエストIDを含むJSONボディと 200とリクエストIDと ステータスを返します。
重要な注意これを記録する
request_idを記録してください。2FAコードの確認や認証リクエストのキャンセルが必要になります。
APIは 200を返送する。 statusこれはリクエストが成功したことを意味する。このリクエストに対するレスポンスは以下のようになる:
{
"request_id":"requestId",
"status":"status",
"error_text":"error"
}ステータスが0以外の場合は、リクエストに何か問題があったということだ。したがって、APIは 400と error_text文字列。
検証をチェック
ユーザーが検証リクエストをキックオフした後、コードを入力してステータスを確認したいと思うだろう。以下のエンドポイントによって、クライアントアプリはそれを行うことができる。
app.post('/check', (req, res) => {
//To verify the phone number the request ID and code are required.
let code = req.body.code;
let requestId = req.body.requestId;
console.log("Code: " + code + " Request ID: " + requestId);
nexmo.verify.check({request_id: requestId, code: code}, (err, result) => {
if(err) {
console.log(err);
//Oops! Something went wrong, respond with 500: Server Error
res.status(500).send(err);
} else {
console.log(result)
if(result && result.status == '0') {
//A status of 0 means success! Respond with 200: OK
res.status(200).send(result);
console.log('Account verified!')
} else {
//A status other than 0 means that something is wrong with the request. Respond with 400: Bad Request
//The rest of the status values can be found here: https://developer.nexmo.com/api/verify#status-values
res.status(400).send(result);
console.log('Error verifying account')
}
}
});
});
このエンドポイントは /requestエンドポイントに似ています。このエンドポイントでは POSTは /checkエンドポイントに codeと request_idパラメータを含む
{"code": "5309",
"request_id": "aaaaaaaa-bbbb-cccc-dddd-0123456789ab"}クライアント・モバイル・アプリが対応するリクエストIDとともに正しいコードを送信した場合、サーバーからの応答は 200とVerify APIからのJSONレスポンスで応答します。リクエストに何か問題があった場合、サーバーは 400と error_text文字列で応答する。成功した検証リクエストに対するレスポンスは、次のようになります:
{
"request_id": "aaaaaaaafffffffff0000000099999999",
"status": "0",
"event_id": "aaaaaaaafffffffff0000000099999999",
"price": "0.10000000",
"currency": "EUR"
} 検証のキャンセル
最後に実装するエンドポイントでは、検証リクエストをキャンセルできるようにします。これは、ユーザーが間違った電話番号を入力したり、アプリにログインしたくなくなったりした場合に必要になることがあります。
app.post('/cancel', (req, res) => {
//User sends the request id to cancel the verification request
let requestId = req.body.request_id;
console.log("Request ID: " + requestId);
nexmo.verify.control({request_id: requestId, cmd:'cancel'}, (err, result) => {
if(err) {
console.log(err);
//Oops! Something went wrong, respond with 500: Server Error
res.status(500).send(err);
} else {
if(result && result.status == '0') {
//A status of 0 means the verify request was succesfully cancelled! Respond with 200: OK
res.status(200).send(result);
} else {
//A status other than 0 means that something is wrong with the request. Respond with 400: Bad Request
//The rest of the status values can be found here: https://developer.nexmo.com/api/verify#status-values
res.status(400).send(result);
}
}
});
});
前回同様、サーバーは 200を送ります。クライアントが行ったリクエストにエラーがあった場合、サーバは応答として 400と error_text文字列で応答する。その他のエラーが発生した場合、サーバは 500とエラーメッセージを応答本文に書きます。エラーがない限り、サーバーはボディにこのJSONを含めて応答します:
{
"status":"0",
"command":"cancel"
} 生産に移す
このNode.jsは、glitch上の私たちのプロジェクトをリミックスすることで、概念実証として簡単にセットアップすることができます: https://glitch.com/edit/#!/remix/nexmo-verify.ファイルに、あなた自身のAPIキーとシークレットを入力するだけです。 .envファイルに入力してください。すぐにHerokuボタンを追加し、このアプリをサーバーレスとしてセットアップする方法を説明します。 Firebase関数.
次のステップ
サーバーのセットアップが完了したら、このサーバーとネットワーク接続するAndroidまたはiOSアプリを作成します。以下のチュートリアルを読んで、その方法を学んでください:
リスク/免責事項
お客様のサーバーをさらに保護するために、IPアドレスに基づいてサーバーへのリクエストを制限します。 エクスプレス・レートリミットは良いリソースです。
