Jwt token is not working, giving error "Invalid token"

Hi,

I am trying to integrate Zoom meetings with my php application, for integration using Zoom API. when I am sending a request to Zoom server to create a meeting getting {“code”:124,“message”:“Invalid access token.”} error. I have also verified token in “https://jwt.io”. but not working in my application.
I am able to successfully create a meeting with same API key and API Secret in my another wordpress application but not working in my custom application.

I am using free basic trial account in zoom.

Please help me.

Thanks.

Hey @tiwari.jyoti.05,

How are you setting the authorization header? If it is working on another application, but not on yours, there must be something off with how you are sending the request or generating the JWT Token.

Thanks,
Tommy

Hi Tommy,

Thanks for reply, below is php code of integration, please review this code and let me know where I am doing wrong

<?php

namespace Drupal\ridishu_zoom_meeting\Controller;

require_once 'G:\php\xampp\htdocs\drupal-8.8.5\drupal-8.8.5\vendor\firebase\php-jwt\src\BeforeValidException.php';
require_once 'G:\php\xampp\htdocs\drupal-8.8.5\drupal-8.8.5\vendor\firebase\php-jwt\src\ExpiredException.php';
require_once 'G:\php\xampp\htdocs\drupal-8.8.5\drupal-8.8.5\vendor\firebase\php-jwt\src\SignatureInvalidException.php';
require_once 'G:\php\xampp\htdocs\drupal-8.8.5\drupal-8.8.5\vendor\firebase\php-jwt\src\JWT.php';

use \Firebase\JWT\JWT;
use Drupal\zoomapi;
use Drupal\Core\Controller\ControllerBase;

/**
 * Defines Zoom_meeting_controller class.
 */
class Zoom_meeting_controller extends ControllerBase {

  /**
   * Display the markup.
   *
   * @return array
   *   Return markup array.
   */

private $zoom_api_key = 'key';
private $zoom_api_secret = 'key';

  public function start_meeting() {
    $zoom_meeting = new Zoom_meeting_controller();
try{
    $z = $zoom_meeting->createAMeeting(array(
'start_date'=>date("Y-m-d h:i:s", strtotime('tomorrow')),
'meetingTopic'=>'Example Test Meeting',
'timezone'=>'Asia/Tashkent',
'alternative_host_ids'=>'ab@gmail.com'
));
print_r($z);
return $z;

} catch (Exception $ex) {
  echo "error in main method............";
//echo $ex;
}

  }

public function createAMeeting( $data = array() ) {
  $post_time = $data['start_date'];
  $start_time = gmdate( "Y-m-d\TH:i:s", strtotime( $post_time ) );
  $createAMeetingArray = array();

  $alternative_host_ids='trivedi.jyoti.05@gmail.com';

  $createAMeetingArray['topic'] = $data['meetingTopic'];
  $createAMeetingArray['agenda'] = ! empty( $data['agenda'] ) ? $data['agenda'] : "";
  $createAMeetingArray['type'] = ! empty( $data['type'] ) ? $data['type'] : 2; //Scheduled
  $createAMeetingArray['start_time'] = $start_time;
  $createAMeetingArray['timezone'] = $data['timezone'];
  $createAMeetingArray['password'] = ! empty( $data['password'] ) ? $data['password'] : "";
  //echo "1..................";
  $createAMeetingArray['duration'] = ! empty( $data['duration'] ) ? $data['duration'] : 60;
  $createAMeetingArray['settings'] = array(
  'join_before_host' => ! empty( $data['join_before_host'] ) ? true : false,
  'host_video' => ! empty( $data['option_host_video'] ) ? true : false,
  'participant_video' => ! empty( $data['option_participants_video'] ) ? true : false,
  'mute_upon_entry' => ! empty( $data['option_mute_participants'] ) ? true : false,
  'enforce_login' => ! empty( $data['option_enforce_login'] ) ? true : false,
  'auto_recording' => ! empty( $data['option_auto_recording'] ) ?
  $data['option_auto_recording'] : "none",
  'alternative_hosts' => $alternative_host_ids
  );
  return $this->sendRequest($createAMeetingArray);
}

protected function sendRequest($data) {
$email="tiwari.jyoti.05@gmail.com";
$request_url="https://api.zoom.us/v2/users/".$email."/meetings";
$jwt_token=$this->generateJWTKey();
//$jwt_token="token";
$headers = array(
"Authorization: Bearer ".$jwt_token,
"content-type: application/json"
);

$postFields = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_URL, $request_url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
$response = curl_exec($ch);
$err = curl_error($ch);
curl_close($ch);

if(!$response){
return $err;
}
return json_decode($response);
}

//function to generate JWT

private function generateJWTKey() {
$key = "rfZsA1MLSruI4MjBz0olUwCopy";
$secret = "HpMQvIREmG9QSevTuYaCos8T6Z3RJ5TULQ9L";
$token = array(
"iss" => $key,
"exp" => time() + 3600 //60 seconds as suggested
);
echo "<br>";
return JWT::encode( $token, $secret );
}

}
?>

Thank,
Jyoti

Hi Tommy,

Thanks for reply, below is php code of integration, please review this code and let me know where I am doing wrong


