2014年4月29日火曜日

Youtube API3.0 その2

Youtube API3.0 その2


<?php
error_reporting(E_ALL);
date_default_timezone_set('Asia/Tokyo');
session_start();


//OAuth 2.0 認証の実装
//https://developers.google.com/youtube/v3/guides/authentication?hl=ja#server-side-apps
//4/Qujeip_Txxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx18wxj3igI
//http://localhost/oauth2callback#error=access_denied

//$req_url = 'http://'.$_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"];

$x = new google_oauth;
$x->y3oauth();
$x->check();
//$x->step3();


class google_oauth {

//scope
// https://www.googleapis.com/auth/youtube Manage your YouTube account.
// https://www.googleapis.com/auth/youtube.readonly View your YouTube account.
// https://www.googleapis.com/auth/youtube.upload Upload YouTube videos and manage your YouTube videos.
// https://www.googleapis.com/auth/youtubepartner

public $step1_code = ''; //
public $client_id = 'xxxxxxxxx.apps.googleusercontent.com';
public $client_secret ='0Wxxxxxxxxxx9F';
public $redirect_uri = 'http://www.xxxxxxx.com/google/oauth.php';
public $scope = '';

public $access_token = '';
public $refresh_token = '';
public $token = array('access_token' => '',
 'token_type' => '',
 'expires_in' => '',
 'refresh_token' => '',
 'error' => '',
 'error_description' => '');

public function y3oauth(){


if(array_key_exists('you_access_token',$_COOKIE) and array_key_exists('you_refresh_token',$_COOKIE)){
//続き
$this->token['access_token'] = $_COOKIE['you_access_token'];
$this->token['refresh_token'] = $_COOKIE['you_refresh_token'];
$this->access_token = $this->token['access_token'];
}

if(!array_key_exists('you_access_token',$_COOKIE) and array_key_exists('you_refresh_token',$_COOKIE)){
//更新
$this->token['refresh_token'] = $_COOKIE['you_refresh_token'];
$this->step2();
setcookie('you_access_token',$this->token['access_token'],time()+$this->token['expires_in'],"/",'xxxxxxx.com');
$this->access_token = $this->token['access_token'];
}

if(!array_key_exists('you_access_token',$_COOKIE) and !array_key_exists('you_refresh_token',$_COOKIE)){
//新規
$this->scope = "https://www.googleapis.com/auth/youtube.upload https://www.googleapis.com/auth/youtube.readonly https://www.googleapis.com/auth/youtube.readonly https://www.googleapis.com/auth/youtubepartner";
$this->step1();
$this->step11();



$this->access_token = $this->token['access_token'];
}

if(array_key_exists('you_access_token',$_COOKIE) and !array_key_exists('you_refresh_token',$_COOKIE)){
//例外、you_access_tokenあり、you_refresh_tokenなし
$this->step3();
$this->access_token = $this->token['access_token'];
exit();
}
}


public function step1(){

//step1 アクセス トークンを取得する
$para['client_id'] = $this->client_id;
$para['redirect_uri'] = $this->redirect_uri;
$para['response_type'] = 'code';
$para['scope'] = $this->scope;
$para['approval_prompt'] = 'auto';
$para['access_type'] = 'offline';
$para['state'] = '';
$para['login_hint'] = '';
$oauth_rul ='https://accounts.google.com/o/oauth2/auth';
$step1_url = $oauth_rul.'?'.http_build_query($para);

if(!array_key_exists('code',$_GET)){
$tempurl ='location: '. $step1_url;
header($tempurl);
}else{
$this->step1_code = $_GET['code'];
}


}



public function step11(){

//step2 更新トークンとアクセス トークンの承認コードを交換する
$para['code'] = $this->step1_code;
$para['client_id'] = $this->client_id;
$para['client_secret'] = $this->client_secret;
$para['redirect_uri'] = $this->redirect_uri;
$para['grant_type'] = 'authorization_code';
$token_url ='https://accounts.google.com/o/oauth2/token';
$temp = http_build_query($para);

$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER ,true);
curl_setopt($ch, CURLOPT_URL, $token_url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $temp);
$code = curl_exec($ch);
$curlgetinfo=curl_getinfo($ch,CURLINFO_HEADER_OUT);
//$curlgetinfo=curl_getinfo($ch);
curl_close($ch);
foreach(json_decode($code) as $key => $val){
$this->token[$key] = $val;
}

setcookie('you_access_token',$this->token['access_token'],time()+$this->token['expires_in'],"/",'xxxxxxx.com');
setcookie('you_refresh_token',$this->token['refresh_token'], time()+60*60*24*30,"/",'xxxxxxx.com');

}


public function step2(){

//アクセス トークンの更新
$para['client_id'] = $this->client_id;
$para['client_secret'] = $this->client_secret;
$para['refresh_token'] = $this->token['refresh_token'];
$para['grant_type'] = 'refresh_token';
$token_url ='https://accounts.google.com/o/oauth2/token';
$temp = http_build_query($para);

$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER ,true);
curl_setopt($ch, CURLOPT_URL, $token_url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $temp);
$code = curl_exec($ch);
$curlgetinfo=curl_getinfo($ch,CURLINFO_HEADER_OUT);
//$curlgetinfo=curl_getinfo($ch);
curl_close($ch);
foreach (json_decode($code) as $key => $val){
$this->token[$key] = $val;
}

//更新でもトークンが取得できなければ、アクセス トークンを取得する
if ($this->token['access_token'] == ''){
$this->step1();
$this->step11();
}
}


public function step3(){
//取り消す
$this->token['access_token'] = $_COOKIE['you_access_token'];
$cl_url = 'https://accounts.google.com/o/oauth2/revoke?token='.$this->token['access_token'] ;

setcookie('you_access_token',$this->token['access_token'],time()-3600,"/",'xxxxxxx.com');
setcookie('you_refresh_token',$this->token['refresh_token'], time()-3600,"/",'xxxxxxx.com');

$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HTTPGET, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER ,true);
curl_setopt($ch, CURLOPT_URL, $cl_url);
$code = curl_exec($ch);
$curlgetinfo=curl_getinfo($ch,CURLINFO_HEADER_OUT);
//$curlgetinfo=curl_getinfo($ch);
curl_close($ch);
exit();

}

public function check(){
if($this->access_token != ''){
header('location: '.$_SESSION['req_url']);
}
}

public function ac_token(){
return $this->access_token;
}
}





0 件のコメント: