Skip to main content

JWT token generation fails, MUX API responds with error

I'm trying to run the PHP example code documented here on the MUX website. I have just generated a brand new API key + secret and have stored them in a mux-credentials.php file:

define('MUX_TOKEN_ID', 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx');
define('MUX_TOKEN_SECRET', '<base64-encoded-string-here>');

I used composer to install Firebase\JWT as instructed here and then ran the exact code specified in the MUX documentation:

// 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";

This code barfs with a fatal error:

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

If I remove the last param in the JWT::encode call like so:

$jwt = JWT::encode($payload, base64_decode($keySecret));

Then the code successfully runs, and generates a long base64-encoded string. That JWT string, however, results in an error when I attempt to use it to contact the API:

curl 'https://stats.mux.com/counts?token=<JWT-HERE>'

The MUX api responds with:

{"error":{"type":"internal error","messages":["Could not get signing key."]}}

Can anyone help me fix this code so that I can contact the MUX API and retrieve the requested information about my asset id?

EDIT: I am grateful for the answers below pointing out that one should use a signing key rather than one's API credentials but, putting aside my confusion about why we need such a thing as a signing key, the curl request to create a signing key doesn't work either. This curl request (i've redacted my MUX_TOKEN_ID and 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'

fails, returning this error:

{"error":{"type":"not_found","messages":["The requested resource either doesn't exist or you don't have access to it."]}}


source https://stackoverflow.com/questions/70573010/jwt-token-generation-fails-mux-api-responds-with-error

Comments

Popular posts from this blog

Prop `className` did not match in next js app

I have written a sample code ( Github Link here ). this is a simple next js app, but giving me error when I refresh the page. This seems to be the common problem and I tried the fix provided in the internet but does not seem to fix my issue. The error is Warning: Prop className did not match. Server: "MuiBox-root MuiBox-root-1" Client: "MuiBox-root MuiBox-root-2". Did changes for _document.js, modified _app.js as mentioned in official website and solutions in stackoverflow. but nothing seems to work. Could someone take a look and help me whats wrong with the code? Via Active questions tagged javascript - Stack Overflow https://ift.tt/2FdjaAW

How to show number of registered users in Laravel based on usertype?

i'm trying to display data from the database in the admin dashboard i used this: <?php use Illuminate\Support\Facades\DB; $users = DB::table('users')->count(); echo $users; ?> and i have successfully get the correct data from the database but what if i want to display a specific data for example in this user table there is "usertype" that specify if the user is normal user or admin i want to user the same code above but to display a specific usertype i tried this: <?php use Illuminate\Support\Facades\DB; $users = DB::table('users')->count()->WHERE usertype =admin; echo $users; ?> but it didn't work, what am i doing wrong? source https://stackoverflow.com/questions/68199726/how-to-show-number-of-registered-users-in-laravel-based-on-usertype

Why is my reports service not connecting?

I am trying to pull some data from a Postgres database using Node.js and node-postures but I can't figure out why my service isn't connecting. my routes/index.js file: const express = require('express'); const router = express.Router(); const ordersCountController = require('../controllers/ordersCountController'); const ordersController = require('../controllers/ordersController'); const weeklyReportsController = require('../controllers/weeklyReportsController'); router.get('/orders_count', ordersCountController); router.get('/orders', ordersController); router.get('/weekly_reports', weeklyReportsController); module.exports = router; My controllers/weeklyReportsController.js file: const weeklyReportsService = require('../services/weeklyReportsService'); const weeklyReportsController = async (req, res) => { try { const data = await weeklyReportsService; res.json({data}) console