I'm using an OAuth 2.0 for Web Server Applications example code on remote web server.
$client->authenticate(...) causes
Internal Server Error 500.
No idea why, can you help me please?
Here is the web address: http://imperiousgroup.com/gdrive/
And here is the code:
//index.php
require_once(__DIR__ ."/api/vendor/autoload.php");
session_start();
$client = new Google_Client();
$client->setAuthConfig('client_secrets.json');
$client->addScope(Google_Service_Drive::DRIVE_METADATA_READONLY);
if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
$client->setAccessToken($_SESSION['access_token']);
$drive_service = new Google_Service_Drive($client);
$files_list = $drive_service->files->listFiles(array())->getItems();
echo json_encode($files_list);
} else {
$redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/gdrive/oauth2callback.php';
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
//oauth2callback.php
require_once __DIR__ .'/api/vendor/autoload.php';
session_start();
$client = new Google_Client();
$client->setAuthConfigFile('client_secrets.json');
$client->setRedirectUri('http://' . $_SERVER['HTTP_HOST'] . '/gdrive/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']);
}
Which PHP version are you using?
I had a similar issue as I was using PHP 5.3 while the google api technically requires 5.4+.
If updating it is not an option, I was able to fix my specific issue by using the solution described here:
SO link
The call to curl_reset() is found in /vendor/guzzlehttp/guzzle/src/Handler/CurlFactory.php in the google api client folder.
Related
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 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.
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/
I am using the PHP Google_Client to get the youtube playlist data on my web (a server-side Oauth ,without user check )
but get Error: redirect_uri_mismatch and message
The redirect URI in the request,
http://localhost/youtube/oauth2callback.php,
does not match the ones authorized for the OAuth client.
Visit https://console.developers.google.com/apis/credentialsoauthclient/112609190871896620853?project=756606231401
to update the authorized redirect URIs.
here is the http://localhost/youtube/index.php code
<?php
require_once 'vendor/autoload.php';
session_start();
$client = new Google_Client();
$client->addScope('https://www.googleapis.com/auth/youtube');
$client->setAuthConfigFile('youtube-762b39a4f0b5.json');
if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
$client->setAccessToken($_SESSION['access_token']);
$youtube = new Google_Service_YouTube($client);
$playlists = $youtube->playlists->listPlaylists("snippet,status", array(
'channelId' => 'UCBbOgoYXQdR-LRqrx7hdd6g'
));
echo json_encode($playlists->toSimpleObject());
} else {
$redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/youtube/oauth2callback.php';
var_dump($redirect_uri);
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
here is the http://localhost/youtube/oauth2callback.php
<?php
require_once __DIR__.'/vendor/autoload.php';
session_start();
$client = new Google_Client();
$client->setAuthConfigFile('client_secret.json');
$client->setRedirectUri('http://' . $_SERVER['HTTP_HOST'] .'/youtube/oauth2callback.php');
$client->addScope('https://www.googleapis.com/auth/youtube');
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'] . '/youtube/index.php';
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
Any idea?
Looking at the screenshots, you have created a Service Account. Service Accounts use two-legged OAuth, and therefore do not have any user-consent-and-redirect dance.
A Service Account would not have permissions to videos in your personal Gmail account. Maybe that's OK, maybe it isn't. It depends on your use case which you don't describe in your question. If you need your application to act with the same permissions as your Gmail account, you'll need to use a slightly different technique. (described How do I authorise an app (web or installed) without user intervention? (canonical ?) and https://www.youtube.com/watch?v=hfWe1gPCnzc)