function bootstrap_new_user()
{
    global $base_url;
    $client = get_google_api_client();
    $client->setAccessToken(get_credentials($_SESSION['userid']));
    // A glass service for interacting with the Mirror API
    $mirror_service = new Google_MirrorService($client);
    $timeline_item = new Google_TimelineItem();
    $timeline_item->setText("Welcome to the Mirror API PHP Quick Start");
    insert_timeline_item($mirror_service, $timeline_item, null, null);
    insert_contact($mirror_service, "php-quick-start", "PHP Quick Start", $base_url . "/static/images/chipotle-tube-640x360.jpg");
    subscribe_to_notifications($mirror_service, "timeline", $_SESSION['userid'], $base_url . "/notify.php");
}
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;
        }
    }
}
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//  Author: Jenny Murphy - http://google.com/+JennyMurphy
require_once 'config.php';
require_once 'mirror-client.php';
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_Oauth2Service.php';
require_once 'util.php';
$client = get_google_api_client();
if (isset($_GET['code'])) {
    // Handle step 2 of the OAuth 2.0 dance - code exchange
    $client->authenticate();
    $access_token = $client->getAccessToken();
    // Use the identity service to get their ID
    $identity_client = get_google_api_client();
    $identity_client->setAccessToken($access_token);
    $identity_service = new Google_Oauth2Service($identity_client);
    $user = $identity_service->userinfo->get();
    $user_id = $user->getId();
    // Store their credentials and register their ID with their session
    $_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
    bootstrap_new_user();
    // redirect back to the base url
    header('Location: ' . $base_url);
} elseif (!isset($_SESSION['userid']) || get_credentials($_SESSION['userid']) == null) {
    // Handle step 1 of the OAuth 2.0 dance - redirect to Google
    header('Location: ' . $client->createAuthUrl());
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//  Author: Jenny Murphy - http://google.com/+JennyMurphy
// Verify that the parameters we want are there
if (!isset($_GET['timeline_item_id']) || !isset($_GET['attachment_id'])) {
    http_response_code(400);
    exit;
}
require_once 'config.php';
require_once 'mirror-client.php';
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_MirrorService.php';
require_once 'util.php';
$client = get_google_api_client();
// Authenticate if we're not already
if (!isset($_SESSION['userid']) || get_credentials($_SESSION['userid']) == null) {
    header('Location: ' . $base_url . '/oauth2callback.php');
    exit;
} else {
    $client->setAccessToken(get_credentials($_SESSION['userid']));
}
// A glass service for interacting with the Mirror API
$mirror_service = new Google_MirrorService($client);
// fetch the metadata
$attachment = $mirror_service->timeline_attachments->get($_GET['timeline_item_id'], $_GET['attachment_id']);
// set the content type header
header('Content-type: ' . $attachment->getContentType());
// echo the bytes
echo download_attachment($_GET['timeline_item_id'], $attachment);
     $custom_menu_item->setValues(array($custom_menu_value));
     $custom_menu_item->setAction("CUSTOM");
     // This is how you identify it on the notification ping
     $custom_menu_item->setId("safe-for-later");
     array_push($menu_items, $custom_menu_item);
     $new_timeline_item->setMenuItems($menu_items);
     insert_timeline_item($mirror_service, $new_timeline_item, null, null);
     $message = "Inserted a timeline item you can reply to";
     break;
 case 'insertTimelineAllUsers':
     $credentials = list_credentials();
     if (count($credentials) > 10) {
         $message = "Found " . count($credentials) . " users. Aborting to save your quota.";
     } else {
         foreach ($credentials as $credential) {
             $user_specific_client = get_google_api_client();
             $user_specific_client->setAccessToken($credential['credentials']);
             $new_timeline_item = new Google_TimelineItem();
             $new_timeline_item->setText("Did you know cats have 167 bones in their tails? Mee-wow!");
             $user_specific_mirror_service = new Google_MirrorService($user_specific_client);
             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 = subscribe_to_notifications($mirror_service, $_POST['subscriptionId'], $_SESSION['userid'], $base_url . "/notify.php");
     break;
 case 'deleteSubscription':
     $message = $mirror_service->subscriptions->delete($_POST['subscriptionId']);
     break;