Skip to content

Instantly share code, notes, and snippets.

@kartikparmar
Created September 22, 2023 07:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kartikparmar/c3331d2ca11900e36567e9e672d811ab to your computer and use it in GitHub Desktop.
Save kartikparmar/c3331d2ca11900e36567e9e672d811ab to your computer and use it in GitHub Desktop.
Connect to Zoom
<?php
/**
* Plugin Name: Zoom OAuth Demo
* Plugin URI: http://www.tychesoftwares.com/
* Description: This plugin is for showing the demo for Zoom Connection via OAuth.
* Version: 1.0
* Author: Tyche Softwares
* Author URI: http://www.tychesoftwares.com/
* Text Domain: zoom-oauth-remo
* Domain Path: /i18n/languages/
* Requires PHP: 5.6
*
* @package Zoom-OAuth-Demo
*/
if ( ! defined( 'ZOOM_CLIENT_ID' ) ) {
define( 'ZOOM_CLIENT_ID', 'FMVtfBKxTy2WpRRss6Ky4A' );
}
if ( ! defined( 'ZOOM_CLIENT_SECRET' ) ) {
define( 'ZOOM_CLIENT_SECRET', 'fz1svs7vVT7RZ4Ufz2YYoxNlVDKoKF4Z' );
}
if ( ! defined( 'ZOOM_REDIRECT_URL' ) ) {
define( 'ZOOM_REDIRECT_URL', 'https://team-sandbox.tychesoftwares.com/kartik/wp-admin/?kartik=parmar' );
}
if ( ! defined( 'ZOOM_API_URL' ) ) {
define( 'ZOOM_API_URL', 'https://api.zoom.us/v2/' );
}
function zoom_oauth_connection() {
if ( isset( $_GET['kartik'] ) && 'parmar' === $_GET['kartik'] ) {
$zoom_redirect_url = admin_url( '?kartik=parmar' );
if ( isset( $_GET['code'] ) && '' !== $_GET['code'] ) { // phpcs:ignore
$response = wp_remote_post(
'https://zoom.us/oauth/token', // to fetch token based on the code data we got.
array(
'body' => array(
'grant_type' => 'authorization_code',
'code' => $_GET['code'],
'client_id' => ZOOM_CLIENT_ID,
'client_secret' => ZOOM_CLIENT_SECRET,
'redirect_uri' => $zoom_redirect_url,
),
)
);
if ( is_wp_error( $response ) ) {
return false;
}
$data = json_decode( wp_remote_retrieve_body( $response ), true );
if ( isset( $data['access_token'] ) ) {
update_option( 'zoom_token', $data['access_token'] );
wp_safe_redirect( $zoom_redirect_url ); exit();
}
}
$token = get_option( 'zoom_token', '' );
if ( '' !== $token ) {
echo 'You are successfully connected to Zoom';
// Here you can make a calls using access token.
$request_url = ZOOM_API_URL . 'users/';
$args = array(
'headers' => array(
'Authorization' => 'Bearer ' . $token,
'Content-Type' => 'application/json',
),
'body' => array(),
);
$response = wp_remote_get( $request_url, $args );
$response_body = wp_remote_retrieve_body( $response );
$response_data = json_decode( $response_body, true );
if ( $response_data['users'] ) {
echo '<br>Below are the list of users.</br>';
foreach ( $response_data['users'] as $key => $value ) {
echo $value['display_name'] . ' ' . $value['email'] . '<br>';
}
} else {
echo 'No users found!';
}
//echo '<pre>'; print_r( $response_data ); echo '</pre>';
exit();
} else {
$connect_to_zoom_str = __( 'Connect to Zoom', 'woocommerce-booking' );
$args = array(
'response_type' => 'code',
'client_id' => ZOOM_CLIENT_ID,
'redirect_uri' => rawurlencode( $zoom_redirect_url ),
);
$zoom_url = add_query_arg( $args, 'https://zoom.us/oauth/authorize' );
printf( '<a class="button button-primary" href="%s">%s</a>', esc_url( $zoom_url ), esc_html( $connect_to_zoom_str ) );
exit();
}
}
}
add_action( 'admin_init', 'zoom_oauth_connection' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment