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.
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 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);
}
?>
Just trying to use the Google Client API to pull traffic data for my site and thought I would try the simple.php file on https://code.google.com/p/google-api-php-client/source/browse/trunk/examples/contacts/simple.php
I've edited it appropriately for my credentials and I can get it to go from the "Connect Me" link to the request permission page, but after I accept I end up at "This webpage is not available". I'm wondering if there might be an issue with needing the token refreshed?
Any thoughts are greatly appreciated. Thanks!
<?php
require_once 'src/Google_Client.php';
require_once 'src/contrib/Google_AnalyticsService.php';
session_start();
$client = new Google_Client();
$client->setApplicationName("Google Analytics PHP Starter Application");
// Visit https://code.google.com/apis/console?api=analytics to generate your
// client id, client secret, and to register your redirect uri.
$client->setClientId('MY_CLIENT_ID');
$client->setClientSecret('MY_CLIENT_SECRET');
$client->setDeveloperKey('MY_DEVELOPER_KEY');
$client->setRedirectUri('http://localhost/google-analytics/simple.php');
$service = new Google_AnalyticsService($client);
if (isset($_GET['logout'])) {
unset($_SESSION['token']);
}
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()) {
$props = $service->management_webproperties->listManagementWebproperties("~all");
print "<h1>Web Properties</h1><pre>" . print_r($props, true) . "</pre>";
$accounts = $service->management_accounts->listManagementAccounts();
print "<h1>Accounts</h1><pre>" . print_r($accounts, true) . "</pre>";
$segments = $service->management_segments->listManagementSegments();
print "<h1>Segments</h1><pre>" . print_r($segments, true) . "</pre>";
$goals = $service->management_goals->listManagementGoals("~all", "~all", "~all");
print "<h1>Segments</h1><pre>" . print_r($goals, true) . "</pre>";
$_SESSION['token'] = $client->getAccessToken();
} else {
$authUrl = $client->createAuthUrl();
print "<a class='login' href='$authUrl'>Connect Me!</a>";
}
I am using Google API PHP Client. And every time i try to login I am forced to grant permissions to the apps.
Below is my code. I am basically accessing Google API for Analytics
require_once 'lib/apiClient.php';
require_once 'lib/contrib/apiAnalyticsService.php';
session_start();
$client = new apiClient();
$client->setApplicationName("Google Analytics");
$client->setClientId('7xxxx');
$client->setClientSecret('xxxx');
$client->setRedirectUri('xxxx');
$client->setDeveloperKey('xxxx');
$analytics = new apiAnalyticsService($client);
if (isset($_GET['logout'])) {
unset($_SESSION['token']);
}
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()) {
$profileId = getProfileIds($analytics);
echo "<select>";
foreach ($profileId as $profiles) {
echo "<option value=\"" . $profiles['profileId'] . "\">" . $profiles['name'] .
"</option>";
}
echo "</select>";
} else {
$authUrl = $client->createAuthUrl();
print "<a class='login' href='$authUrl'>Connect Me!</a>";
}
What option can we pass to createAuthUrl() ??
By Setting
$client->setApprovalPrompt('auto');
It will automatically redirect if the account has access to the apis. By Default its 'force'
My goal is to show google analytics datas on the header of my site. Thats all I have until now (its from googleApiPhPClient/examples/analytics:
require_once 'library/GoogleApiPhpClient/apiClient.php';
$client = new apiClient();
$client->setApplicationName("Google Analytics PHP Starter Application");
// Visit https://code.google.com/apis/console?api=analytics to generate your
// client id, client secret, and to register your redirect uri.
$client->setClientId('aaa');
$client->setClientSecret('bbb_gK');
$client->setRedirectUri('ccc');
$client->setDeveloperKey('dd');
require_once ('contrib/apiAnalyticsService.php');
$service = new apiAnalyticsService($client);
if (isset($_GET['logout'])) {
unset($_SESSION['token']);
}
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()) {
$props = $service->management_webproperties->listManagementWebproperties("~all");
print "<h1>Web Properties</h1><pre>" . print_r($props, true) . "</pre>";
$accounts = $service->management_accounts->listManagementAccounts();
print "<h1>Accounts</h1><pre>" . print_r($accounts, true) . "</pre>";
$segments = $service->management_segments->listManagementSegments();
print "<h1>Segments</h1><pre>" . print_r($segments, true) . "</pre>";
$goals = $service->management_goals->listManagementGoals("~all", "~all", "~all");
print "<h1>Segments</h1><pre>" . print_r($goals, true) . "</pre>";
$_SESSION['token'] = $client->getAccessToken();
} else {
$authUrl = $client->createAuthUrl();
print "<a class='login' href='$authUrl'>Connect Me!</a>";
}
but this only drops a "connect me" link. Am in the somewhat right way on getting analytic datas anyway?
I've been using this code.google.com/p/gapi-google-analytics-php-interface which only requires the google account user name and password and works pretty well
a sample code of mine where im checking visits $ga->requestReportData($ga_profile_id,array('browser','browserVersion','country'),array('pageviews','visits'),null,"country==USA",$yesterday,$todate);
when you log in to your account, select the profile you already made for your web site . when you will click it, the url would be like google.com/analytics/web/#report/visitors-overview/… so the ga_profile_id would be the one written after "p" in the URL.. i see this way only to get this profile id