Youtube API v3 without the need of End-User Authentication - php

Im trying to create a Youtube upload script that does not require the end user to login in order to upload a video to my channel. The issue is I can gain the access token but it expires after an hour of being inactive.
Also I am unsure where you can get the "refresh_token" from.
So essentially I wish to have this Google API v3 script not require any client side authentication in order to upload video's.
From what I gather from the API documentation at google if I had the "refresh_token" I could renew the token without the need of user interaction required.
Please see below my existing code on the matter.
<?php
$key = json_decode(file_get_contents('the_key.txt'));
require_once 'Client.php';
require_once 'Service/YouTube.php';
session_start();
$OAUTH2_CLIENT_ID = 'YOUR_CLIENT_ID.apps.googleusercontent.com';
$OAUTH2_CLIENT_SECRET = 'YOUR_SECRET_KEY';
$REDIRECT = 'http://example.com/test.php';//Must be exact as setup on google API console
$APPNAME = "Test upload app";
$client = new Google_Client();
$client->setClientId($OAUTH2_CLIENT_ID);
$client->setClientSecret($OAUTH2_CLIENT_SECRET);
$client->setScopes('https://www.googleapis.com/auth/youtube');
$client->setRedirectUri($REDIRECT);
$client->setApplicationName($APPNAME);
$client->setAccessType('offline');
// Define an object that will be used to make all API requests.
$youtube = new Google_Service_YouTube($client);
if (isset($_GET['code'])) {
if (strval($_SESSION['state']) !== strval($_GET['state'])) {
die('The session state did not match.');
}
$client->authenticate($_GET['code']);
$_SESSION['token'] = $client->getAccessToken();
}
if (isset($_SESSION['token'])) {
//if session current save to file updates token key
$client->setAccessToken($_SESSION['token']);
echo '<code>' . $_SESSION['token'] . '</code><br>';
file_put_contents('the_key.txt', $_SESSION['token']);
}
// Check to ensure that the access token was successfully acquired.
if ($client->getAccessToken()) {
if($client->isAccessTokenExpired()) {
echo "Token Expired<br>Attempt Refresh: ";
$client->refreshToken($key->access_token);
//refresh token, now need to update it in session
$_SESSION['access_token']= $client->getAccessToken();
}
$_SESSION['token'] = $client->getAccessToken();
///////////////////////////////////////////////////////////////////////
//Run Upload script here from google Youtube API v3
//https://developers.google.com/youtube/v3/code_samples/php#resumable_uploads
///////////////////////////////////////////////////////////////////////
} else {
$state = mt_rand();
$client->setState($state);
$_SESSION['state'] = $state;
$authUrl = $client->createAuthUrl();
$htmlBody = '<h3>Authorization Required</h3><p>You need to authorise access before proceeding.<p>';
}
?>
<!doctype html>
<html>
<head>
<title>My Uploads</title>
</head>
<body>
<?php echo $htmlBody?>
</body>
</html>
If someone can look over this code and tell me what I'm missing would be great.

I finally figured it out!!!
Thanks to Burcu Dogan for the solution:
Automatically refresh token using google drive api with php script
It turns out that the refresh_token will only be returned on the first $client->authenticate(), for that account you login with and you need to permanently store it remove the need for the user authentication.
As I had already authenticated desired user I was unable to regenerate the refresh_token with this project so I had to delete the project and recreate it.
Re-Authenticate the user and the refresh_token appeared! I then stored this token safe and have not had any issues since.
If you do not wish to delete your project and start again you can also go to oAuth Playground and aquire your refresh_token that way.
YouTube run through
Hope this help solve a lot of peoples problems online.

Related

Composer and Google Auth JSON Key

I need a sample code that shows me how I use the .json file I got from Google to authenticate that I am the owner of the storage Bucket.
Basically users upload their profile picture and I want to store it using Google Cloud, but every time I run the code below it says
Could not load the default credentials
I have gone to the URL it gives and I get a 404 error.
My index file has this in it
<?php
require 'vendor/autoload.php';
require 'sys/v1/core.php';
require 'sys/v1/google_cloud_storeage_upload.php';
$client = new Google_Client();
$client->setAuthConfig('/home/SITE/FOLDER/KEY-GOOGLE.json');
// $service implements the client interface, has to be set before auth call
$service = new Google_AnalyticsService($client);
if (isset($_GET['logout'])) { // logout: destroy token
unset($_SESSION['token']);
die('Logged out.');
}
if (isset($_GET['code'])) { // we received the positive auth callback, get the token and store it in session
$client->authenticate();
$_SESSION['token'] = $client->getAccessToken();
}
if (isset($_SESSION['token'])) { // extract token from session and configure client
$token = $_SESSION['token'];
$client->setAccessToken($token);
}
if (!$client->getAccessToken()) { // auth call to google
$authUrl = $client->createAuthUrl();
header("Location: ".$authUrl);
die;
}
?>

