From 958e63a5c98d5c4929691699792f1e633cb8f173 Mon Sep 17 00:00:00 2001 From: mimming Date: Sun, 5 May 2013 22:29:06 -0700 Subject: [PATCH 1/6] Added timeline echo for photos and timeline notifications --- notify.php | 68 +++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 62 insertions(+), 6 deletions(-) diff --git a/notify.php b/notify.php index 5ddb4d7..c9e4197 100755 --- a/notify.php +++ b/notify.php @@ -15,6 +15,34 @@ * limitations under the License. */ // Author: Jenny Murphy - http://google.com/+JennyMurphy +// Modified by: Winnie Tong + + + +// Always respond with a 200 right away and then terminate the connection to prevent notification +// retries. How this is done depends on your HTTP server configs. I'll try a few common techniques +// here, but if none of these work, start troubleshooting here. + +// First try: the content length header +header("Content-length: 0"); + +// Next, assuming it didn't work, attempt to close the output buffer by setting the time limit. +ignore_user_abort(true); +set_time_limit(0); + +// And one more thing to try: forking the heavy lifting into a new process. Yeah, crazy eh? +if(function_exists('pcntl_fork')) { + $pid = pcntl_fork(); + if ($pid == -1) { + error_log("could not fork!"); + exit(); + } else if ($pid) { + // fork worked! but I'm the parent. time to exit. + exit(); + } +} + +// In the child process (hopefully). Do the processing. require_once 'config.php'; require_once 'mirror-client.php'; require_once 'google-api-php-client/src/Google_Client.php'; @@ -22,16 +50,18 @@ require_once 'util.php'; if($_SERVER['REQUEST_METHOD'] != "POST") { - http_send_status(400); + echo "method not supported"; exit(); } // Parse the request body -$request = json_decode(http_get_request_body()); +$request_bytes = @file_get_contents('php://input'); +$request = json_decode($request_bytes, true); // A notification has come in. If there's an attached photo, bounce it back // to the user $user_id = $request['userToken']; + $access_token = get_credentials($user_id); $client = get_google_api_client(); @@ -40,8 +70,34 @@ // A glass service for interacting with the Mirror API $mirror_service = new Google_MirrorService($client); -$timeline_item = new Google_TimelineItem(); -$timeline_item->setText("Got a notification " . $request); +switch($request['collection']) { + case "timeline": + $timeline_item_id = $request['itemId']; + + $timeline_item = new Google_TimelineItem($mirror_service->timeline->get($timeline_item_id)); + + $attachments = $timeline_item->getAttachments(); + $attachment = $attachments[0]; + + $bytes = downloadAttachment($timeline_item_id, $attachment); + + // Insert a new timeline card, with a copy of that photo attached + $echo_timeline_item = new Google_TimelineItem(); + $echo_timeline_item->setText("Echoing your shared photo"); + $echo_timeline_item->setNotification( + new google_NotificationConfig(array("level"=>"DEFAULT"))); + insertTimelineItem($mirror_service, $echo_timeline_item, "image/jpeg", $bytes); + break; + case "locations": + $location = new Google_Location($mirror_service->locations->get("latest")); + // Insert a new timeline card, with a copy of that photo attached + $loc_timeline_item = new Google_TimelineItem(); + $loc_timeline_item->setText("You are at " . $location->getLatitude() . " by " . + $location->getLongitude()); + + insertTimelineItem($mirror_service, $loc_timeline_item, null, null); + break; + default: + error_log("I don't know how to process this notification:" . $request); +} -insertTimelineItem($mirror_service, $timeline_item, null, null); -//TODO: if there's an attached photo, echo it \ No newline at end of file From b1ab44bed30ee5c04a5d379a4caecce1253dc7f6 Mon Sep 17 00:00:00 2001 From: mimming Date: Wed, 29 May 2013 16:32:49 -0700 Subject: [PATCH 2/6] lots of clean up in response to Alain's comments --- config.php | 6 +++--- index.php | 14 +++++++------- notify.php | 44 ++++++++++++++++++++++++++------------------ util.php | 24 ------------------------ 4 files changed, 36 insertions(+), 52 deletions(-) diff --git a/config.php b/config.php index 026060f..59ef614 100755 --- a/config.php +++ b/config.php @@ -18,9 +18,9 @@ // TODO: You must configure these fields for the starter project to function. // Visit https://developers.google.com/glass/getting-started to learn more -$api_client_id = "API_CLIENT_ID"; -$api_client_secret = "API_CLIENT_SECRET"; -$api_simple_key = "API_KEY"; +$api_client_id = "465303650846.apps.googleusercontent.com"; +$api_client_secret = "wZgGkyRosOTHQkPnCB2zvb5L"; +$api_simple_key = "AIzaSyCCbHcqDanbwH5eb0oycQ9niV8P3n0F0qM"; $base_url = "http://localhost/mirror-quickstart-php"; diff --git a/index.php b/index.php index 766949b..8144bb0 100755 --- a/index.php +++ b/index.php @@ -37,7 +37,7 @@ // But first, handle POST data from the form (if there is any) switch ($_POST['operation']) { - case "insertItem": + case 'insertItem': $new_timeline_item = new Google_TimelineItem(); $new_timeline_item->setText($_POST['message']); @@ -54,7 +54,7 @@ $message = "Timeline Item inserted!"; break; - case "insertItemWithAction": + case 'insertItemWithAction': $new_timeline_item = new Google_TimelineItem(); $new_timeline_item->setText("What did you have for lunch?"); @@ -92,7 +92,7 @@ $message = "Inserted a timeline item you can reply to"; break; - case "insertTimelineAllUsers": + case 'insertTimelineAllUsers': $credentials = list_credentials(); if (count($credentials) > 10) { $message = "Found " . count($credentials) . " users. Aborting to save your quota."; @@ -111,19 +111,19 @@ $message = "Sent a cat fact to " . count($credentials) . " users."; } break; - case "insertSubscription": + case 'insertSubscription': $message = subscribeToNotifications($mirror_service, $_POST['subscriptionId'], $_SESSION['userid'], $base_url . "/notify.php"); break; - case "deleteSubscription": + case 'deleteSubscription': $message = $mirror_service->subscriptions->delete($_POST['subscriptionId']); break; - case "insertContact": + case 'insertContact': insertContact($mirror_service, $_POST['id'], $_POST['name'], $base_url . "/static/images/chipotle-tube-640x360.jpg"); $message = "Contact inserted. Enable it on MyGlass."; break; - case "deleteContact": + case 'deleteContact': deleteContact($mirror_service, $_POST['id']); $message = "Contact deleted."; break; diff --git a/notify.php b/notify.php index c9e4197..0be5929 100755 --- a/notify.php +++ b/notify.php @@ -31,7 +31,7 @@ set_time_limit(0); // And one more thing to try: forking the heavy lifting into a new process. Yeah, crazy eh? -if(function_exists('pcntl_fork')) { +if (function_exists('pcntl_fork')) { $pid = pcntl_fork(); if ($pid == -1) { error_log("could not fork!"); @@ -49,7 +49,7 @@ require_once 'google-api-php-client/src/contrib/Google_MirrorService.php'; require_once 'util.php'; -if($_SERVER['REQUEST_METHOD'] != "POST") { +if ($_SERVER['REQUEST_METHOD'] != "POST") { echo "method not supported"; exit(); } @@ -70,26 +70,34 @@ // A glass service for interacting with the Mirror API $mirror_service = new Google_MirrorService($client); -switch($request['collection']) { - case "timeline": - $timeline_item_id = $request['itemId']; +switch ($request['collection']) { + case 'timeline': + // Verify that it's a share + foreach ($request['userActions'] as $i => $user_action) { + if ($user_action['type'] == 'SHARE') { - $timeline_item = new Google_TimelineItem($mirror_service->timeline->get($timeline_item_id)); + $timeline_item_id = $request['itemId']; - $attachments = $timeline_item->getAttachments(); - $attachment = $attachments[0]; + $timeline_item = $mirror_service->timeline->get($timeline_item_id); - $bytes = downloadAttachment($timeline_item_id, $attachment); + foreach($timeline_item->getAttachments() as $j => $attachment) { + $attachment = $mirror_service->timeline->attachments->get($timeline_item_id, $attachment.getId()); + $bytes = downloadAttachment($timeline_item_id, $attachment); + + // Insert a new timeline card, with a copy of that photo attached + $echo_timeline_item = new Google_TimelineItem(); + $echo_timeline_item->setText("Echoing your shared photo"); + $echo_timeline_item->setNotification( + new google_NotificationConfig(array("level"=>"DEFAULT"))); + insertTimelineItem($mirror_service, $echo_timeline_item, "image/jpeg", $bytes); + } + break; + } + } - // Insert a new timeline card, with a copy of that photo attached - $echo_timeline_item = new Google_TimelineItem(); - $echo_timeline_item->setText("Echoing your shared photo"); - $echo_timeline_item->setNotification( - new google_NotificationConfig(array("level"=>"DEFAULT"))); - insertTimelineItem($mirror_service, $echo_timeline_item, "image/jpeg", $bytes); break; - case "locations": - $location = new Google_Location($mirror_service->locations->get("latest")); + case 'locations': + $location = $mirror_service->locations->get("latest"); // Insert a new timeline card, with a copy of that photo attached $loc_timeline_item = new Google_TimelineItem(); $loc_timeline_item->setText("You are at " . $location->getLatitude() . " by " . @@ -98,6 +106,6 @@ insertTimelineItem($mirror_service, $loc_timeline_item, null, null); break; default: - error_log("I don't know how to process this notification:" . $request); + error_log("I don't know how to process this notification: $request"); } diff --git a/util.php b/util.php index eadf508..624629d 100644 --- a/util.php +++ b/util.php @@ -21,30 +21,6 @@ require_once 'google-api-php-client/src/Google_Client.php'; require_once 'google-api-php-client/src/contrib/Google_MirrorService.php'; -// Returns an unauthenticated service -function get_google_api_client() { - global $api_client_id, $api_client_secret, $api_simple_key, $base_url; - // Set your cached access token. Remember to replace $_SESSION with a - // real database or memcached. - session_start(); - - $client = new Google_Client(); - - $client->setApplicationName('Google Mirror API PHP Quick Start'); - - // These are set in config.php - $client->setClientId($api_client_id); - $client->setClientSecret($api_client_secret); - $client->setDeveloperKey($api_simple_key); - $client->setRedirectUri($base_url."/oauth2callback.php"); - - $client->setScopes(array( - 'https://www.googleapis.com/auth/glass.timeline', - 'https://www.googleapis.com/auth/glass.location', - 'https://www.googleapis.com/auth/userinfo.profile')); - - return $client; -} function store_credentials($user_id, $credentials) { $db = init_db(); From c46d2a7b3894b8c084c10201031fac4298dd6bfb Mon Sep 17 00:00:00 2001 From: mimming Date: Wed, 29 May 2013 16:35:55 -0700 Subject: [PATCH 3/6] removed identifiers from config.php --- config.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/config.php b/config.php index 59ef614..f9e8891 100755 --- a/config.php +++ b/config.php @@ -18,10 +18,10 @@ // TODO: You must configure these fields for the starter project to function. // Visit https://developers.google.com/glass/getting-started to learn more -$api_client_id = "465303650846.apps.googleusercontent.com"; -$api_client_secret = "wZgGkyRosOTHQkPnCB2zvb5L"; -$api_simple_key = "AIzaSyCCbHcqDanbwH5eb0oycQ9niV8P3n0F0qM"; +$api_client_id = "API_CLIENT_ID"; +$api_client_secret = "API_CLIENT_SECRET"; +$api_simple_key = "API_KEY"; $base_url = "http://localhost/mirror-quickstart-php"; -$sqlite_database = "/tmp/database.sqlite"; \ No newline at end of file +$sqlite_database = "/tmp/database.sqlite"; From d3eb49a3f6e88ebb963c580ad448576b8bc12e74 Mon Sep 17 00:00:00 2001 From: mimming Date: Wed, 29 May 2013 16:43:08 -0700 Subject: [PATCH 4/6] cleaned up author header that was mismerged before --- notify.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/notify.php b/notify.php index 0be5929..23a4cf5 100755 --- a/notify.php +++ b/notify.php @@ -14,8 +14,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -// Author: Jenny Murphy - http://google.com/+JennyMurphy -// Modified by: Winnie Tong +// Author: Jenny Murphy - http://google.com/+JennyMurphy @@ -82,14 +81,14 @@ foreach($timeline_item->getAttachments() as $j => $attachment) { $attachment = $mirror_service->timeline->attachments->get($timeline_item_id, $attachment.getId()); - $bytes = downloadAttachment($timeline_item_id, $attachment); + $bytes = download_attachment($timeline_item_id, $attachment); // Insert a new timeline card, with a copy of that photo attached $echo_timeline_item = new Google_TimelineItem(); $echo_timeline_item->setText("Echoing your shared photo"); $echo_timeline_item->setNotification( new google_NotificationConfig(array("level"=>"DEFAULT"))); - insertTimelineItem($mirror_service, $echo_timeline_item, "image/jpeg", $bytes); + insert_timeline_item($mirror_service, $echo_timeline_item, "image/jpeg", $bytes); } break; } @@ -103,7 +102,7 @@ $loc_timeline_item->setText("You are at " . $location->getLatitude() . " by " . $location->getLongitude()); - insertTimelineItem($mirror_service, $loc_timeline_item, null, null); + insert_timeline_item($mirror_service, $loc_timeline_item, null, null); break; default: error_log("I don't know how to process this notification: $request"); From 68ecbbd14217d8f4445ab7376ee9f11a50a2e009 Mon Sep 17 00:00:00 2001 From: mimming Date: Wed, 29 May 2013 16:44:03 -0700 Subject: [PATCH 5/6] somehow I missed these in my last push --- attachment-proxy.php | 4 +- index.php | 93 ++++++++++++++++++++++---------------------- mirror-client.php | 68 ++++++++++++++++++++++---------- oauth2callback.php | 6 +-- signout.php | 2 +- util.php | 6 +-- 6 files changed, 103 insertions(+), 76 deletions(-) diff --git a/attachment-proxy.php b/attachment-proxy.php index d3f7f5f..d63bfd8 100644 --- a/attachment-proxy.php +++ b/attachment-proxy.php @@ -47,7 +47,7 @@ $_GET['timeline_item_id'], $_GET['attachment_id']); // set the content type header -header('Content-type: '. $attachment['contentType']); +header('Content-type: '. $attachment->getContentType()); // echo the bytes -echo downloadAttachment($_GET['timeline_item_id'], $attachment); +echo download_attachment($_GET['timeline_item_id'], $attachment); diff --git a/index.php b/index.php index 8144bb0..e38c0e2 100755 --- a/index.php +++ b/index.php @@ -46,10 +46,10 @@ $new_timeline_item->setNotification($notification); if (isset($_POST['imageUrl']) && isset($_POST['contentType'])) { - insertTimelineItem($mirror_service, $new_timeline_item, + insert_timeline_item($mirror_service, $new_timeline_item, $_POST['contentType'], file_get_contents($_POST['imageUrl'])); } else { - insertTimelineItem($mirror_service, $new_timeline_item, null, null); + insert_timeline_item($mirror_service, $new_timeline_item, null, null); } $message = "Timeline Item inserted!"; @@ -62,33 +62,33 @@ $notification->setLevel("DEFAULT"); $new_timeline_item->setNotification($notification); - $menuItems = array(); + $menu_items = array(); // A couple of built in menu items - $menuItem = new Google_MenuItem(); - $menuItem->setAction("READ_ALOUD"); - array_push($menuItems, $menuItem); + $menu_item = new Google_MenuItem(); + $menu_item->setAction("READ_ALOUD"); + array_push($menu_items, $menu_item); $new_timeline_item->setSpeakableText("What did you eat? Bacon?"); - $menuItem = new Google_MenuItem(); - $menuItem->setAction("SHARE"); - array_push($menuItems, $menuItem); + $menu_item = new Google_MenuItem(); + $menu_item->setAction("SHARE"); + array_push($menu_items, $menu_item); // A custom menu item - $customMenuItem = new Google_MenuItem(); - $customMenuValue = new Google_MenuValue(); - $customMenuValue->setDisplayName("Drill Into"); - $customMenuValue->setIconUrl($service_base_url . "/static/images/drill.png"); + $custom_menu_item = new Google_MenuItem(); + $custom_menu_value = new Google_MenuValue(); + $custom_menu_value->setDisplayName("Drill Into"); + $custom_menu_value->setIconUrl($service_base_url . "/static/images/drill.png"); - $customMenuItem->setValues(array($customMenuValue)); - $customMenuItem->setAction("CUSTOM"); + $custom_menu_item->setValues(array($custom_menu_value)); + $custom_menu_item->setAction("CUSTOM"); // This is how you identify it on the notification ping - $customMenuItem->setId("safe-for-later"); - array_push($menuItems, $customMenuItem); + $custom_menu_item->setId("safe-for-later"); + array_push($menu_items, $custom_menu_item); - $new_timeline_item->setMenuItems($menuItems); + $new_timeline_item->setMenuItems($menu_items); - insertTimelineItem($mirror_service, $new_timeline_item, null, null); + insert_timeline_item($mirror_service, $new_timeline_item, null, null); $message = "Inserted a timeline item you can reply to"; break; @@ -106,25 +106,25 @@ $user_specific_mirror_service = new Google_MirrorService($user_specific_client); - insertTimelineItem($user_specific_mirror_service, $new_timeline_item, null, null); + insert_timeline_item($user_specific_mirror_service, $new_timeline_item, null, null); } $message = "Sent a cat fact to " . count($credentials) . " users."; } break; case 'insertSubscription': - $message = subscribeToNotifications($mirror_service, $_POST['subscriptionId'], + $message = subscribe_to_notifications($mirror_service, $_POST['subscriptionId'], $_SESSION['userid'], $base_url . "/notify.php"); break; case 'deleteSubscription': $message = $mirror_service->subscriptions->delete($_POST['subscriptionId']); break; case 'insertContact': - insertContact($mirror_service, $_POST['id'], $_POST['name'], + insert_contact($mirror_service, $_POST['id'], $_POST['name'], $base_url . "/static/images/chipotle-tube-640x360.jpg"); $message = "Contact inserted. Enable it on MyGlass."; break; case 'deleteContact': - deleteContact($mirror_service, $_POST['id']); + delete_contact($mirror_service, $_POST['id']); $message = "Contact deleted."; break; } @@ -138,13 +138,13 @@ $contact = null; } $subscriptions = $mirror_service->subscriptions->listSubscriptions(); -$timelineSubscriptionExists = false; -$locationSubscriptionExists = false; -foreach ($subscriptions['items'] as $subscription) { - if ($subscription['id'] == 'timeline') { - $timelineSubscriptionExists = true; - } elseif ($subscription['id'] == 'location') { - $locationSubscriptionExists = true; +$timeline_subscription_exists = false; +$location_subscription_exists = false; +foreach ($subscriptions->getItems() as $subscription) { + if ($subscription->getId() == 'timeline') { + $timeline_subscription_exists = true; + } elseif ($subscription->getId() == 'location') { + $location_subscription_exists = true; } } @@ -184,26 +184,27 @@

