【发布时间】:2022-01-05 01:55:32
【问题描述】:
我正在尝试运行 MUX 网站上记录的 PHP 示例代码 here。我刚刚生成了一个全新的 API 密钥 + 秘密并将它们存储在 mux-credentials.php 文件中:
define('MUX_TOKEN_ID', 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx');
define('MUX_TOKEN_SECRET', '<base64-encoded-string-here>');
我使用 composer 按照here 的指示安装 Firebase\JWT,然后运行 MUX 文档中指定的确切代码:
// load libs
require_once 'vendor/autoload.php';
// load MUX credentials
require_once 'mux-credentials.php';
use \Firebase\JWT\JWT;
$myId = "<MY-ASSET-ID-HERE>"; // Enter the id for which you would like to get counts here
$myIdType = "asset_id"; // Enter the type of ID provided in my_id; one of video_id | asset_id | playback_id | live_stream_id
$keyId = MUX_TOKEN_ID; // Enter your signing key id here
$keySecret = MUX_TOKEN_SECRET; // Enter your base64 encoded private key here
$payload = array(
"sub" => $myId,
"aud" => $myIdType,
"exp" => time() + 600, // Expiry time in epoch - in this case now + 10 mins
"kid" => $keyId
);
$jwt = JWT::encode($payload, base64_decode($keySecret), 'RS256');
print "$jwt\n";
此代码出现致命错误:
PHP Warning: openssl_sign(): supplied key param cannot be coerced into a private key in /home/example/vendor/firebase/php-jwt/src/JWT.php on line 225
PHP Fatal error: Uncaught DomainException: OpenSSL unable to sign data in /home/example/vendor/firebase/php-jwt/src/JWT.php:227
Stack trace:
#0 /home/example/vendor/firebase/php-jwt/src/JWT.php(195): Firebase\JWT\JWT::sign()
#1 /home/example/people-watching.php(30): Firebase\JWT\JWT::encode()
#2 {main}
thrown in /home/example/vendor/firebase/php-jwt/src/JWT.php on line 227
如果我像这样删除 JWT::encode 调用中的最后一个参数:
$jwt = JWT::encode($payload, base64_decode($keySecret));
然后代码成功运行,并生成一个长的base64编码字符串。但是,当我尝试使用它来联系 API 时,该 JWT 字符串会导致错误:
curl 'https://stats.mux.com/counts?token=<JWT-HERE>'
MUX api 响应:
{"error":{"type":"internal error","messages":["Could not get signing key."]}}
谁能帮我修复此代码,以便我可以联系 MUX API 并检索有关我的资产 ID 的请求信息?
编辑:我很感谢下面的答案指出应该使用签名密钥而不是 API 凭据,但是,抛开我对为什么我们需要像签名密钥这样的东西的困惑,curl 请求创建一个签名密钥也不起作用。这个 curl 请求(我已经修改了我的 MUX_TOKEN_ID 和 MUX_TOKEN_SECRET):
curl -X POST \
-H "Content-Type: application/json" \
-u <MUX_TOKEN_KEY>:<MUX_TOKEN_SECRET> \
'https://api.mux.com/system/v1/signing-keys?product=data'
失败,返回此错误:
{"error":{"type":"not_found","messages":["The requested resource either doesn't exist or you don't have access to it."]}}
【问题讨论】:
标签: php firebase api openssl jwt