I'm checking Google Drive API docs because I want to use it in the browser and not in command line, so, according to examples, my code is like this:
index.php
<?php
require_once __DIR__.'/vendor/autoload.php';
session_start();
$client = new Google_Client();
$client->setAuthConfig('11*****.apps.googleusercontent.com_client_secret.json');
$client->addScope(Google_Service_Drive::DRIVE);
if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
$client->setAccessToken($_SESSION['access_token']);
$drive = new Google_Service_Drive($client);
$files = $drive->files->listFiles(array())->getFiles();
echo json_encode($files);
} else {
$redirect_uri = 'https://' . $_SERVER['HTTP_HOST'] . '/drive/oauth2callback.php';
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
?>
oauth2callback.php
<?php
require_once __DIR__.'/vendor/autoload.php';
session_start();
$client = new Google_Client();
$client->setAuthConfigFile('11*****.apps.googleusercontent.com_client_secret.json');
$client->setRedirectUri('https://' . $_SERVER['HTTP_HOST'] . '/drive/oauth2callback.php');
$client->addScope(Google_Service_Drive::DRIVE);
if (! isset($_GET['code'])) {
$auth_url = $client->createAuthUrl();
header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
} else {
$client->authenticate($_GET['code']);
$_SESSION['access_token'] = $client->getAccessToken();
$redirect_uri = 'https://' . $_SERVER['HTTP_HOST'] . '/drive/';
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
?>
But when I execute index.php in the browser and after login with my google account I receive this message:
How can I avoid this message?
You need to go through verification before you launch a user-facing app. You can continue to build and test your application while waiting to complete verification. When your app is successfully verified, the unverified app screen will be removed from your client.
You can follow the steps of verification for apps here.
Related
I have successfully managed to get the contacts on CLI as the documentation was perfect. I wanted to implement the same thing for the web browser but it is not working and repeating the google auth/consent screens again and again. I couldn't find any clue from the documentation because there was no proper example given to implement people API for the web application. The code which tried to develop at my own is given below
index.php
<?php
require_once __DIR__ . '/vendor/autoload.php';
use Google\Client;
use Google\Service\PeopleService;
session_start();
$client = new Google\Client();
$client->setAuthConfig('credentials.json');
$client->addScope(Google\Service\PeopleService::CONTACTS);
if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
$client->setAccessToken($_SESSION['access_token']);
$service = new PeopleService($client);
$optParams = array(
'pageSize' => 10,
'personFields' => 'names,emailAddresses',
);
$results = $service->people_connections->listPeopleConnections('people/me', $optParams);
echo json_encode($results);
} else {
$redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/gc-web/oauth2callback.php';
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
oath2callback.php
<?php
require_once __DIR__.'/vendor/autoload.php';
session_start();
$client = new Google\Client();
$client->setAuthConfigFile('credentials.json');
$client->setRedirectUri('http://' . $_SERVER['HTTP_HOST'] . '/gc-web/oauth2callback.php');
$client->addScope(Google\Service\Drive::DRIVE_METADATA_READONLY);
if (! isset($_GET['code'])) {
$auth_url = $client->createAuthUrl();
header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
} else {
$client->authenticate($_GET['code']);
$_SESSION['access_token'] = $client->getAccessToken();
$redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/gc-web';
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
I am stucked 3 days ago with Google Calendar API, I am new in Google API.
First of all I want to get events from users calendar.
I went through on the install process a few times, but my code always stop at fetchAccessTokenWithAuthCode function.
Probably I miss something really obvious, but I can't figure out what is wrong.
In web root I created a new folder for testing with 2 basic file from Google install site.
My php files:
index.php:
<?php
require_once 'path/googleapi/vendor/autoload.php';
session_start();
$client = new Google_Client();
$client->setAuthConfig('path/googleapi/credentials.json');
$client->addScope(Google_Service_Calendar::CALENDAR_READONLY);
$client->setRedirectUri('https://site/google/oauth2callback.php');
$client->setAccessType('offline');
$client->setPrompt("consent");
$client->setIncludeGrantedScopes(true);
$auth_url = $client->createAuthUrl();
var_dump($auth_url);
if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
var_dump($_SESSION);
} else {
header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
}
?>
oauth2callback.php:
<?php
require_once 'path/googleapi/vendor/autoload.php';
session_start();
$client = new Google_Client();
$client->setAuthConfig('path/googleapi/credentials.json');
$client->setRedirectUri('https://site/google/oauth2callback.php');
$client->addScope(Google_Service_Calendar::CALENDAR_READONLY);
$client->setAccessType("offline");
if (! isset($_GET['code'])) {
$auth_url = $client->createAuthUrl();
header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
} else {
var_dump($_GET['code']);
$token = $client->fetchAccessTokenWithAuthCode($_GET['code']);
echo "<br>token:<br>";
var_dump($token);
$_SESSION['access_token'] = $client->getAccessToken();
$redirect_uri = 'https://site/google/';
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
?>
I got redirected to Google's site, once I grant permission I am redirected to the oauth2callback.php
In oauth2callback.php I can see the var_dump output for the code, but no further output, which means no response from fetchAccessTokenWithAuthCode function.
Any help is appreciated
I'm trying to refresh token on my PHP script but It's not working yet. I'm using Google Drive API with PHP, my script works great in browser until token expires, checking in other sites I changed the code in this way:
index.php
<?php
require_once 'vendor/autoload.php';
if (!file_exists("client_id.json")) exit("Client secret file not found");
$client = new Google_Client();
$client->setAuthConfig('client_id.json');
$client->addScope(Google_Service_Drive::DRIVE);
if (file_exists("credentials.json")) {
$access_token = (file_get_contents("credentials.json"));
$client->setAccessToken($access_token);
//Refresh the token if it's expired.
if ($client->isAccessTokenExpired()) {
$refreshTokenSaved = $client->getRefreshToken();
$client->fetchAccessTokenWithRefreshToken($refreshTokenSaved);
$accessTokenUpdated = $client->getAccessToken();
$accessTokenUpdated['refresh_token'] = $refreshTokenSaved;
file_put_contents("credentials.json", json_encode($accessTokenUpdated));
//I also tried in this way but it's also not working:
//$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
//file_put_contents("credentials.json", json_encode($client->getAccessToken()));
}
$drive_service = new Google_Service_Drive($client);
$files_list = $drive_service->files->listFiles(array())->getFiles(); //http://stackoverflow.com/questions/37975479/call-to-undefined-method-google-service-drive-filelistgetitems
echo json_encode($files_list);
} else {
$redirect_uri = 'https://' . $_SERVER['HTTP_HOST'] . '/drive/oauth2callback.php';
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
?>
oauth2callback.php
<?php
require_once 'vendor/autoload.php';
$client = new Google_Client();
$client->setAuthConfigFile('client_id.json');
$client->setRedirectUri('https://' . $_SERVER['HTTP_HOST'] . '/drive/oauth2callback.php');
$client->addScope(Google_Service_Drive::DRIVE); //::DRIVE_METADATA_READONLY
if (! isset($_GET['code'])) {
$auth_url = $client->createAuthUrl();
header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
} else {
$client->authenticate($_GET['code']);
$access_token = $client->getAccessToken();
file_put_contents("credentials.json", json_encode($access_token));
$redirect_uri = 'https://' . $_SERVER['HTTP_HOST'] . '/';
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
?>
But I still receive the message in logs:
PHP Fatal error: Uncaught exception 'LogicException' with message 'refresh token must be passed in or set as part of setAccessToken' in /var/www/drive/vendor/google/apiclient/src/Google/Client.php:266\nStack trace:\n#0 /var/www/drive/index.php(14): Google_Client->fetchAccessTokenWithRefreshToken(NULL)\n#1 {main}\n thrown in /var/www/drive/vendor/google/apiclient/src/Google/Client.php on line 266
I have to delete credentials.json and generate a new one executing index.php and being redirected to oauth2callback.php each hour, but that's not the idea.
How can I fix it?
I'd like to receive your help.
Finally the token refresh works, I had to add this line $client->setAccessType("offline"); in oauth2callback.php, also delete and generate a new client_id.json to avoid errors.
<?php
require_once 'vendor/autoload.php';
$client = new Google_Client();
$client->setAuthConfigFile('new_client_id.json');
$client->setRedirectUri('https://' . $_SERVER['HTTP_HOST'] . '/drive/resp.php');
$client->addScope(Google_Service_Drive::DRIVE); //::DRIVE_METADATA_READONLY
$client->setAccessType("offline");
if (! isset($_GET['code'])) {
$auth_url = $client->createAuthUrl();
header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
} else {
$client->authenticate($_GET['code']);
$access_token = $client->getAccessToken();
file_put_contents("credentials.json", json_encode($access_token));
$redirect_uri = 'https://' . $_SERVER['HTTP_HOST'] . '/drive';
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
?>
Currently, I am working on We application in PHP, which will get Google Drive files and perform some operation on it.
I have created Google Project with name My Project, with credentials which allow me to download the credential json file. I have Used following two files as mentioned in the google documentation
https://developers.google.com/api-client-library/php/auth/web-app
index.php
<?php
require 'vendor/autoload.php';
session_start();
$client = new Google_Client();
$client->setAuthConfig('client_secret_new.json');
//$client->setAccessType("online"); // offline access
//$client->setIncludeGrantedScopes(true); // incremental auth
$client->addScope(Google_Service_Drive::DRIVE);
$client->setRedirectUri('http://' . $_SERVER['HTTP_HOST'] . '/google-
drive/oauth2callback.php');
if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
$client->setAccessToken($_SESSION['access_token']);
$drive = new Google_Service_Drive($client);
$optParams = array(
'fields' => 'nextPageToken, files(id, name)'
);
$files = $drive->files->listFiles($optParams);
if (count($files->getFiles()) == 0) {
print "No files found.\n";
} else {
print "Files:\n";
foreach ($files->getFiles() as $file) {
echo $file->getName()." ".$file->getId()."<br/>";
}
}
} else {
$redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/google-
drive/oauth2callback.php';
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
?>
oauth2callback.php
<?php
require_once 'vendor/autoload.php';
session_start();
$client = new Google_Client();
$client->setAuthConfigFile('client_secret_new.json');
$client->setRedirectUri('http://' . $_SERVER['HTTP_HOST'] . '/google-
drive/oauth2callback.php');
$client->addScope(Google_Service_Drive::DRIVE);
if (!isset($_GET['code'])) {
$auth_url = $client->createAuthUrl();
header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
} else {
$client->authenticate($_GET['code']);
$_SESSION['access_token'] = $client->getAccessToken();
$redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/google-drive/';
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
?>
Once I run index.php file, it as me to login to google account and ask for required permission, once i access the permission it moves to redirect url which is oauth2callback.php file with code in url, but oauth2callback.php once it authenticate code it gives 500 error in line $client->authenticate($_GET['code']);
please give me solution for the same.
I'm trying to set up Management API using this Google tutorial: https://developers.google.com/analytics/devguides/config/mgmt/v3/quickstart/web-php
I've downloaded index.php and oauth2callback.php files and edited URL to fixed addresses (I am using XAMPP, not terminal, and my files are in a subfolder).
I'm stuck in infinite redirects for Google authentication. It never shows me the output or any error.
index.php:
<?php
require_once __DIR__ . '/vendor/autoload.php';
session_start();
$client = new Google_Client();
$client->setAuthConfig(__DIR__ . '/client_secrets.json');
$client->addScope(Google_Service_Analytics::ANALYTICS_READONLY);
if ($_SESSION['access_token']) {
$client->setAccessToken($_SESSION['access_token']);
$analytics = new Google_Service_Analytics($client);
$profile = getFirstProfileId($analytics);
$results = getResults($analytics, $profile);
//printResults($results);
echo $results;
} else {
$redirect_uri = 'http://localhost/zen-analytics/management-api/oauth2callback.php';
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
...
oauth2callback.php:
<?php
require_once __DIR__ . '/vendor/autoload.php';
session_start();
$client = new Google_Client();
$client->setAuthConfig(__DIR__ . '/client_secrets.json');
$client->setRedirectUri = 'http://localhost/zen-analytics/management-api/oauth2callback.php';
$client->addScope(Google_Service_Analytics::ANALYTICS_READONLY);
if (! isset($_GET['code'])) {
$auth_url = $client->createAuthUrl();
header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL));
} else {
$client->authenticate($_GET['code']);
$_SESSION['access_token'] = $client->getAccessToken();
$redirect_uri = 'http://localhost/zen-analytics/management-api/';
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
?>
Web application authorized redirect URI is set to:
http://localhost/zen-analytics/management-api/