Your Recent Timeline

- Message: + Message:
- + getItems() as $timeline_item) { ?>
    -
  • ID: +
  • ID: getId(); ?>
  • - Text: + Text: getId(); ?>
  • Attachments: getAttachments() != null) { + $attachments = $timeline_item->getAttachments(); foreach ($attachments as $attachment) { ?> - + - "> @@ -265,7 +266,7 @@
    - "> @@ -277,7 +278,7 @@
    - +
@@ -290,14 +291,14 @@

Note: Subscriptions require SSL.
They will not work on localhost.

- +
- +
@@ -305,7 +306,7 @@
- +
diff --git a/mirror-client.php b/mirror-client.php index c908d54..88d87ab 100755 --- a/mirror-client.php +++ b/mirror-client.php @@ -23,15 +23,41 @@ require_once 'config.php'; -function insertTimelineItem($service, $timelineItem, $contentType, $attachment) +// Returns an unauthenticated service +function get_google_api_client() { + global $api_client_id, $api_client_secret, $api_simple_key, $base_url; + // Set your cached access token. Remember to replace $_SESSION with a + // real database or memcached. + session_start(); + + $client = new Google_Client(); + + $client->setUseObjects(true); + $client->setApplicationName('Google Mirror API PHP Quick Start'); + + // These are set in config.php + $client->setClientId($api_client_id); + $client->setClientSecret($api_client_secret); + $client->setDeveloperKey($api_simple_key); + $client->setRedirectUri($base_url."/oauth2callback.php"); + + $client->setScopes(array( + 'https://www.googleapis.com/auth/glass.timeline', + 'https://www.googleapis.com/auth/glass.location', + 'https://www.googleapis.com/auth/userinfo.profile')); + + return $client; +} + +function insert_timeline_item($service, $timeline_item, $content_type, $attachment) { try { - $optParams = array(); - if ($contentType != null && $attachment != null) { - $optParams['data'] = $attachment; - $optParams['mimeType'] = $contentType; + $opt_params = array(); + if ($content_type != null && $attachment != null) { + $opt_params['data'] = $attachment; + $opt_params['mimeType'] = $content_type; } - return $service->timeline->insert($timelineItem, $optParams); + return $service->timeline->insert($timeline_item, $opt_params); } catch (Exception $e) { print 'An error ocurred: ' . $e->getMessage(); return null; @@ -44,18 +70,18 @@ function insertTimelineItem($service, $timelineItem, $contentType, $attachment) * @param Google_MirrorService $service Authorized Mirror service. * @param string $collection Collection to subscribe to (supported * values are "timeline" and "locations"). - * @param string $userToken Opaque token used by the Service to + * @param string $user_token Opaque token used by the Service to * identify the user the notification pings * are sent for (recommended). - * @param string $callbackUrl URL receiving notification pings (must be HTTPS). + * @param string $callback_url URL receiving notification pings (must be HTTPS). */ -function subscribeToNotifications($service, $collection, $userToken, $callbackUrl) +function subscribe_to_notifications($service, $collection, $user_token, $callback_url) { try { $subscription = new Google_Subscription(); $subscription->setCollection($collection); - $subscription->setUserToken($userToken); - $subscription->setCallbackUrl($callbackUrl); + $subscription->setUserToken($user_token); + $subscription->setCallbackUrl($callback_url); $service->subscriptions->insert($subscription); return "Subscription inserted!"; } catch (Exception $e) { @@ -63,13 +89,13 @@ function subscribeToNotifications($service, $collection, $userToken, $callbackUr } } -function insertContact($service, $contactId, $displayName, $iconUrl) +function insert_contact($service, $contact_id, $display_name, $icon_url) { try { $contact = new Google_Contact(); - $contact->setId($contactId); - $contact->setDisplayName($displayName); - $contact->setImageUrls(array($iconUrl)); + $contact->setId($contact_id); + $contact->setDisplayName($display_name); + $contact->setImageUrls(array($icon_url)); return $service->contacts->insert($contact); } catch (Exception $e) { print 'An error ocurred: ' . $e->getMessage(); @@ -81,11 +107,11 @@ function insertContact($service, $contactId, $displayName, $iconUrl) * Delete a contact for the current user. * * @param Google_MirrorService $service Authorized Mirror service. - * @param string $contactId ID of the Contact to delete. + * @param string $contact_id ID of the Contact to delete. */ -function deleteContact($service, $contactId) { +function delete_contact($service, $contact_id) { try { - $service->contacts->delete($contactId); + $service->contacts->delete($contact_id); } catch (Exception $e) { print 'An error occurred: ' . $e->getMessage(); } @@ -94,12 +120,12 @@ function deleteContact($service, $contactId) { /** * Download an attachment's content. * - * @param string $timelineId ID of the timeline item the attachment belongs to. + * @param string item_id ID of the timeline item the attachment belongs to. * @param Google_Attachment $attachment Attachment's metadata. * @return string The attachment's content if successful, null otherwise. */ -function downloadAttachment($itemId, $attachment) { - $request = new Google_HttpRequest($attachment['contentUrl'], 'GET', null, null); +function download_attachment($item_id, $attachment) { + $request = new Google_HttpRequest($attachment->getContentUrl(), 'GET', null, null); $httpRequest = Google_Client::$io->authenticatedRequest($request); if ($httpRequest->getResponseHttpCode() == 200) { return $httpRequest->getResponseBody(); diff --git a/oauth2callback.php b/oauth2callback.php index 6fd03e5..f9d2dcd 100644 --- a/oauth2callback.php +++ b/oauth2callback.php @@ -34,11 +34,11 @@ $identity_client->setAccessToken($access_token); $identity_service = new Google_Oauth2Service($identity_client); $user = $identity_service->userinfo->get(); - $userid = $user['id']; + $user_id = $user->getId(); // Store their credentials and register their ID with their session - $_SESSION['userid'] = $userid; - store_credentials($userid, $client->getAccessToken()); + $_SESSION['userid'] = $user_id; + store_credentials($user_id, $client->getAccessToken()); // Bootstrap the new user by inserting a welcome message, a contact, // and subscribing them to timeline notifications diff --git a/signout.php b/signout.php index e91ebe6..8080c60 100755 --- a/signout.php +++ b/signout.php @@ -18,4 +18,4 @@ session_start(); session_destroy(); ?> -You have been signed out. Sign back in \ No newline at end of file +You have been signed out. Sign back in \ No newline at end of file diff --git a/util.php b/util.php index 624629d..96d2e86 100644 --- a/util.php +++ b/util.php @@ -77,11 +77,11 @@ function bootstrap_new_user() { $timeline_item = new Google_TimelineItem(); $timeline_item->setText("Welcome to the Mirror API PHP Quick Start"); - insertTimelineItem($mirror_service, $timeline_item, null, null); + insert_timeline_item($mirror_service, $timeline_item, null, null); - insertContact($mirror_service, "php-quick-start", "PHP Quick Start", + insert_contact($mirror_service, "php-quick-start", "PHP Quick Start", $base_url . "/static/images/chipotle-tube-640x360.jpg"); - subscribeToNotifications($mirror_service, "timeline", + subscribe_to_notifications($mirror_service, "timeline", $_SESSION['userid'], $base_url . "/notify.php"); } \ No newline at end of file From d3caf0e326cb36b1f588e39a289c5e1141dadcec Mon Sep 17 00:00:00 2001 From: mimming Date: Wed, 29 May 2013 21:12:38 -0700 Subject: [PATCH 6/6] responded to Alain's pull request comments --- notify.php | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/notify.php b/notify.php index 23a4cf5..19fde77 100755 --- a/notify.php +++ b/notify.php @@ -17,6 +17,12 @@ // Author: Jenny Murphy - http://google.com/+JennyMurphy +// Only process POST requests +if ($_SERVER['REQUEST_METHOD'] != "POST") { + header("HTTP/1.0 405 Method not supported"); + echo("Method not supported"); + exit(); +} // Always respond with a 200 right away and then terminate the connection to prevent notification // retries. How this is done depends on your HTTP server configs. I'll try a few common techniques @@ -48,11 +54,6 @@ require_once 'google-api-php-client/src/contrib/Google_MirrorService.php'; require_once 'util.php'; -if ($_SERVER['REQUEST_METHOD'] != "POST") { - echo "method not supported"; - exit(); -} - // Parse the request body $request_bytes = @file_get_contents('php://input'); $request = json_decode($request_bytes, true); @@ -96,7 +97,8 @@ break; case 'locations': - $location = $mirror_service->locations->get("latest"); + $location_id = $request['itemId']; + $location = $mirror_service->locations->get($location_id); // Insert a new timeline card, with a copy of that photo attached $loc_timeline_item = new Google_TimelineItem(); $loc_timeline_item->setText("You are at " . $location->getLatitude() . " by " .