2017-11-13 4 views
0

をログインシステムを作成する私は、GoogleのAPIで新しいですし、私はのOauth GoogleのAPIを介して電子メールIDとユーチューブチャンネルデータをフェッチ、ログインシステムを作成しようとしています。私のコードの下には、データをフェッチするために完全に働いたが、私はページを更新するとき、私は、全体のデータを失い、再びログインをお願いしても、ログインシステムを作成することはできませんしています。 Plsのログインシステムをacheiveしても私のコードをチェックしてくださいする方法を私を導い正しいかどのようにGoogleのOAuthを経由

$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>'; 
    } 
+0

GoogleのPHPクライアントライブラリを試してみてください。それはあなたのために簡単になります。これをやりたくないのであれば、リフレッシュトークンを使って再度アクセスしてください。 – DaImTo

+0

あなたは私のコードでrefresh_tokenをどこで使用して再度アクセスすることができますか –

答えて

0

を短縮することができますが、上記のリクエストから取得access_tokenはあなたがサービスに要求を行うために使用されるものです。これはgrant_type = refresh_token

https://accounts.google.com/o/oauth2/token 
client_id={ClientId}.apps.googleusercontent.com&client_secret={ClientSecret}&refresh_token=1/ffYmfI0sjR54Ft9oupubLzrJhD1hZS5tWQcyAvNECCA&grant_type=refresh_token 

:注意:1時間後にあなたのアクセストークンは、それをあなたはあなたが上だrefresh_tokenを取る新しいaccess_tokenはを要求する必要があります期限切れとHTTPポストています応答:

{ 
"access_token" : "ya29.1.AADtN_XK16As2ZHlScqOxGtntIlevNcasMSPwGiE3pe5ANZfrmJTcsI3ZtAjv4sDrPDRnQ", 
"token_type" : "Bearer", 
"expires_in" : 3600 
} 
+0

私はrefresh_tokenを使って新しいaccess_tokenを取得していますが、常に他のユーザーのデータを表示しています。私のウェブサイトは、ユーザーが「Googleログイン」ボタンをクリックして自分のデータを取得するパブリック・ユーザーに基づいています。また、私はボタンをクリックした後、私のウェブサイトでユーザーのサインする必要があるが、私は2つの切り抜いたものである。この –

+0

を行う方法がわかりません。 OAuth2を使用すると、開いているIDは、ログインhttps://developers.google.com/identity/protocols/OpenIDConnectのために使用されているAPIにアクセスすることができます – DaImTo

関連する問題