I am trying to learn to use Oauth for a web abb i am running.
I have tried to set up a basic example, which should display a list of files from the user who enters.
How ever, i only get a blank screen.
I've managed to ask it for an authentication - but thats how far i've gotten.
My Codes below:
Index2.php:
<?php
require_once __DIR__ . '/../vendor/autoload.php';
session_start();
$client = new Google_Client();
$client->setAuthConfigFile('oauth-credentials.json');
$client->addScope("https://www.googleapis.com/auth/drive");
if (isset($_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);
echo "success"
} else {
$redirect_uri = '*******';
header('Location: ' . $redirect_uri);
echo "failure";
}
if ($_GET['logout'] == 1) {
unset($_SESSION['access_token']);
}
?>
oauth2callback.php:
<?php
require_once __DIR__ . '/../vendor/autoload.php';
session_start();
$client = new Google_Client();
$client->setAuthConfigFile('oauth-credentials.json');
$client->setRedirectUri('*****');
$client->addScope("https://www.googleapis.com/auth/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->fetchAccessTokenWithAuthCode($_GET['code']);
$redirect_uri = '*****';
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));
}
?>
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));
}
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 google login service for user to login to member area without signup for an account.
After logging successfully, I can only get their email info but not their name? In the code below, I can only echo email, but name is empty? Here is my code, could you please tell what have I done wrong?
<?php
//error_reporting(0);
//#ini_set('display_errors', 0);
require_once '../social_login/social/google-api-php- client/src/Google_Client.php';
require_once '../social_login/social/google-api-php- client/src/contrib/Google_PlusService.php';
require_once '../social_login/social/google-api-php-client/src/contrib/Google_Oauth2Service.php';
include('../social_login/db.php');
include('../configs/dbconnect.php');
ob_start();
session_start();
$client = new Google_Client();
$client->setApplicationName('test');
$client->setClientId($Clientid);
$client->setClientSecret($Client_secret);
$client->setRedirectUri($Redirect_URIs);
$client->setDeveloperKey($apikeys);
$client->setScopes(array(
'https://www.googleapis.com/auth/userinfo.email',
'https://www.googleapis.com/auth/plus.me'
));
$client->setApprovalPrompt('auto');
$client->setAccessType('offline');
$plus = new Google_PlusService($client);
if (isset($_GET['error'])) {
header('Location: /');
exit;
}
if (isset($_GET['code'])) {
$client->authenticate();
$_SESSION['token'] = $client->getAccessToken();
$redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}
if (isset($_SESSION['token'])) {
$client->setAccessToken($_SESSION['token']);
}
if (isset($_REQUEST['logout'])) {
unset($_SESSION['token']);
$client->revokeToken();
}
if ($client->getAccessToken()) {
if($client->isAccessTokenExpired()) {
$authUrl = $client->createAuthUrl();
header('Location: ' . filter_var($authUrl, FILTER_SANITIZE_URL));
}
$oauth2Service = new Google_Oauth2Service($client);
// We're not done yet. Remember to update the cached access token.
// Remember to replace $_SESSION with a real database or memcached.
$_SESSION['token'] = $client->getAccessToken();
$userinfo= $oauth2Service->userinfo->get();
$email = $userinfo['email'];
$username= $userinfo['name'];
echo "My name is: $username<br>";
echo "My email is: $email";
} else {
$authUrl = $client->createAuthUrl();
// print "<a href='$authUrl'>Connect Me!</a>";
}
if (isset($authUrl)) {
// print "<a class='login' href='$authUrl'>Connect Me!</a>";
header('location:' . $authUrl);
}
?>
I am new to Google Oauth 2.I saw google documentation and got the below php library
<?php
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_PlusService.php';
session_start();
$client = new Google_Client();
$client->setApplicationName('Google+ PHP Starter Application');
$client->setClientId('...');
$client->setClientSecret('....');
$client->setRedirectUri('http://photoapp.biz/0/blogger/test.php');
$client->setDeveloperKey('....');
$plus = new Google_PlusService($client);
if (isset($_GET['code'])) {
$client->authenticate();
$_SESSION['token'] = $client->getAccessToken();
$redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}
if (isset($_SESSION['token'])) {
$client->setAccessToken($_SESSION['token']);
}
if ($client->getAccessToken()) {
$activities = $plus->activities->listActivities('me', 'public');
print 'Your Activities: <pre>' . print_r($activities, true) . '</pre>';
$_SESSION['token'] = $client->getAccessToken();
} else {
$authUrl = $client->createAuthUrl();
print "<a href='$authUrl'>Connect Me!</a>";
}
?>
From above code My authentication works fine and i am able to get the Token.But This code allows access for Google Plus .I need To authenticat Blogger with Oauth.Google documentation did not help me.Can some one please guide me .Thanks.