我试图在MUX网站上运行这里文档中的PHP示例代码。我刚刚生成了一个全新的API密钥+秘密,并将它们存储在一个mux-credentials.php
文件中:
define('MUX_TOKEN_ID', 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx');
define('MUX_TOKEN_SECRET', '<base64-encoded-string-here>');
我按照这里的指示使用composer安装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调用中的最后一个param,如下所示:
$jwt = JWT::encode($payload, base64_decode($keySecret));
然后代码成功运行,并生成一个长的base64 64编码字符串。然而,当我试图使用JWT字符串与API联系时,它会导致错误:
curl 'https://stats.mux.com/counts?token=<JWT-HERE>'
MUX api的响应如下:
{"error":{"type":"internal error","messages":["Could not get signing key."]}}
有人能帮我修复这段代码吗?这样我就可以联系MUX并检索所请求的有关我的资产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."]}}
发布于 2022-01-04 01:38:08
看起来,您正在尝试使用API密钥和秘密来对JWT进行签名。相反,Mux正在寻找单独的签名密钥。
此签名密钥可以使用当前在https://api.mux.com/system/v1/signing-keys?product=data
请求中使用的API密钥和机密生成。
您可以在这里看到此请求的一个示例:https://docs.mux.com/guides/data/see-how-many-people-are-watching#1-create-a-signing-key
然后,JWT声明的kid
值将被设置为在创建签名密钥时返回的密钥ID。
我是一个社区工程师与Mux -如果你有任何想法,你觉得这个过程可能会更清楚,不要犹豫提供任何反馈!
https://stackoverflow.com/questions/70573010
复制相似问题