0

我收到错误消息:负载丢失或格式不正确

我正在使用真实设备以 swift 语言从 Apple DeviceCheck API 生成设备令牌,并将事务 ID 传递给这个 php api。

使用此代码成功生成 jwt 令牌,但进一步的代码不适用于苹果查询位 api。

这是我在 php 中的服务器端代码:

<?php
require_once "vendor/autoload.php";
use Zenstruck\JWT\Token;
use Zenstruck\JWT\Signer\OpenSSL\ECDSA\ES256;
use \Ramsey\Uuid\Uuid;


$deviceToken = (isset($_POST["deviceToken"]) ? $_POST["deviceToken"] : null);
$transId = (isset($_POST["transId"]) ? $_POST["transId"] : null);

function generateJWT($teamId, $keyId, $privateKeyFilePath) {
    $payload = [
    "iss" => $teamId,
    "iat" => time()
    ];
    $header = [
    "kid" => $keyId
    ];
    $token = new Token($payload, $header);
    return (string)$token->sign(new ES256(), $privateKeyFilePath);
}

$teamId = "#####";// I'm passing My team id
$keyId = "#####"; // I'm passing my key id 
$privateKeyFilePath = "AuthKey_4AU5LJV3.p8";
$jwt = generateJWT($teamId, $keyId, $privateKeyFilePath);


function postReq($url, $jwt, $bodyArray) {

    $header = [
        "Authorization: Bearer ". $jwt
    ];



    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $bodyArray);  //Post Fields
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);

//        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
//        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

    $server_output = curl_exec($ch);

    //$info = curl_getinfo($ch);
   // print_r($info);
    //echo 'http code: ' . $info['http_code'] . '<br />';
    //echo curl_error($ch);

    curl_close($ch);

    return  $server_output;


}


$body = [
"device_token" => $deviceToken,
"transaction_id" => $transId,
"timestamp" => ceil(microtime(true)*1000)
];

$myjsonis = postReq("https://api.development.devicecheck.apple.com/v1/query_two_bits", $jwt, $body);

echo $myjsonis;

 ?>

这段代码哪里有问题?或 php 代码中的任何其他解决方案。

有什么我想念的吗。

4

1 回答 1

0

我刚刚检查了文档。他们不想要像表单那样的 POST 字段,而是想要一个 JSON 正文。这是一段代码,您应该可以对其进行调整以执行您需要的操作:

$data = array("name" => "delboy1978uk", "age" => "41");                                                                    
$data_string = json_encode($data);                                                                                   

$ch = curl_init('http://api.local/rest/users');                                                                      
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/json',                                                                                
    'Content-Length: ' . strlen($data_string))                                                                       
);                                                                                                                   

$result = curl_exec($ch);

实际上,再次查看您的代码,它可能就像json_encode()在您的阵列上运行一样简单。

于 2019-07-18T11:49:59.467 回答