Site icon Hip-Hop Website Design and Development

use google api for wordpress login

I’m utilizing Google OAuth API to create a login system and fetching youtube channel knowledge in WordPress. I’ve executed to get channel knowledge and electronic mail id additionally get access_token, token_type, expires_in, refresh_token and id_token however I don’t know find out how to obtain login system in WordPress can anybody information me how to try this and likewise let me know my beneath code is right or may be shorten.

Thanks

$client_id = 'xxxxxxxxxxxxxxxxxxx';
$client_secret = 'xxxxxxxxxxxxxxx';
$redirect_uri = 'http://localhost/mysite/oauth2callback';
$code = $_GET["code"];

$oauth2token_url = "https://accounts.google.com/o/oauth2/token";
$clienttoken_post = array(
        "code" => $code,
        "client_id" => $client_id,
        "client_secret" => $client_secret,
        "redirect_uri" => $redirect_uri,
        "grant_type" => "authorization_code"
);

$curl = curl_init($oauth2token_url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $clienttoken_post);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);  

$json_response = curl_exec($curl); 
curl_close($curl);
$authObj = json_decode($json_response);
$access_token = $authObj->access_token;
$token_type = $authObj->token_type;
$expires_in = $authObj->expires_in;
$refresh_token = $authObj->refresh_token;
$Id_token = $authObj->id_token;

session_start();
$_SESSION['access_token'] = $access_token;  

//getting the e-mail id
$email_url = 'https://www.googleapis.com/oauth2/v1/userinfo?alt=json&access_token='.$_SESSION['access_token'];
$em = curl_init();
curl_setopt( $em, CURLOPT_URL, $email_url );
curl_setopt($em, CURLOPT_HEADER, 0);    
curl_setopt( $em, CURLOPT_RETURNTRANSFER, 1 );
$eamilOBJ = json_decode( curl_exec( $em ) );
$electronic mail = $eamilOBJ->electronic mail;


if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {        
    $url = 'https://www.googleapis.com/youtube/v3/channels?fields=objects(id,snippet(title,description,customUrl,thumbnails(default)),statistics(viewCount,subscriberCount))&half=snippetpercent2Cstatistics&mine=true&access_token='.$_SESSION['access_token'];

    $ch = curl_init();
    curl_setopt( $ch, CURLOPT_URL, $url );
    curl_setopt($ch, CURLOPT_HEADER, 0);    
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
    $channelOBJ = json_decode( curl_exec( $ch ) );

    $channel_id = $channelOBJ->objects[0]->id;
    $thumbnail_url = $channelOBJ->objects[0]->snippet->thumbnails->default->url;
    $youtubetitle = $channelOBJ->objects[0]->snippet->title;
    $description = $channelOBJ->objects[0]->snippet->description;
    $total_subscriber = $channelOBJ->objects[0]->statistics->subscriberCount;

    echo 'Electronic mail ID : '.$electronic mail;
    echo 'Picture: URL: '.$thumbnail_url;
    echo 'Channel Title: '.$youtubetitle;
    echo 'Whole Subscriber: '.$total_subscriber;    
}
else{
    echo '<a href="https://accounts.google.com/o/oauth2/auth?
        redirect_uri=http://localhost/mysite/oauth2callback&
        response_type=code&
        client_id=xxxxxxxxxxxxxxx&
        scope=https://www.googleapis.com/auth/youtube.readonly+https://www.googleapis.com/auth/userinfo.electronic mail&
        approval_prompt=drive&
        access_type=offline">
        <button class="loginBtn loginBtn--google">Sign up with Google</button></a>';
   }