require_once 'G:\php\xampp\htdocs\drupal-8.8.5\drupal-8.8.5\vendor\firebase\php-jwt\src\BeforeValidException.php';
require_once 'G:\php\xampp\htdocs\drupal-8.8.5\drupal-8.8.5\vendor\firebase\php-jwt\src\ExpiredException.php';
require_once 'G:\php\xampp\htdocs\drupal-8.8.5\drupal-8.8.5\vendor\firebase\php-jwt\src\SignatureInvalidException.php';
require_once 'G:\php\xampp\htdocs\drupal-8.8.5\drupal-8.8.5\vendor\firebase\php-jwt\src\JWT.php';

use \Firebase\JWT\JWT;
use Drupal\zoomapi;
use Drupal\Core\Controller\ControllerBase;

/**
 * Defines Zoom_meeting_controller class.
 */
class Zoom_meeting_controller extends ControllerBase {

  /**
   * Display the markup.
   *
   * @return array
   *   Return markup array.
   */

private $zoom_api_key = 'key';
private $zoom_api_secret = 'key';

  public function start_meeting() {
    $zoom_meeting = new Zoom_meeting_controller();
try{
    $z = $zoom_meeting->createAMeeting(array(
'start_date'=>date("Y-m-d h:i:s", strtotime('tomorrow')),
'meetingTopic'=>'Example Test Meeting',
'timezone'=>'Asia/Tashkent',
'alternative_host_ids'=>'ab@gmail.com'
));
print_r($z);
return $z;

} catch (Exception $ex) {
  echo $ex;
}

  }

public function createAMeeting( $data = array() ) {
  $post_time = $data['start_date'];
  $start_time = gmdate( "Y-m-d\TH:i:s", strtotime( $post_time ) );
  $createAMeetingArray = array();

  $alternative_host_ids='trivedi.jyoti.05@gmail.com';

  $createAMeetingArray['topic'] = $data['meetingTopic'];
  $createAMeetingArray['agenda'] = ! empty( $data['agenda'] ) ? $data['agenda'] : "";
  $createAMeetingArray['type'] = ! empty( $data['type'] ) ? $data['type'] : 2; //Scheduled
  $createAMeetingArray['start_time'] = $start_time;
  $createAMeetingArray['timezone'] = $data['timezone'];
  $createAMeetingArray['password'] = ! empty( $data['password'] ) ? $data['password'] : "";
  //echo "1..................";
  $createAMeetingArray['duration'] = ! empty( $data['duration'] ) ? $data['duration'] : 60;
  $createAMeetingArray['settings'] = array(
  'join_before_host' => ! empty( $data['join_before_host'] ) ? true : false,
  'host_video' => ! empty( $data['option_host_video'] ) ? true : false,
  'participant_video' => ! empty( $data['option_participants_video'] ) ? true : false,
  'mute_upon_entry' => ! empty( $data['option_mute_participants'] ) ? true : false,
  'enforce_login' => ! empty( $data['option_enforce_login'] ) ? true : false,
  'auto_recording' => ! empty( $data['option_auto_recording'] ) ?
  $data['option_auto_recording'] : "none",
  'alternative_hosts' => $alternative_host_ids
  );
  return $this->sendRequest($createAMeetingArray);
}

protected function sendRequest($data) {
$email="tiwari.jyoti.05@gmail.com";
$request_url="https://api.zoom.us/v2/users/".$email."/meetings";
$jwt_token=$this->generateJWTKey();
//$jwt_token="token";
$headers = array(
"Authorization: Bearer ".$jwt_token,
"content-type: application/json"
);

$postFields = json_encode($data);
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch, CURLOPT_URL, $request_url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
$response = curl_exec($ch);
$err = curl_error($ch);
curl_close($ch);

if(!$response){
return $err;
}
return json_decode($response);
}

//function to generate JWT

private function generateJWTKey() {
$key = "rfZsA1MLSruI4MjBz0olUwCopy";
$secret = "HpMQvIREmG9QSevTuYaCos8T6Z3RJ5TULQ9L";
$token = array(
"iss" => $key,
"exp" => time() + 3600 //60 seconds as suggested
);
echo "<br>";
return JWT::encode( $token, $secret );
}

}
?>
    
Thanks,
Jyoti

Hey @tiwari.jyoti.05,

I am not an expert in PHP so please try referencing PHP documentation or stack overflow.

You can also use one of the PHP libraries listed here to generate a JWT:

Thanks,
Tommy

Hi Tommy,
Thanks for help, now my code is working and I am able to create a meeting in Zoom from my application.

Thanks,
Jyoti

1 Like

Hi Jyoti,

Can you please help me with generating the access token. i’m facing the same issue.

Thanks in advance!!!

Happy to hear you got it working @tiwari.jyoti.05! :slight_smile:

Thanks,
Tommy

Hey @sandipchavan1990,

Please provide more details about your issue. If you are trying to generate a token in PHP, use one of the PHP libraries listed here:

Thanks,
Tommy

Hi Tommy,

As I updated earlier now my code is working fine. thanks for help.

Thanks.

1 Like

Great! Happy to hear you got it working! :slight_smile:

Thanks,
Tommy