Ejemplo n.º 1
0
 public function refresh_access_token($email = '')
 {
     if ($email == '') {
         return '';
     }
     $query = $this->db->query("SELECT refresh_token FROM " . TABLE_GOOGLE . " WHERE email=?", array($email));
     if (!isset($query->row['refresh_token'])) {
         return '';
     }
     $client = new apiClient();
     $client->setApplicationName(GOOGLE_APPLICATION_NAME);
     $client->setClientId(GOOGLE_CLIENT_ID);
     $client->setClientSecret(GOOGLE_CLIENT_SECRET);
     $client->setRedirectUri(GOOGLE_REDIRECT_URL);
     $client->setDeveloperKey(GOOGLE_DEVELOPER_KEY);
     $client->refreshToken($query->row['refresh_token']);
     $s = $client->getAccessToken();
     $a = json_decode($s);
     if (isset($a->{'access_token'})) {
         return $a->{'access_token'};
     }
     return '';
 }
Ejemplo n.º 2
0
if (isset($_GET['code'])) {
    $client->authenticate();
    $_SESSION['access_token'] = $client->getAccessToken();
    header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
}
if (isset($_SESSION['access_token'])) {
    $client->setAccessToken($_SESSION['access_token']);
    //json decode the session token and save it in a variable as object
    $sessionToken = json_decode($_SESSION['access_token']);
    //Save the refresh token (object->refresh_token) into a cookie called 'token' and make last for 1 month
    if (isset($sessionToken->refresh_token)) {
        //refresh token is only set after a proper authorisation
        $number_of_days = 30;
        $date_of_expiry = time() + 60 * 60 * 24 * $number_of_days;
        setcookie('token', $sessionToken->refresh_token, $date_of_expiry);
    }
} else {
    if (isset($_COOKIE["token"])) {
        //if we don't have a session we will grab it from the cookie
        $client->refreshToken($_COOKIE["token"]);
        //update token
    }
}
if ($client->getAccessToken()) {
    $me = $plus->people->get('me');
    $optParams = array('maxResults' => 100);
    $activities = $plus->activities->listActivities('me', 'public', $optParams);
    $_SESSION['access_token'] = $client->getAccessToken();
} else {
    $authUrl = $client->createAuthUrl();
}