Google API :OAuth 2.0 authenticate return "invalid_grand" "Bad request"

I am using code examples of YouTube Api v3
I turned on YouTube Data API v3.
I create Oauth Client in the developer console
I added my url ($app->url('google.auth.test')) to allowed redirect URI
I copied the client id and secret
Then I used example code
$client = new Google_Client();
$client->setClientId($OAUTH2_CLIENT_ID);
$client->setClientSecret($OAUTH2_CLIENT_SECRET);
$client->setApprovalPrompt('force');//read some topics - it could help
$client->setAccessType('offline');//read some topics - it could help
$client->setScopes('https://www.googleapis.com/auth/youtube');
$redirect = filter_var($app->url('google.auth.test'),
FILTER_SANITIZE_URL);//uri of this page
$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'])) {
if (strval($_SESSION['state']) !== strval($_GET['state'])) {
die('The session state did not match.');
}
$cred = $client->authenticate($_GET['code']);
dump($_GET['code']);
dump($cred);
dump($client->getAccessToken());
exit;
$_SESSION['token'] = $client->getAccessToken();
header('Location: ' . $redirect);
}
if (isset($_SESSION['token'])) {
$client->setAccessToken($_SESSION['token']);
}
// Check to ensure that the access token was successfully acquired.
if ($client->getAccessToken()) {
// This code creates a new, private playlist in the authorized user's
//some code
$_SESSION['token'] = $client->getAccessToken();
} else {
// If the user hasn't authorized the app, initiate the OAuth flow
$state = mt_rand();
$client->setState($state);
$_SESSION['state'] = $state;
$authUrl = $client->createAuthUrl();
$htmlBody = "
<h3>Authorization Required</h3>
<p>You need to authorize access before proceeding.<p>
";
return $htmlBody;
}
After I go to to $app-url('google.auth.test')
I am being redirected to
Authorization Required
You need to authorize access before proceeding.
It is OK.
After clicking - redirect to google auth it is OK.
Accept google auth and redirect back to $app-url('google.auth.test')
I get code and my status in get request, but $client->authenticate($_GET['code']); return me error
Dump info:
I think I did not find some advanced google settings.
Thanks for answers!

How to work with Google api on multiple pages?

I am having trouble working with Google API on multiple pages. Example from Google works fine but all the code is on one page.
I have two pages. First page, where user click on login button and second page, where I use google api to pull user information.
First Page:
<?php
########## Google Settings.. Client ID, Client Secret from https://cloud.google.com/console #############
$google_client_id = 'myid';
$google_client_secret = 'mysecret';
$google_redirect_url = 'http://www.myWebsite.com/secondPage.php'; //path to your script
$google_developer_key = 'mydeveloperkey';
//include google api files
require_once '../includes/Google/autoload.php';
//start session
session_start();
$client = new Google_Client();
$client->setClientId($google_client_id);
$client->setClientSecret($google_client_secret);
$client->setRedirectUri($google_redirect_url);
$client->addScope("https://www.googleapis.com/auth/drive");
$client->addScope("https://www.googleapis.com/auth/calendar");
$client->addScope("https://www.googleapis.com/auth/gmail.compose");
$client->addScope("https://www.googleapis.com/auth/plus.me");
$drive_service = new Google_Service_Drive($client);
$calendar_service = new Google_Service_Calendar($client);
$gmail_service = new Google_Service_Gmail($client);
/************************************************
If we're logging out we just need to clear our
local access token in this case
************************************************/
if (isset($_REQUEST['logout'])) {
unset($_SESSION['access_token']);
}
// Authenticating the aunthentication URL. and starting session
if (isset($_GET['code'])) {
$client->authenticate($_GET['code']);
$_SESSION['access_token'] = $client->getAccessToken();
$redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['secondPage.php'];
header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}
/************************************************
If we have an access token, we can make redirect to secondPage.php, else we generate an authentication URL.
************************************************/
if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
header("Location: http://www.myWebsite.com/secondPage.php");
die();
} else {
$authUrl = $client->createAuthUrl();
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
</head>
<body>
<img src="images/google-login-button.png" alt="Click to login">
</body>
</html>
secondPage.php:
<?php ob_start() ?>
<?php
//include google api files
require_once '../includes/Google/autoload.php';
//start session
session_start();
$client = new Google_Client();
/************************************************
If we have an access token, we can make
requests, else we redirect to firstPage.php.
************************************************/
if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
$client->setAccessToken($_SESSION['access_token']);
} else {
header("Location: http://www.myWebsite.com/firstPage.php");
die();
}
// Rest is HTML
For some reason if statement on secondPage.php is resulting into false and else statement is redirecting it back to firstPage.php.
I am very new to programming altogether, I am pretty sure I am doing something which does not make sense. Let me know if I should add more information. When answering question please try to cover following questions:
Do I have to create separate Google_client object on every page.
Can I create Google_client object by setting access_token from session variable.
How should I divide the code so that on the firstPage.php, only have a login button and all other pages can use access_token to use google services.
there will be other pages where I will be using googleapi other than firstPage.php and secondPage.php.
1. Redirect properly
I do have some doubts about that line:
$redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['secondPage.php'];
It's because I'm pretty sure, that you have nothing set behind $_SERVER['secondPage.php'] variable. You should fix this to redirect to secondPage.php, and afterwards no more code in first page is required.
2. Set up accont in secondPage.php
In your first script you have such lines:
$client = new Google_Client();
$client->setClientId($google_client_id);
$client->setClientSecret($google_client_secret);
$client->setRedirectUri($google_redirect_url);
And you have ommited them in secondPage.php. That's probably reason why your second script does not work, because your script does not know with what account it is working. You have to configure you script all again there in second script, same way you did in first one. Also for now I would cut-off ob_start(), it may harden debugging.
I prefer you to read that example on google repository once again, carefully. It's pretty self explanatory and it just requires you to read it again and again, as long as you will have that strange little feeling... I can assure you, that you can do it easy and clean in three files: first one for ::authenticate() and set $_SESSION, second for the ::setAccessToken() and all rest, and third one with all $client class set up used by previous both.

