Timeouts when generating access token

Description
We are using plugin to generate access tokens but getting timeout whiles doing so. Please help us to debug this problem. This doesn’t happen always but happens frequently

Error
cURL error 28: Connection timed out after 5001 milliseconds

Which App Type (OAuth / Chatbot / JWT / Webhook)?
JWT

Which Endpoint/s?
$response = wp_remote_post( ‘https://zoom.us/oauth/token’, array(

    'headers' => array(

        'Content-Type' => 'application/x-www-form-urlencoded; charset=utf-8',

        'Authorization' => 'Basic '.woocommerce_to_zoom_get_authorisation(),

        'Cache-Control' => 'no-cache',

    ),

    'body' => array(

        'refresh_token' => $refresh_token,

        'grant_type' => 'refresh_token',

    ),

));

How To Reproduce (If applicable)
Steps to reproduce the behavior:

  1. Request URL / Headers (without credentials) / Body
  2. See error

Screenshots (If applicable)
If applicable, add screenshots to help explain your problem.

Additional context
Add any other context about the problem here.

Hi @meditationwithachari,

Do you run into this same issue when you hit the Zoom APIs directly, such as in Postman? If you only run into this issue while using a 3rd party plugin, this could be an issue with the plugin. Can you share additional details about which plugin you’re using and how it’s authenticating requests to Zoom?

Thanks,
Will

Hi Will

Good day!

Thanks for the reply. No this happens only when using the plugin. But still

This is the plugin we are using : Official page

Following are the main snippets that involve in generating registrants. The plugin developer was of no help so I took it upon myself to resolve this.

What happens, succinctly, in this plugin is once a customer pays (using woocommerce) for the webinar,

function woocommerce_to_zoom_add_registrant() gets called. This function in turn calls the function woocommerce_to_zoom_get_access_token() to get the access_token. There in lies the problem. This function sometimes fails to retrieve the access _token due to the timeout.

 function woocommerce_to_zoom_add_registrant( $order_id ) {

    //start WooCommerce logging
    $logger = wc_get_logger();
    $context = array( 'source' => 'woocommerce-to-zoom' );
    $logger->info( 'STARTING PROCESSING ORDER: '.$order_id, $context );

    //get order object inc ase we need it
    $order = new WC_Order( $order_id );

    //get the metadata for the order
    $all_post_meta = get_post_meta($order_id);

    foreach($all_post_meta as $meta_key => $meta_value){

        //only proceed for keys which contain zoom_webinar
        if (strpos($meta_key, 'zoom_webinar') !== false) {
            //key looks like: zoom_webinar-217072930-1
            $key_exploded = explode('-',$meta_key);
            $webinar_id = $key_exploded[1];
            $webinar_id = woocommerce_to_zoom_sanitize_webinar_id($webinar_id);

            $meta_value = get_post_meta($order_id,$meta_key,true);

            $body_data_to_json = json_encode($meta_value);

            //register the person for the webinar
            $url = woocommerce_to_zoom_get_api_base().'webinars/'.$webinar_id.'/registrants';

            $access_token = woocommerce_to_zoom_get_access_token();

            $response = wp_remote_post( $url, array(
                'headers' => array(
                    'Authorization' => 'Bearer '.$access_token,
                    'Content-Type' => 'application/json; charset=utf-8',
                ),
                'body' => $body_data_to_json,
            ));

            $status = wp_remote_retrieve_response_code( $response );

            

            if($status == 201){
                //do something
                $decodedBody = json_decode(preg_replace('/("\w+"):(\d+(\.\d+)?)/', '\\1:"\\2"', $response['body']), true);
                $registrant_id = $decodedBody['registrant_id'];
                $join_url = $decodedBody['join_url'];

                //lets add meta to the order id
                if(get_post_meta($order_id,'zoom_registrant_ids',false)){
                    $existing_registrants = get_post_meta($order_id,'zoom_registrant_ids',true);

                    $existing_registrants[$registrant_id] = array(
                        'first_name' => $meta_value['first_name'],
                        'last_name' => $meta_value['last_name'],
                        'email' => $meta_value['email'],
                        'join_url' => $join_url,
                        'webinar_id' => $webinar_id,
                    ); 

                    update_post_meta( $order_id, 'zoom_registrant_ids', $existing_registrants );

                } else {
                    update_post_meta( $order_id, 'zoom_registrant_ids', array($registrant_id => array(
                        'first_name' => $meta_value['first_name'],
                        'last_name' => $meta_value['last_name'],
                        'email' => $meta_value['email'],
                        'join_url' => $join_url,
                        'webinar_id' => $webinar_id,
                    )));
                }

                //we will also log the success
                $logger->info('USER ADDED TO ZOOM WEBINAR ('.$webinar_id.'): '.$meta_value['first_name'].' '.$meta_value['last_name'],$context);
                

            } else {
                //something bad happened
                $logger->info('URL: '.$url,$context);
                $logger->info('BODY: '.$body_data_to_json,$context);
                $logger->info('ACCESS TOKEN: '.$access_token,$context);
                $logger->info('STATUS: '.$status,$context);

                //if in error, $response would be an object of WP_Error not an array
                //check this
                if(is_array($response)){
                    $logger->info('RETURN BODY: '.$response['body'],$context);
                }
                else if(is_wp_error( $response )){
                   
                    $error_message = $response->get_error_message();
                    error_log('wc2zoom wp add registrant error '.$error_message);
                    
                }
                
            }

        }
    }   
    

}

