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>";
}
Related
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 completely lost... All I'm trying to do is get rid of this error message and log into Google API. I've created all the keys and added them to /src/Google/Config.php.
PHP Fatal error: Uncaught exception 'Google_Service_Exception' with message 'Error calling GET
https://www.googleapis.com/calendar/v3/users/me/calendarList/0qfrti6gst4q8hpl89utm8elno%40grou p.calendar.google.com?key=AIzaSyAiR_4OsoheCPbd7tU2u3QrqbEW_a2uVCc: (401) Login Required'
in C:\\Bitnami\\wampstack\\apache2\\htdocs\\yac\\google-api-php-
client\\src\\Google\\Http\\REST.php:79\nStack ...
Here's my (most likely) incorrect script:
session_start();
set_include_path( get_include_path() . PATH_SEPARATOR . 'google-api-php-client/src' );
include_once('google-api-php-client/src');
require_once('google-api-php-client/src/Google/Client.php');
require_once ('google-api-php-client/src/Google/Service/Calendar.php');
$client = new Google_Client();
$client->setApplicationName("YAC_Calendar");
$client->setDeveloperKey(<MY KEY>);
$service = new Google_Service_Calendar($client);
$calendarListEntry = $service->calendarList->get('0qfrti6gst4q8hpl89utm8elno#group.calendar.google.com');
echo $calendarListEntry->getSummary();
you need to login using Oauth before you can access the calendar. The only example I have on hand right now is one using Google Analytics API with PHP if you have any switching it over to Calendar let me know I will see if I can get a working example for that.
<?php
require_once 'Google/Client.php';
require_once 'Google/Service/Analytics.php';
session_start();
$client = new Google_Client();
$client->setApplicationName("Client_Library_Examples");
$client->setDeveloperKey("{devkey}");
$client->setClientId('{clientid}.apps.googleusercontent.com');
$client->setClientSecret('{clientsecret}');
$client->setRedirectUri('http://www.daimto.com/Tutorials/PHP/Oauth2.php');
$client->setScopes(array('https://www.googleapis.com/auth/analytics.readonly'));
//For loging out.
if ($_GET['logout'] == "1") {
unset($_SESSION['token']);
}
// Step 2: The user accepted your access now you need to exchange it.
if (isset($_GET['code'])) {
$client->authenticate($_GET['code']);
$_SESSION['token'] = $client->getAccessToken();
$redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}
// Step 1: The user has not authenticated we give them a link to login
if (!$client->getAccessToken() && !isset($_SESSION['token'])) {
$authUrl = $client->createAuthUrl();
print "<a class='login' href='$authUrl'>Connect Me!</a>";
}
// Step 3: We have access we can now create our service
if (isset($_SESSION['token'])) {
print "<a class='logout' href='".$_SERVER['PHP_SELF']."?logout=1'>LogOut</a><br>";
$client->setAccessToken($_SESSION['token']);
$service = new Google_Service_Analytics($client);
// request user accounts
$accounts = $service->management_accountSummaries->listManagementAccountSummaries();
foreach ($accounts->getItems() as $item) {
echo "Account: ",$item['name'], " " , $item['id'], "<br /> \n";
foreach($item->getWebProperties() as $wp) {
echo ' WebProperty: ' ,$wp['name'], " " , $wp['id'], "<br /> \n";
$views = $wp->getProfiles();
if (!is_null($views)) {
foreach($wp->getProfiles() as $view) {
// echo ' View: ' ,$view['name'], " " , $view['id'], "<br /> \n";
}
}
}
} // closes account summaries
}
print "<br><br><br>";
print "Access from google: " . $_SESSION['token'];
?>
Code from my tutorial Google Oauth2 php
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.
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