【发布时间】:2019-06-05 21:19:50
【问题描述】:
我正在尝试使用 JWT Bearer Grant Type 连接到 Docebo API,但遇到了问题:
使用下面的代码,我收到以下响应(即使在 https://jwt.io 验证时我的 $token 似乎是正确的):
stdClass Object ( [name] => Unauthorized [message] => Array ( [0] => Your request was made with invalid credentials. ) [code] => 0 [status] => 401 )
这是用于生成 $token 的代码,以及那个错误 ^
<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
function base64url_encode($data) {
return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
}
$key = 'example_public_key';
$headers = ['alg'=>'RS256','typ'=>'JWT'];
$headers_encoded = base64url_encode(json_encode($headers));
$today = time();
$tomorrow = time() + (1 * 24 * 60 * 60);
$payload = [
'iss' => 'example_client_id',
'sub' => 'example_user',
'aud' => 'example.docebosaas.com',
'iat' => $today,
'exp' => $tomorrow
];
$payload_encoded = base64url_encode(json_encode($payload));
$signature = hash_hmac('SHA256',"$headers_encoded.$payload_encoded",$key,true);
$signature_encoded = base64url_encode($signature);
$token = "$headers_encoded.$payload_encoded.$signature_encoded";
$curl = curl_init();
$curl_data = array(
'token' => $token
);
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_POSTFIELDS => $curl_data,
CURLOPT_URL => 'https://example.docebosaas.com/manage/v1/user'
));
$resp = curl_exec($curl);
curl_close($curl);
$json_obj = json_decode($resp);
print_r($json_obj);
更新:
根据下面的评论,我现在添加了适当的标头,并且不再收到 401。但是,将 CURL 调用更改为以下有一个新错误:
stdClass Object ( [error] => invalid_grant [error_description] => JWT failed signature verification )
这是更新后的 CURL 调用的代码:
$curl = curl_init();
$curl_data = array(
'grant_type' => 'urn:ietf:params:oauth:grant-type:jwt-bearer',
'scope' => 'api',
'assertion' => $token
);
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_POSTFIELDS => $curl_data,
CURLOPT_URL => 'https://example.docebosaas.com/oauth2/token'
));
$resp = curl_exec($curl);
curl_close($curl);
$json_obj = json_decode($resp);
print_r($json_obj);
【问题讨论】: