Skip to content
This repository has been archived by the owner on May 11, 2022. It is now read-only.

Commit

Permalink
Now properly displays timelineItem.text on index page hero unit
Browse files Browse the repository at this point in the history
Now auto-reauthenticates when disabled from MyGlass. This required a database change. If you're upgrading your local copy you will need to migrate or purge your current database.
  • Loading branch information
mimming committed Jun 30, 2013
1 parent a6e0c49 commit 415e061
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 8 deletions.
8 changes: 2 additions & 6 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
header('Location: ' . $base_url . '/oauth2callback.php');
exit;
} else {
verify_credentials(get_credentials($_SESSION['userid']));
$client->setAccessToken(get_credentials($_SESSION['userid']));
}

Expand Down Expand Up @@ -170,11 +171,6 @@
<div class="navbar-inner">
<div class="container">
<a class="brand" href="#">Glassware Starter Project: PHP Edition</a>
<div class="nav-collapse collapse">
<form class="navbar-form pull-right" action="signout.php" method="post">
<button type="submit" class="btn">Sign out</button>
</form>
</div>
</div>
</div>
</div>
Expand All @@ -193,7 +189,7 @@
<li><strong>ID: </strong> <?php echo $timeline_item->getId(); ?>
</li>
<li>
<strong>Text: </strong> <?php echo $timeline_item->getId(); ?>
<strong>Text: </strong> <?php echo $timeline_item->getText(); ?>
</li>
<li>
<strong>Attachments: </strong>
Expand Down
34 changes: 34 additions & 0 deletions mirror-client.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
// found at https://developers.google.com/glass/v1/reference

require_once 'config.php';
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_Oauth2Service.php';

// Returns an unauthenticated service
function get_google_api_client() {
Expand Down Expand Up @@ -49,6 +51,38 @@ function get_google_api_client() {
return $client;
}

/*
* Verify the credentials. If they're broken, attempt to re-auth
* This will only work if you haven't printed anything yet (since
* it uses an HTTP header for the redirect)
*/
function verify_credentials($credentials) {
//TODO: Use the oauth2.tokeninfo() method instead once it's
// exposed by the PHP client library
global $base_url;

$client = get_google_api_client();
$client->setAccessToken($credentials);

$token_checker = new Google_Oauth2Service($client);
try {
$token_checker->userinfo->get();
} catch (Google_ServiceException $e) {
if($e->getCode() == 401) {
// This user may have disabled the Glassware on MyGlass.
// Clean up the mess and attempt to re-auth.
unset($_SESSION['userid']);
header('Location: ' . $base_url . '/oauth2callback.php');
exit;

} else {
// Let it go...
throw $e;
}
}

}

function insert_timeline_item($service, $timeline_item, $content_type, $attachment)
{
try {
Expand Down
5 changes: 3 additions & 2 deletions util.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function store_credentials($user_id, $credentials) {
$user_id = sqlite_escape_string(strip_tags($user_id));
$credentials = sqlite_escape_string(strip_tags($credentials));

$insert = "insert into credentials values ('$user_id', '$credentials')";
$insert = "insert or replace into credentials values ('$user_id', '$credentials')";
sqlite_exec($db, $insert);

}
Expand Down Expand Up @@ -59,7 +59,8 @@ function init_db() {
$db = sqlite_open($sqlite_database);
$test_query = "select count(*) from sqlite_master where name = 'credentials'";
if (sqlite_fetch_single(sqlite_query($db, $test_query)) == 0) {
$create_table = "create table credentials (userid text, credentials text);";
$create_table = "create table credentials (userid text not null unique, " .
"credentials text not null);";
sqlite_exec($db, $create_table);
}
return $db;
Expand Down

0 comments on commit 415e061

Please sign in to comment.