function woocommerce_to_zoom_get_access_token() {

$refresh_token = get_option('wc_settings_zoom_refresh_token');

//debug
$start = microtime(true);
error_log('wc2zoom get access token process started at '.$start );

$response = wp_remote_post( 'https://zoom.us/oauth/token', array(
    'headers' => array(
        'Content-Type' => 'application/x-www-form-urlencoded; charset=utf-8',
        'Authorization' => 'Basic '.woocommerce_to_zoom_get_authorisation(),
        'Cache-Control' => 'public,max-age=3300',
    ),
    'body' => array(
        'refresh_token' => $refresh_token,
        'grant_type' => 'refresh_token',
    ),
));

$status = wp_remote_retrieve_response_code( $response );

//debug
$end = microtime(true);
error_log('wc2zoom token status ' .$status );
error_log('wc2zoom get access token process ended after ' ($end - $start) );

//debug
if ( is_wp_error( $response ) ) {
    error_log('wc2zoom wp remote response error in woocommerce_to_zoom_get_access_token '. print_r($response,true));
}


if($status == 200){

    $decodedBody = json_decode(preg_replace('/("\w+"):(\d+(\.\d+)?)/', '\\1:"\\2"', $response['body']), true);

    $refresh_token = $decodedBody['refresh_token'];
    $access_token = $decodedBody['access_token'];
  
    //update the options with the result
    update_option('wc_settings_zoom_refresh_token', $refresh_token);

    //debug
    error_log('wc2zoom token updated ref token' .$refresh_token. ' access token '.$access_token );

    return $access_token;

} else {

    //debug
    error_log('wc2zoom refresh token error returned' );
    //recursively try to get the access token
    woocommerce_to_zoom_get_access_token();
    //return 'ERROR';

} 

}

Please give a look see and let me know

Best Regards

Hi @meditationwithachari ,

Thanks for clarifying and sharing these details.

First, I should note that we recommend working with the app developer directly, as this particular app wasn’t built by Zoom.

Having said that, two things I would check:

  • If possible, increase any timeout durations and see if that helps
  • Check that the Authorization header that’s being passed is valid and is a Base64Encoded string containing the user/account (who’s authorizing) OAuth Client ID and Secret. In other words, I would check the function that’s getting called within the Authorization header is behaving as intended.

While these are hopefully helpful pointers, I’d encourage you to reach out to Northern Beaches Websites directly for pointed support:

Thanks,
Will

Hi @will.zoom

Developers wasn’t much of a help. That’s why we are seeking you to figure out this.

I’m back to square one i guess.

Hi @meditationwithachari,

Thank you for clarifying, and I hear you. Since they developed this app, however, they will be the best source to help debug this. If you’re in touch with them, you’re welcome to encourage them to reach out to us directly as well.

Thanks,
Will

can you at least tell me is there anything that could’ve improve in the below request to avoid a timeout?

This is the function that connects to zoom to get an access token. wp_remote_post is a standard wordpress function that performs an HTTP request using the POST method and returns its response.

Also is there a minimum wait time before we can make connection requests to zoom?

$response = wp_remote_post( 'https://zoom.us/oauth/token', array(
'headers' => array(
    'Content-Type' => 'application/x-www-form-urlencoded; charset=utf-8',
    'Authorization' => 'Basic '.woocommerce_to_zoom_get_authorisation(),
    'Cache-Control' => 'public,max-age=3300',
),
'body' => array(
    'refresh_token' => $refresh_token,
    'grant_type' => 'refresh_token',
),

));

Hey @meditationwithachari,

Thank you for the update. I’m not aware of the timeout and I haven’t encountered that before but one thing I’m noticing about that request is that you’re using is formatted incorrectly.

When making a request to the https://zoom.us/oauth/token endpoint, you’ll want to use query parameters instead of a form body in order to make the request. We have an example of what this looks like in our documentation

https://zoom.us/oauth/token?grant_type=authorization_code&code=obBEe8ewaL_KdYKjnimT4KPd8KKdQt9FQ&redirect_uri=https://yourapp.com

Let me know if that helps.

Thanks,
Max

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.