Google API Oauth php permanent access

I am using the google Calendar API. This is what I want, once you give the app the permission, I can always use the app, without the need of giving access everyday. I keep hearing that I need to save the access token or use the refresh token to do what I want to do.. Here is the thing, how do you do it? How does the code look like? I've tried saving the token in a cookie, but after an hour, the access token has expired. How do I keep the user logged in?
PS: Please give me code examples with explanations.
Here is my code (using CakePHP):
$client = new Google_Client();
$client->setApplicationName("Wanda3.0 Agenda");
$cal = new Google_CalendarService($client);
if (isset($_GET['code'])) {
$client->authenticate($_GET['code']);
$_SESSION['token'] = $client->getAccessToken();
header('Location: http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']);
}
if (isset($_SESSION['token'])) { $client->setAccessToken($_SESSION['token']); }
if ($client->getAccessToken()) {
/************* Code entry *************/
}else{
/************* Not connected to google calendar code *************/
$authUrl = $client->createAuthUrl();
$returnArr = array('status' => 'false', 'message' => "<a class='login' href='$authUrl'>Connect Me!</a>");
return $returnArr;
}
Ok, after waiting for a few days, the suggestion from Terry Seidler(comments below) made it all happen! Here is my piece of code on how to automaticly refresh the access token without autenticating each time using cookies.
(NOTICE: It's safer to save the refresh token in your database)
This is the magic (using cookies):
$client = new Google_Client();
$client->setApplicationName("Wanda3.0 Agenda");
$cal = new Google_CalendarService($client);
$client->setAccessType('offline');
if (isset($_GET['code'])) {
$client->authenticate($_GET['code']);
$_SESSION['token'] = $client->getAccessToken();
header('Location: http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']);
}
//Where the magic happends
if (isset($_SESSION['token'])) {
//Set the new access token after authentication
$client->setAccessToken($_SESSION['token']);
//json decode the session token and save it in a variable as object
$sessionToken = json_decode($_SESSION['token']);
//Save the refresh token (object->refresh_token) into a cookie called 'token' and make last for 1 month
$this->Cookie->write('token', $sessionToken->refresh_token, false, '1 month');
}
//Each time you need the access token, check if there is something saved in the cookie.
//If $cookie is empty, you are requested to get a new acces and refresh token by authenticating.
//If $cookie is not empty, you will tell the client to refresh the token for further use,
// hence get a new acces token with the help of the refresh token without authenticating..
$cookie = $this->Cookie->read('token');
if(!empty($cookie)){
$client->refreshToken($this->Cookie->read('token'));
}
And thats it! IF you have any questions, feel free to leave a comment below and I will answer the best I can. Goodluck and Cheers!
Instead of using cookies and sessions you should save it in the database and use it.
A similar implementation for drupal site is at Google OAuth2 sandbox at http://drupal.org/sandbox/sadashiv/1857254 This module allows you to handle the authentication from admin interface of drupal. You can then use the fetched access token from google and then use the api function of google_oauth2_account_load or google_oauth2_client_get to get the Google_Client and carry your api call.
Much the same as hope industries but after testing I only wanted to refresh the token when I had to. Full source just change the keys etc at the top:
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_CalendarService.php';
session_start();
$client = new Google_Client();
$client->setApplicationName("Google Calendar PHP Starter Application");
// Visit https://code.google.com/apis/console?api=calendar to generate your
// client id, client secret, and to register your redirect uri.
$client->setClientId('your_id');
$client->setClientSecret('your_secret');
$client->setRedirectUri("http://localhost/your_redirect.php");
$client->setDeveloperKey('your_key');
$cal = new Google_CalendarService($client);
if (isset($_GET['code'])) {
$client->authenticate($_GET['code']);
$_SESSION['token'] = $client->getAccessToken();
header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'] . $query_string);
}
if (isset($_SESSION['token'])) {
$client->setAccessToken($_SESSION['token']);//update token
//json decode the session token and save it in a variable as object
$sessionToken = json_decode($_SESSION['token']);
//Save the refresh token (object->refresh_token) into a cookie called 'token' and make last for 1 month
if (isset($sessionToken->refresh_token)) { //refresh token is only set after a proper authorisation
$number_of_days = 30 ;
$date_of_expiry = time() + 60 * 60 * 24 * $number_of_days ;
setcookie('token', $sessionToken->refresh_token, $date_of_expiry);
}
}
else if (isset($_COOKIE["token"])) {//if we don't have a session we will grab it from the cookie
$client->refreshToken($_COOKIE["token"]);//update token
}
if ($client->getAccessToken()) {
$calList = $cal->calendarList->listCalendarList();
print "<h1>Calendar List</h1><pre>" . print_r($calList, true) . "</pre>";
$_SESSION['token'] = $client->getAccessToken();
} else {
$authUrl = $client->createAuthUrl();
print "<a class='login' href='$authUrl'>Select a calendar!</a>";
}

