- API (データ&プログラム) -API(data&program)

生育予測情報:AuthenticationKey

提供ベンダー:
システム名:
WAGRI情報処理システム
利用条件:
提供ベンダーと有償契約が必要

概要

API名称
生育予測情報
説明

本APIはビジョンテック提供の生育予測情報を取得する為のAPIです。 本APIの利用には、ビジョンテックとのライセンス契約が必要となります。 ライセンス契約につきましては、ビジョンテック(tec@vti.co.jp)にお問い合わせください。

データの更新頻度
カテゴリー
添付ファイル

リクエスト

URL

https://api.wagri.net/API/Public/GrowthVti/AuthenticationKey?userid={userid}&password={password}

説明

本メソッドは、ビジョンテックが提供する公開APIを除く、APIを利用する前に認証キーを発行するメソッドです。 ビジョンテック提供のAPIを利用する際には、パラメータにユーザIDと認証キー(本メソッドで取得) を加えることで初めて利用可能となります。

認証キーの有効期間はリクエストしてから30分間とし、有効期間内に再リクエストされた場合は、 認証キーは上書きされ、その前のキーは無効となります。

HTTPメソッド
GET
パラメータ

URLデータ定義
認証キー発行

名称 サイズ 値の説明
userid ユーザーID string Required
password パスワード string Required
呼出例

レスポンス

レイアウト

レスポンスデータ定義
Result

名称 サイズ 値の説明
Result string
ResultStatus リザルトステータス string
ErrCode エラーコード string
ErrMsg エラーメッセージ string
ServerTime APIサーバの処理日時 string Format: yyyy-MM-dd HH:mm:ss.ffff
AuthenticationKey string
Key 認証キー文字列 string Length: 64
IssueTime 認証キー発行日時 string Format: yyyy-MM-dd HH:mm:ss.ffff
レスポンス例

ステータスコード
コード 名称 値の説明
400 Bad Request リクエストが不正です。
401 Unauthorized 認証情報が不正です。
403 Forbidden リソースのアクセスが禁止されています。
500 Internal Server Error サーバー内部でエラーが発生しました。

 

サンプルソースコード

ソースコード

PHP
1)アクセストークン認証用スクリプト
WAGRIのアクセストークンを取得する為のスクリプトです。

WAGRI_checkAuth.php

<?php
require_once 'FileController.php';

$client_id="****";
$client_secret="****";
$url = "https://api.wagri.net/Token";

$headers = array(
    'Content-Type: application/x-www-form-urlencoded',
);
$data = array(
    'grant_type' => "client_credentials",
    'client_id' => $client_id,
    'client_secret' => $client_secret
);
$content = http_build_query($data);

$options = array('http' => array(
    'method' => 'POST',
    'content' => $content,
    'header' => implode("\r\n", $headers),
    'ignore_errors' => true,
));
$out = file_get_contents($url, false, stream_context_create($options));
preg_match("/[0-9]{3}/", $http_response_header[0], $stcode);
if((int)$stcode[0] >= 200 && (int)$stcode[0] <= 299){
    $result = json_decode($out);
    print_r($result);
    $controller = new FileController();
    $controller->setToken($result);
} else {
    throw new Exception("リクエストが不正です。\n");
}

?>

4行 $client_id=””; 5行 $client_secret=””; の「****」にWAGRIのclient_id、client_secretの入力が必要です。

2)GrowthVtiを使うための認証用スクリプト
GrowthVtiを使用するための認証キーを取得する為のスクリプトです。

checkAuth_token.php

<?php
require_once 'FileController.php';

$controller = new FileController();
$token = $controller->getToken();
if($token === false){
    return false;
}

$url = "https://api.wagri.net/API/Public/GrowthVti/AuthenticationKey?";
$header = array("X-Authorization:" . $token);

$context = array(
    "http" => array(
        "method"  => "GET",
    "content-type" => "json",
        "header"  => implode("\r\n",$header),
        'ignore_errors' => true,
    )
);

