Skip to content

Instantly share code, notes, and snippets.

@luancmaia
Created June 2, 2015 13:18
Show Gist options
  • Save luancmaia/cf3db2dabc6c81b88a25 to your computer and use it in GitHub Desktop.
Save luancmaia/cf3db2dabc6c81b88a25 to your computer and use it in GitHub Desktop.
Youtube API
function auth_authorize_user() {
require_once get_template_directory() . '/google-api-php-client/src/Google/autoload.php'; // or wherever autoload.php is located
$OAUTH2_CLIENT_ID = '563013973974-kadbdid56ilvk6fk0digm27llbub88h3.apps.googleusercontent.com';
$OAUTH2_CLIENT_SECRET = '2Gb9oII1mk-mXi5EOPSO8S6z';
$client = new Google_Client();
$client->setClientId( $OAUTH2_CLIENT_ID );
$client->setClientSecret( $OAUTH2_CLIENT_SECRET );
$client->setScopes( 'https://www.googleapis.com/auth/youtube' );
$redirect = filter_var( home_url(),FILTER_SANITIZE_URL );
$client->setRedirectUri( $redirect );
// Define an object that will be used to make all API requests.
$youtube = new Google_Service_YouTube( $client );
if ( isset( $_GET['code'] ) && !empty( $_GET['code'] ) )
update_option( 'token_youtube', $_GET['code'] );
$token = get_option( 'token_youtube', false );
if ( !$token ) return;
try {
$client->authenticate( $token );
} catch ( Exception $e) {
echo $e;
}
if ( $client->getAccessToken() ) {
} else {
// If the user has not authorized the application, start the OAuth 2.0 flow.
$state = mt_rand();
$client->setState($state);
$authUrl = $client->createAuthUrl();
echo '<a href="'. $authUrl .'">Allow Youtube API</a>';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment