Gotowebinar_Helpers::validate_oauth_tokens()

Callback function for OAuth redirect verification.


Return Return

(void)


Source Source

File: src/integrations/gotowebinar/helpers/gotowebinar-helpers.php

	public function validate_oauth_tokens() {

		if ( ! automator_filter_has_var( 'state' ) || $this->setting_tab !== automator_filter_input( 'state' ) ) {
			return;
		}

		if ( ! automator_filter_has_var( 'code' ) ) {
			return;
		}

		$consumer_key = trim( get_option( 'uap_automator_gtw_api_consumer_key', '' ) );
		$consumer_secret = trim( get_option( 'uap_automator_gtw_api_consumer_secret', '' ) );

		$code = automator_filter_input( 'code' );

		$params = array(
			'method' => 'POST',
			'url' => 'https://api.getgo.com/oauth/v2/token',
			'headers' => array(
				'Content-Type'  => 'application/x-www-form-urlencoded; charset=utf-8',
				'Authorization' => 'Basic ' . base64_encode( $consumer_key . ':' . $consumer_secret ), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode
				'Accept'        => 'application/json',
			),
			'body'    => array(
				'code'       => $code,
				'grant_type' => 'authorization_code',
				//'redirect_uri' => urlencode( $tab_url ),
			)
		);

		$connect = 2;

		try {

			$response = $this->remote_request( $params );

			if ( 200 !== wp_remote_retrieve_response_code( $response ) ) {
				throw new \Exception( __( 'Error validating Oauth tokens', 'uncanny-automator' ) );
			}

			$jsondata = array();
			
			//lets get the response and decode it
			$jsondata = json_decode( $response['body'], true );

			update_option( '_uncannyowl_gtw_settings', $jsondata );
			delete_option( '_uncannyowl_gtw_settings_expired' );

			// Set the transient.
			set_transient( '_uncannyowl_gtw_settings', $jsondata['access_token'] . '|' . $jsondata['organizer_key'], 60 * 50 );

			$connect = 1;

		} catch ( \Exception $e ) {
			automator_log( $e->getMessage() );
		}
		
		wp_safe_redirect( automator_get_premium_integrations_settings_url( 'go-to-webinar' ) . '&connect=' . $connect );
		die;

	}