Created
June 17, 2014 17:52
-
-
Save pherrymason/bb0f76e1f33ff2461b19 to your computer and use it in GitHub Desktop.
youtube v3 change banner channel example not working
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Place your google client_secret.json file here |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "Example", | |
"require": { | |
"fkooman/guzzle-bearer-auth-plugin": "dev-master", | |
"fkooman/php-oauth-client": "dev-master" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
ini_set('display_errors',1); | |
error_reporting(-1); | |
session_start(); | |
use \fkooman\OAuth\Client; | |
require 'vendor/autoload.php'; | |
require 'youtube.handler.php'; | |
$youtube = new YoutubeHandler(); | |
if( !isset($_GET['action']) ) | |
{ | |
echo '<link href="css/bootstrap.css" rel="stylesheet"/>'; | |
echo '<form method="get" name="form">'. | |
'<input type="hidden" name="action" value="upload">'. | |
'<button type="submit">Upload</button>'. | |
'</form>'; | |
} | |
elseif( $_GET['action']=='upload' || $_GET['action']=='oauth_callback' ) | |
{ | |
if( $youtube->isAuthenticated()===false ) | |
{ | |
$authenticated = $youtube->authenticate(); | |
} | |
if( $youtube->isAuthenticated() || $authenticated ) | |
{ | |
$youtube->uploadBanner( '18H.jpg' ); | |
} | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use \fkooman\OAuth\Client\GoogleClientConfig as OauthGoogleClientConfig; | |
use \fkooman\OAuth\Client\Api as OauthApi; | |
use \fkooman\OAuth\Client\SessionStorage as OauthSessionStorage; | |
use \fkooman\OAuth\Client\Context as OauthContext; | |
use \fkooman\OAuth\Client\Callback as OauthCallback; | |
use \fkooman\OAuth\Client\AuthorizeException as OauthAuthorizeException; | |
use fkooman\Guzzle\Plugin\BearerAuth\BearerAuth as OauthGuzzleBearer; | |
use \Guzzle\Http\Client as GuzzleClient; | |
use \Guzzle\Http\Exception\ClientErrorResponseException as ClientErrorResponseException; | |
class YoutubeHandler{ | |
protected $api_uri; | |
public function __construct() | |
{ | |
$this->client = null; | |
$this->api_uri = 'https://www.googleapis.com/youtube/v3/'; | |
$this->clientConfig = new OauthGoogleClientConfig( | |
json_decode(file_get_contents("client_secret.json"), true) | |
); | |
$this->oauth_api = new OauthApi("foo", $this->clientConfig, new OauthSessionStorage(), new GuzzleClient()); | |
$this->oauth_context = new OauthContext( 'mycontext', array('https://www.googleapis.com/auth/youtube') ); | |
$this->accessToken = $this->oauth_api->getAccessToken( $this->oauth_context ); | |
if( $this->accessToken===false ) | |
{ | |
} | |
else | |
{ | |
// OK!!! | |
} | |
} | |
public function isAuthenticated() | |
{ | |
return $this->accessToken!==false; | |
} | |
public function authenticate( $callback_url=null ) | |
{ | |
if( !isset($_GET['action']) || $_GET['action']!='oauth_callback' ) | |
{ | |
header( 'Location: ' . $this->oauth_api->getAuthorizeUri( $this->oauth_context ) ); | |
exit(); | |
} | |
else | |
{ | |
// Come from Google... | |
try | |
{ | |
$this->oauth_callback = new OauthCallback('foo', $this->clientConfig, new OauthSessionStorage(), new GuzzleClient() ); | |
$this->oauth_callback->handleCallback($_GET); | |
return true; | |
//header("HTTP/1.1 302 Found"); | |
//header("Location: http://localhost:8888/digraf/youtube/index.php"); | |
//exit; | |
} | |
catch( OauthAuthorizeException $e ) | |
{ | |
// this exception is thrown by Callback when the OAuth server returns a | |
// specific error message for the client, e.g.: the user did not authorize | |
// the request | |
die(sprintf("ERROR: %s, DESCRIPTION: %s", $e->getMessage(), $e->getDescription())); | |
} | |
catch( \Exception $e ) | |
{ | |
// other error, these should never occur in the normal flow | |
die(sprintf("ERROR: %s", $e->getMessage())); | |
} | |
} | |
} | |
public function call( $method, $resource, $headers=null, $body=null ) | |
{ | |
try | |
{ | |
if( $this->client===null ) | |
{ | |
$this->client = new GuzzleClient(); | |
$bearerAuth = new OauthGuzzleBearer( $this->accessToken->getAccessToken() ); | |
$this->client->addSubscriber($bearerAuth); | |
} | |
$request = $this->client->createRequest( $method, $resource, $headers, $body ); | |
$response = $request->send(); | |
} | |
catch( \fkooman\Guzzle\Plugin\BearerAuth\Exception\BearerErrorResponseException $e ) | |
{ | |
if ("invalid_token" === $e->getBearerReason()) | |
{ | |
// the token we used was invalid, possibly revoked, we throw it away | |
$oauth_api->deleteAccessToken($context); | |
$oauth_api->deleteRefreshToken($context); | |
/* no valid access token available, go to authorization server */ | |
header("HTTP/1.1 302 Found"); | |
header("Location: " . $oauth_api->getAuthorizeUri($context)); | |
exit; | |
} | |
throw $e; | |
} | |
catch (\Exception $e) | |
{ | |
die(sprintf('ERROR: %s', $e->getMessage())); | |
} | |
return $response; | |
} | |
public function uploadBanner( $banner_path ) | |
{ | |
$body = ''; | |
$handle = fopen( $banner_path, "rb" ); | |
$chunkSizeBytes = 1 * 1024 * 1024; | |
while( !feof($handle) ) | |
{ | |
$body.= fread($handle,$chunkSizeBytes); | |
} | |
$r = $this->call( | |
'POST', | |
'https://www.googleapis.com/upload/youtube/v3/channelBanners/insert', | |
array('content-type'=>'image/jpeg'), // headers | |
$body | |
); | |
if( $r->getStatusCode()==200 ) | |
{ | |
$upload_response = json_decode( $r->getBody(), true ); | |
echo '<pre>'.print_r($upload_response,true).'</pre>'; | |
$r = $this->call('GET', $this->api_uri.'channels?part=brandingSettings&mine=true' ); | |
if( $r->getStatusCode()===200 ) | |
{ | |
$response = json_decode( $r->getBody(), true ); | |
$params = array( | |
// "kind"=> $response['items'][0]['kind'], | |
// "etag"=> $response['items'][0]['etag'], | |
"id" => $response['items'][0]['id'], | |
'brandingSettings' => array( | |
'bannerExternalUrl' => $upload_response['url'] | |
) | |
); | |
try | |
{ | |
$request = $this->client->put( $this->api_uri . 'channels?part=brandingSettings&mine=true' ); | |
$request->setBody( json_encode($params), 'application/json' ); | |
$res = $request->send(); | |
echo $res->getStatusCode().'<br>'; | |
echo $res; | |
} | |
catch( ClientErrorResponseException $e ) | |
{ | |
echo $e->getResponse()->getBody().'<br>'; | |
echo $e->getMessage().'<br>'; | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment