我正在使用Google OAuth API创建login system 和获取youtube channel data 在Wordpress中。我已经完成了获取频道数据和电子邮件id的工作access_token, token_type, expires_in, refresh_token 和id_token 但我不知道如何在Wordpress中实现登录系统,有人能指导我如何做到这一点,并让我知道我下面的代码是正确的或可以缩短。
谢谢
$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 email 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 ) );
$email = $eamilOBJ->email;
if (isset($_SESSION[\'access_token\']) && $_SESSION[\'access_token\']) {
$url = \'https://www.googleapis.com/youtube/v3/channels?fields=items(id,snippet(title,description,customUrl,thumbnails(default)),statistics(viewCount,subscriberCount))&part=snippet%2Cstatistics&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->items[0]->id;
$thumbnail_url = $channelOBJ->items[0]->snippet->thumbnails->default->url;
$youtubetitle = $channelOBJ->items[0]->snippet->title;
$description = $channelOBJ->items[0]->snippet->description;
$total_subscriber = $channelOBJ->items[0]->statistics->subscriberCount;
echo \'Email ID : \'.$email;
echo \'Image: URL: \'.$thumbnail_url;
echo \'Channel Title: \'.$youtubetitle;
echo \'Total 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.email&
approval_prompt=force&
access_type=offline">
<button class="loginBtn loginBtn--google">Sign in with Google</button></a>\';
}
SO网友:Piyush Rawat
我还在开发使用谷歌oAuth和YouTube数据API的插件。该功能在后端工作,管理员只需使用他的gmail凭据登录一次,然后使用refresh_token 我们可以让他登录。
请记住,刷新令牌仅在您第一次授予应用程序访问权限时生成。你必须clear your permissions 再把它拿回来。
我使用的是PHP而不是javascript,但下面是我的完整函数,让您简单了解一下
<?php
public function uty_main_page(){
require_once (\'config.php\');
global $wpdb;
if(isset($_GET[\'code\'])) {
$client->authenticate($_GET[\'code\']);
$_SESSION[\'token\'] = $client->getAccessToken();
$token_decode = json_decode($_SESSION[\'token\']);
update_option( \'uty_refresh_token\' , $token_decode->refresh_token );
$client->setAccessToken($_SESSION[\'token\']);
echo \'Authorization Successful\';
}
?>
<?php if(get_option( \'uty_refresh_token\' ) != \'\'){ ?>
<li><a href="#uty_upload">Upload</a></li>
<li><a href="#uty_videos">All Videos</a></li>
<?php } ?>
<li><a href="#uty_settings">Settings</a></li>
</ul>
</div>
<div class="tab_content">
<?php if( $client->isAccessTokenExpired() ) { ?>
<?php if(get_option( \'uty_refresh_token\' ) != \'\'){ ?>
<?php
$client->refreshToken( get_option( \'uty_refresh_token\' ) );
$_SESSION[\'token\'] = $client->getAccessToken();
$client->setAccessToken($client->getAccessToken());
?>
<div id="uty_upload">
<?php
if(isset($_POST[\'uty_video_submit\'])){
if(isset($_FILES[\'uty_video\'])){
$uploaded = media_handle_upload(\'uty_video\', 0);
if(is_wp_error($uploaded)){
$message = $uploaded->get_error_message();
}
else{
//Do Some coding here
}
}
}
?>
</div>
<?php } ?>
<?php } ?>
<div id="uty_settings">
<?php if(isset($_POST[\'revoke-access-token\'])){ ?>
<?php update_option( \'uty_refresh_token\' , \'\' ); ?>
<?php update_option( \'uty_google_client_api\' , \'\' ); ?>
<?php update_option( \'uty_google_client_id\' , \'\' ); ?>
<?php update_option( \'uty_google_client_secret\' , \'\' ); ?>
<?php update_option( \'uty_youtube_channel\' , \'\' ); ?>
<?php } ?>
<h3>Please enter the below details to kick start your Youtube uploads</h3>
<?php if( !empty(get_option(\'uty_google_client_api\')) && !empty(get_option(\'uty_google_client_id\')) && !empty(get_option(\'uty_google_client_secret\')) && empty(get_option(\'uty_refresh_token\')) ){ ?>
<?php $state = mt_rand(); ?>
<?php $client->setState($state); ?>
<?php $_SESSION[\'state\'] = $state; ?>
<?php $authUrl = $client->createAuthUrl(); ?>
<a href="<?php echo $authUrl; ?>">Click here to authorize your credentials</a>
<?php } elseif( !empty(get_option(\'uty_google_client_api\')) && !empty(get_option(\'uty_google_client_id\')) && !empty(get_option(\'uty_google_client_secret\')) && !empty(get_option(\'uty_refresh_token\')) ){ ?>
<?php } ?>
</div>
</div>
</div>
</div>
<?php } ?>
在上述函数中,我所做的主要工作是存储
refresh_token 当用户第一次登录并将其传入函数时,每次都会获取新的访问令牌。
$client->refreshToken( get_option( \'uty_refresh_token\' ) );
$_SESSION[\'token\'] = $client->getAccessToken();
$client->setAccessToken($client->getAccessToken());
这三条线路每次都要获取新的访问令牌。希望这对你有帮助