【问题标题】:Pritunl Rest Api using Php使用 Php 的 Pritunl Rest Api
【发布时间】:2018-07-15 21:32:16
【问题描述】:

有没有人设法使用 php 访问 pritunl 的 api? 源代码https://github.com/pritunl/pritunl/blob/master/tools/test_pritunl.py 提供的指南只有 python 示例。 如果我尝试读取数据,它工作正常。但是当我尝试更新或创建一个新实体时,我收到的 HTTP 请求失败了! HTTP/1.1 401 未经授权

我的代码是:

public static function auth_request($method, $path, $data="")
        $BASE_URL   = env('PRITUNL_BASE_URL');
        $API_TOKEN  = env('PRITUNL_API_TOKEN');
        $API_SECRET = env('PRITUNL_API_SECRET');
        $auth_timestamp = strval(time());
        $auth_nonce = uniqid();
        $auth_array = array(
            $API_TOKEN,
            $auth_timestamp,
            $auth_nonce,
            strtoupper($method),
            $path,
        );
        if ($data) {
            array_push($auth_array, $data);
        }
        $auth_string = join("&", $auth_array);
        $auth_signature = base64_encode(hash_hmac(
            "sha256", $auth_string, $API_SECRET, true));
        $options = array(
            "http" => array(
                "header"  => array(
                    'Content-Type: application/json',
                    'Auth-Token: '.$API_TOKEN,
                    'Auth-Timestamp: '.$auth_timestamp,
                    'Auth-Nonce: '.$auth_nonce,
                    'Auth-Signature: '.$auth_signature
                ),
                "method"  => $method,
                "content" => $data,
            ),
            "ssl" => array(
                "allow_self_signed" => true,
                "verify_peer_name" => false,
            ),
        );
        $context = stream_context_create($options);
        return file_get_contents($BASE_URL.$path, false, $context);

【问题讨论】:

    标签: php api httprequest vpn


    【解决方案1】:

    解决了从 $auth_array 中删除 $data 的问题。 这是我的新代码:

    public static function auth_request($method, $path, $data="") {
        $BASE_URL   = env('PRITUNL_BASE_URL');
        $API_TOKEN  = env('PRITUNL_API_TOKEN');
        $API_SECRET = env('PRITUNL_API_SECRET');
        $auth_timestamp = strval(time());
        $auth_nonce = uniqid();
        $auth_array = array(
            $API_TOKEN,
            $auth_timestamp,
            $auth_nonce,
            strtoupper($method),
            $path,
        );
    
        $auth_string = join("&", $auth_array);
        $auth_signature = base64_encode(hash_hmac(
            "sha256", $auth_string, $API_SECRET, true));
        $options = array(
            "http" => array(
                "header"  => array(
                    'Content-Type: application/json',
                    'Auth-Token: '.$API_TOKEN,
                    'Auth-Timestamp: '.$auth_timestamp,
                    'Auth-Nonce: '.$auth_nonce,
                    'Auth-Signature: '.$auth_signature
                ),
                "method"  => $method,
                "content" => $data,
            ),
            "ssl" => array(
                "allow_self_signed" => true,
                "verify_peer_name" => false,
            ),
        );
        $context = stream_context_create($options);
        return file_get_contents($BASE_URL.$path, false, $context);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-24
      • 2013-06-18
      • 2011-04-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多