Invalid access token while Creating meeting JWT

Hi
i am trying to crate a meeting but it gives me invalid access token , Here is my code

use \Firebase\JWT\JWT;
class Zoom_Api {
		private $zoom_api_key = '***************';
		private $zoom_api_secret = '**************';
		
		protected function sendRequest($data) {
            $request_url = 'https://api.zoom.us/v2/users/me/meetings';
          

 $headers = array( "authorization: {$this->generateJWTKey()}", 'content-type: application/json' );
 $postFields = json_encode($data);
           
		   $ch = curl_init();
			curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
			curl_setopt($ch, CURLOPT_URL, $request_url);
			curl_setopt($ch, CURLOPT_POST, 1);
			curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
			$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 = $this->zoom_api_key;
            $secret = $this->zoom_api_secret;
            $token = array(
                "iss" => $key,
                "exp" => time() + 3600 //60 seconds as suggested
            );
            return JWT::encode( $token, $secret );
        }
		
		public function createAMeeting( $data = array() ) {
            $post_time  = $data['start_date'];
			$start_time = gmdate( "Y-m-d\TH:i:s", strtotime( $post_time ) );
            $createAMeetingArray = array();
            if ( ! empty( $data['alternative_host_ids'] ) ) {
                if ( count( $data['alternative_host_ids'] ) > 1 ) {
                    $alternative_host_ids = implode( ",", $data['alternative_host_ids'] );
                } else {
                    $alternative_host_ids = $data['alternative_host_ids'][0];
                }
            }
            $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'] : "";
            $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' => isset( $alternative_host_ids ) ? $alternative_host_ids : ""
            );
            return $this->sendRequest($createAMeetingArray);
        }

	}


$zoom_meeting = new Zoom_Api();
try{
$z = $zoom_meeting->createAMeeting(
	array(
		'start_date'=>date("Y-m-d h:i:s", strtotime('tomorrow')),
		'topic'=>'Example Test Meeting'
	)
);
echo $z->message;
} catch (Exception $ex) {
echo $ex;
}

Any help ???

Hey @m.elsisi,

Have you tried using the JWT Token generated for you in the marketplace to see if that works?

Thanks,
Tommy

the JWT generated token in marketplace works fine it gives me this:

{“page_count”:1,“page_number”:1,“page_size”:30,“total_records”:1,“users”:[{“id":“CnbSZ6UvTaCH1qXyo7tk3w”,“first_name”:“Mohamed”,“last_name”:“Elsisi”,“email”:"m.elsisi@focuspublish.com”,“type”:1,“pmi”:4235506987,“timezone”:“Africa/Cairo”,“verified”:1,“created_at”:“2020-06-10T18:17:53Z”,“last_login_time”:“2020-08-14T13:15:54Z”,“last_client_version”:“5.1.28652.0706(android)”,“language”:“en-US”,“phone_number”:"",“status”:“active”}]}

Now what is wrong in my code also how can i get the meeting id and password from the reply ?

Hey @m.elsisi,

I am not sure what is wrong with your code. You can see libraries here: jwt.io to help you generate your JWT Token properly.

As for getting the meetingID and passcode, you can use the Get Meeting API.

Thanks,
Tommy

hi @tommy
The link you send is to get meeting id and password for OATH type not JWT type ,
I want to get the meeting id and password from this token response

{“page_count”:1,“page_number”:1,“page_size”:30,“total_records”:1,“users”:[{“id”:“CnbSZ6UvTaCH1qXyo7tk3w”,“first_name”:“Mohamed”,“last_name”:“Elsisi”,“email”:"*****",“type”:1,“pmi”:4235506987,“timezone”:“Africa/Cairo”,“verified”:1,“created_at”:“2020-06-10T18:17:53Z”,“last_login_time”:“2020-08-14T13:15:54Z”,“last_client_version”:“5.1.28652.0706(android)”,“language”:“en-US”,“phone_number”:"",“status”:“active”}]}

Hey @m.elsisi,

You can still use a JWT App to call the Get Meeting API.

Just pass in the authorization token as the auth header.

"Authorization": "Bearer JWT_TOKEN_HERE"

Thanks,
Tommy

HI @tommy
The Get Meeting API needs a meeting id to retrieve its info , but as you see the response i get doesn’t contain a meeting id , it has only id ( i don’t know id refers to what )
and i am passing the Authorization token to auth header
here is my code :

> <?php
>  require_once 'vendor/firebase/phpjwt/src/BeforeValidException.php';
>   require_once 'vendor/firebase/phpjwt/src/ExpiredException.php';
>  require_once 'vendor/firebase/phpjwt/src/SignatureInvalidException.php';
>  require_once 'vendor/firebase/phpjwt/src/JWT.php';
> 
> 
>     use \Firebase\JWT\JWT;
>     $zoom_api_key = '******';
> 		 $zoom_api_secret = '*****';
> 		
> 		$curl = curl_init();
> 
>     curl_setopt_array($curl, array(
>      CURLOPT_URL => "https://api.zoom.us/v2/users?status=active&page_size=30&page_number=1",
>      CURLOPT_RETURNTRANSFER => true,
>     CURLOPT_ENCODING => "",
>      CURLOPT_MAXREDIRS => 10,
>     CURLOPT_TIMEOUT => 30,
>     CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
>      CURLOPT_CUSTOMREQUEST => "GET",
>     CURLOPT_HTTPHEADER => array(
>     "authorization:Bearer  My token",
>     "content-type: application/json"
>      ),
>     ));
> 
>     $response = curl_exec($curl);
>     $err = curl_error($curl);
> 
>     curl_close($curl);
> 
>     if ($err) {
>      echo "cURL Error #:" . $err;
>     } else {
>      echo $response;
>      }
>     ?>

Hey @m.elsisi,

You can get a meeting ID from the Meeting webhooks, or the List Meetings endpoint.

Thanks,
Tommy

Hi @tommy
Sorry for the late reply
The meeting webhooks and list meeting endpoints are for Oath type ( they ask for Client ID and Client Secret ) i am using JWT , so how can i retrieve the meeting ID and Password , I would appreciate if there is a PHP example i can follow

Hey @m.elsisi,

You can use a JWT App to call the API and enable Webhooks. :slight_smile:

Just generate a JWT Token to make API requests.

And here is how to enable webhooks in a JWT App.

Thanks,
Tommy