ラベル Youtube API3.0 の投稿を表示しています。 すべての投稿を表示
ラベル Youtube API3.0 の投稿を表示しています。 すべての投稿を表示

2015年8月6日木曜日

Google にアプリケーションIDの取得サイト

Google にアプリケーションIDの取得サイト

https://appengine.google.com/
https://console.developers.google.com/project




2015年4月26日日曜日

Youtube API 2.0 サポート終了、API3.0のサーチのために記録

Youtube API 2.0 サポート終了のため、検索結果に「UKY3scPIMd8」
動画が追加されます。 API3.0のOath認証しないサーチのプログラム



<?php
//YoutubeClass.php ver 2
//youtube api 3.0
//https://www.youtube.com/watch?v=UKY3scPIMd8

$g=$_GET['g'];

if($g){
$x=new YoutubeAPI($g);
$x->link();
}else{
exit('88');
}


class youtubeapi {
public $url;
public function youtubeapi($key){
$API_URL = 'https://www.googleapis.com/youtube/v3/search?';
$para['key'] = 'AI********************************Ao';
$para['part'] = 'id';
$para['q'] = $key;
$para['type'] = 'video';
$para['maxResults'] = 3;
$this->url = $API_URL.http_build_query($para);
}

public function link(){
$ll=array();
$data = file_get_contents($this->url);
$xdata = json_decode($data ,true);
foreach ($xdata['items'] as $key=>$val){
$ll[count($ll)]=$val['id']['videoId'];
}
unset ($key);
unset ($val);
echo implode('|',$ll);
}
}

?>

2014年4月29日火曜日

Youtube API 3.0 の応用

Youtube API 3.0 の応用

1. Youtube リスト作成、公開、List ID
2. 必要な曲を検索する、Video ID
3. Video ID を List ID に追加する。





https://www.youtube.com/user/icq7219388

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;
}
}





YouTube Data API 3.0 その1

Function Youtube API3.0



<?php
error_reporting(0);
date_default_timezone_set('Asia/Tokyo');
//session_start();
$_SESSION['req_url'] = 'http://'.$_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"];

if(array_key_exists('you_access_token',$_COOKIE)){
$token = $_COOKIE['you_access_token'];
}else{
$_SESSION['req_url'] = 'http://'.$_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"];
header('location: http://www.xxxxxxx.com/google/oauth.php');
}

?>
<?php
//YouTube Data API 3.0
//Searchメソッド
set_time_limit(0);
$xx = new dataapi;
$xx->token_check(); //
//$xx->c_play_list('本語 対応しますか'); //再生リストの作成、公開 名前の入力が必須 return list id;
//$xx->c_play_item_insert(Lid,Vid); //
//$xx->search('Part Of Me - Katy Perry'); //サーチ return Video ID array[0[;





class dataapi{

public function dataapi(){



}

public function token_check(){

if(array_key_exists('you_access_token',$_COOKIE)){
$token = $_COOKIE['you_access_token'];
}else{
$_SESSION['req_url'] = 'http://'.$_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"];
header('location: http://www.xxxxxxx.com/google/oauth.php');
}
}


public function search($q){
//API リクエストで指定したクエリ パラメータに一致する検索結果のコレクションを返します。
$request_url = 'https://www.googleapis.com/youtube/v3/search';
$mode = 'GET';
$para['part'] = 'id'; //必須パラメータ
$para['q'] = $q;
$para['type'] = 'video';

$curl_url = $request_url.'?'.http_build_query($para);

$temp = $this->curl($mode,$curl_url);

return $temp['items']['0']['id']['videoId'];

//echo '<pre>';
//echo 'search';
//print_r($temp);
}


public function c_play_list($name){
//playlist リソースは YouTube の再生リストを表します。
//再生リストを作成します   Playlists: insert

$request_url ='https://www.googleapis.com/youtube/v3/playlists';
$mode = 'POST';
$para['part'] = 'snippet,status';
$paras['snippet'] = array('title' => $name);
$paras['status'] = array ('privacyStatus' => 'public');

$curl_url = $request_url.'?'.http_build_query($para);

$temp = $this->curl($mode,$curl_url,$paras);

//echo '<br>';
// print_r($temp);


return $temp['id'];
}






public function c_play_item_insert($Lid,$Vid){
//playlist リソースは YouTube の再生リストを表します。
//作成したリストにアイテムを追加

$request_url ='https://www.googleapis.com/youtube/v3/playlistItems';
$mode = 'POST';
$para['part'] = 'snippet';
$paras['snippet'] = array('playlistId' => $Lid,
 'resourceId' => array('videoId' => $Vid,
'kind' => 'youtube#video'));

$curl_url = $request_url.'?'.http_build_query($para);
$temp = $this->curl($mode,$curl_url,$paras);
//echo '<pre>';
//print_r($temp);

}




public function curl($mode,$url,$paras=null){
//cURL 送信
$Authorization_header = 'Authorization: Bearer  '.$_COOKIE['you_access_token'];
if ($mode == 'GET'){
$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_HEADER, false);
//curl_setopt($ch, CURLINFO_HEADER_OUT, true);

curl_setopt($ch, CURLOPT_HTTPHEADER, array($Authorization_header));
curl_setopt($ch, CURLOPT_URL, $url);
$code = curl_exec($ch);
//$curlgetinfo=curl_getinfo($ch,CURLINFO_HEADER_OUT);
//$curlgetinfo=curl_getinfo($ch);
//echo '<pre>';
//var_dump($curlgetinfo);

curl_close($ch);
}

if ($mode == 'POST'){
$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_HEADER, false);
//curl_setopt($ch, CURLINFO_HEADER_OUT, true);

//curl_setopt($ch, CURLOPT_HTTPHEADER, array($Authorization_header,'Content-Length:','Expect:','Content-Tpye: application/x-www-form-urlencoded'));
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: ','Content-Type:  application/json',$Authorization_header, 'X-JavaScript-User-Agent:  Google APIs Explorer'));
//echo '<pre>';

$data_string = json_encode($paras);

curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_URL, $url);
$code = curl_exec($ch);
$curlgetinfo=curl_getinfo($ch,CURLINFO_HEADER_OUT);
//$curlgetinfo=curl_getinfo($ch);
//echo '<pre>';
//var_dump($curlgetinfo);
curl_close($ch);
}


return json_decode($code,true);






}


}