Google+ get userID using e-mail?

I want to know if it is possible to get the "userId" value using the Google+ api, in order to display a link to some specific user profile. For better explanation, I have a PHP application and there is a panel where I can view the existing users in my database. So, when detailing a specific user, I want to show a Google+ badge which will link to that user's Google+ profile. I don't have the "userId", but still have access to his email address. Any suggestions?
There is no endpoint that you can hit to convert an email address into a Google+ userId.
However, you can use the REST API to determine the Google+ userId of the currently logged in user. This will require the user to grant you access to their identity on Google+ via OAuth. To do this use the people get endpoint with me as the userId. You can try this out on the API Explorer to observe what the user experience with the OAuth dialog will be like.
Here's some code lifted from the PHP starter project that illustrates everything you need to fetch the userId of the current user:
if (ini_get('register_globals') === "1") {
die("register_globals must be turned off before using the starter application");
}
require_once 'google-api-php-client/src/apiClient.php';
require_once 'google-api-php-client/src/contrib/apiPlusService.php';
session_start();
$client = new apiClient();
$client->setApplicationName("Google+ PHP Starter Application");
// Visit https://code.google.com/apis/console to generate your
// oauth2_client_id, oauth2_client_secret, and to register your oauth2_redirect_uri.
$client->setClientId('insert_your_oauth2_client_id');
$client->setClientSecret('insert_your_oauth2_client_secret');
$client->setRedirectUri('insert_your_oauth2_redirect_uri');
$client->setDeveloperKey('insert_your_developer_key');
$client->setScopes(array('https://www.googleapis.com/auth/plus.me'));
$plus = new apiPlusService($client);
if (isset($_REQUEST['logout'])) {
unset($_SESSION['access_token']);
}
if (isset($_GET['code'])) {
$client->authenticate();
$_SESSION['access_token'] = $client->getAccessToken();
$redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}
if (isset($_SESSION['access_token'])) {
$client->setAccessToken($_SESSION['access_token']);
}
if ($client->getAccessToken()) {
$me = $plus->people->get('me');
// Do stuff with the id
echo $me['id'];
// The access token may have been updated lazily.
$_SESSION['access_token'] = $client->getAccessToken();
} else {
$authUrl = $client->createAuthUrl();
}

Categories