I am trying to send a PubSub message from a PHP EBS. Using the documentation at Reference | Twitch Developers and a php-jwt library, I came up with the following code to create the payload.
$jwt = new JWT(base64_decode(SOTD_SECRET), 'HS256', 3600, 10);
$token = $jwt->encode([
'exp' => $exp,
'user_id' => $userID,
'channel_id' =>$broadcasterID,
'role'=>'external',
'is_linked'=>'true',
'pubsub_perms'=>['send'=>['broadcast']]
]);
The secret is from Extension Secrets under Extension Client Configuration.
I have used several online jwt “testers” and and I am confident that the token is created and signed correctly.
I’m using the following code to send the post.
$message="refreshLeaders";
$body=json_encode(["broadcaster_id" => $broadcasterID,'message' => $message,'target' => ['broadcast'],]) ;
$curl = curl_init($url);
mylog("Body: $body");
$url='https://api.twitch.tv/helix/extensions/pubsub/';
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$headers = array( 'Authorization' => 'Bearer ' . $token, 'Client-ID' => SOTD_CLIENT_ID, 'Content-Type' => 'application/json');
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POSTFIELDS, $body);
$resp = curl_exec($curl);
curl_close($curl);
mylog("Response from Send Message: ".$resp);
The Client ID is the extension client ID
The response I get is {“error”:“Unauthorized”,“status”:401,“message”:“OAuth token is missing”}. This seems strange since the documentation clearly states " Authorization - Signed JWT created by an Extension Backend Service (EBS), following the requirements documented in [Signing the JWT]".
I’ve tried different combinations of client id and secrets. I’ve tried opaque_user_id instead of user_id. I’ve tried different combinations for target.
I’ve read as much documentation as I could find, as well as any forum posts on the topic. I cannot figure out what I’m doing wrong.