$userid="****";
$pass="****";
$url = $url . "userid=" .$userid ."&password=" .$pass;
$out = file_get_contents($url, false, stream_context_create($context));
preg_match("/[0-9]{3}/", $http_response_header[0], $stcode);
if((int)$stcode[0] >= 200 && (int)$stcode[0] <= 299){
    $result = json_decode($out);
    print_r($result);
    $controller->setAuthKey($userid, $result);
} else {
    throw new Exception("リクエストが不正です。\n");
}
?>

22行 $userid=””; 23行 $pass=””; の「****」にビジョンテックが提供したユーザID、パスワードの入力が必要です。

3)取得したキーをファイル入出力するスクリプト
取得したキーをファイル入出力するスクリプトです。 ※当スクリプトではファイル出力を行っておりますが、トークンの盗難防止の為本来はデータベース等への保存を推奨しています。

FileController.php

<?php
class FileController{
    private $token_file = "files/token.txt";
    private $auth_file = "files/authkey.txt";
    private $get_token_file = "WAGRI_checkAuth.php";
    private $get_authkey_file = "checkAuth_token.php";

    public function deleteToken(){
        if($this->existsToken()){
            unlink($this->token_file);
        }
    }

    public function deleteAuthKey(){
        if($this->existsAuthKey()){
            unlink($this->auth_file);
        }
    }

    public function existsToken(){
        return file_exists($this->token_file);
    }

    public function execWAGRIGetToken(){
        $cmd = "php " . $this->get_token_file;
        system($cmd, $return_var);
        return $return_var;
    }

    public function setToken($tokenObject){
        if(empty($tokenObject) || empty($tokenObject->access_token)){
            echo "Tokenの取得に失敗しました。\n";
            return false;
        }
        return file_put_contents($this->token_file, $tokenObject->access_token);
    }

    public function getToken(){
        if(!$this->existsToken()){
            $result = $this->execWAGRIGetToken();
            if($result !== 0){
                echo "Tokenの取得に失敗しました。\n";
                return false;
            }
        }
        $token = file_get_contents($this->token_file);
        if($token === false || empty($token)){
            echo "Tokenの読み込みに失敗しました。\n";
            $this->deleteToken();
            return false;
        }
        return $token;
    }

    public function existsAuthKey(){
        return file_exists($this->auth_file);
    }

    public function execGetAuthKey(){
        $cmd = "php " . $this->get_authkey_file;
        system($cmd, $return_var);
        return $return_var;
    }

    public function setAuthKey($userid, $authObject){
        if(empty($authObject) || $authObject->Result->ResultStatus->ErrCode != 0){
            echo "AuthenticationKeyの取得に失敗しました。\n";
            return false;
        }
        $content = array(
            'userid' => $userid,
            'authkey' => $authObject->Result->AuthenticationKey->Key,
        );
        return file_put_contents($this->auth_file, json_encode($content));
    }

    public function getAuthKey(){
        if(!$this->existsAuthKey()){
            $result = $this->execGetAuthKey();
            if($result !== 0){
                echo "AuthenticationKeyの取得に失敗しました。\n";
                return false;
            }
        }
        $authkeyjson = file_get_contents($this->auth_file);
        if($authkeyjson === false || empty($authkeyjson)){
            echo "AuthenticationKeyの読み込みに失敗しました。\n";
            $this->deleteAuthKey();
            return false;
        }
        $authkey = json_decode($authkeyjson, true);
        if(empty($authkey['userid']) || empty($authkey['authkey'])){
            echo "AuthenticationKeyの読み込みに失敗しました。\n";
            $this->deleteAuthKey();
            return false;
        }
        return $authkey;
    }
}
?>

実行例:

$ php WAGRI_checkAuth.php    
$ php checkAuth_token.php
前に戻る
上部へスクロール
Scroll to Top