Пример #1
0
function getClient()
{
    $client = new Google_Client();
    $client->setApplicationName(APPLICATION_NAME);
    $client->setScopes(SCOPES);
    $client->setAuthConfigFile(CLIENT_SECRET);
    $client->setAccessType('offline');
    // Load previously authorized credentials from a file.
    $credentialsPath = expandHomeDirectory(CREDENTIAL_PATH);
    if (file_exists($credentialsPath)) {
        $accessToken = file_get_contents($credentialsPath);
    } else {
        // Request authorization from the user.
        $authUrl = $client->createAuthUrl();
        printf("Open the following link in your browser:\n\n\t%s\n\n", $authUrl);
        print 'Enter verification code: ';
        $authCode = trim(fgets(STDIN));
        // Exchange authorization code for an access token.
        $accessToken = $client->authenticate($authCode);
        // Store the credentials to disk.
        if (!file_exists(dirname($credentialsPath))) {
            mkdir(dirname($credentialsPath), 0700, true);
        }
        file_put_contents($credentialsPath, $accessToken);
        printf("Credentials saved to %s\n", $credentialsPath);
    }
    $client->setAccessToken($accessToken);
    // Refresh the token if it's expired.
    if ($client->isAccessTokenExpired()) {
        $client->refreshToken($client->getRefreshToken());
        file_put_contents($credentialsPath, $client->getAccessToken());
    }
    return $client;
}
Пример #2
0
function getAccessToken($client)
{
    $credentialsPath = expandHomeDirectory(CREDENTIALS_PATH);
    $accessToken = file_get_contents($credentialsPath);
    $client->setAccessToken($accessToken);
    // Refresh the token if it's expired.
    if ($client->isAccessTokenExpired()) {
        $client->refreshToken($client->getRefreshToken());
        file_put_contents($credentialsPath, $client->getAccessToken());
    }
    return $client;
}
Пример #3
0
/**
 * Returns an authorized API client.
 * @return Google_Client the authorized client object
 */
function gfGetClient()
{
    $client = new Google_Client();
    $client->setApplicationName(APPLICATION_NAME);
    $client->setScopes(SCOPES);
    $client->setAuthConfigFile(CLIENT_SECRET_PATH);
    $client->setAccessType('offline');
    // Load previously authorized credentials from a file.
    $credentialsPath = expandHomeDirectory(CREDENTIALS_PATH);
    if (file_exists($credentialsPath)) {
        $accessToken = file_get_contents($credentialsPath);
    } else {
        echo $credentialsPath;
        die("Error: User is not authenticated.");
    }
    $client->setAccessToken($accessToken);
    // Refresh the token if it's expired.
    if ($client->isAccessTokenExpired()) {
        $client->refreshToken($client->getRefreshToken());
        file_put_contents($credentialsPath, $client->getAccessToken());
    }
    return $client;
}
Пример #4
0
// require __DIR__ . '/libs/constants.php';
require __DIR__ . '/libs/functions.php';
if (isset($_GET['code'])) {
    $authCode = trim($_GET['code']);
    $client = getClient();
    // Exchange authorization code for an access token.
    $accessToken = $client->authenticate($authCode);
    $credentialsPath = expandHomeDirectory(CREDENTIALS_PATH);
    // Store the credentials to disk.
    if (!file_exists(dirname($credentialsPath))) {
        mkdir(dirname($credentialsPath), 0700, true);
    }
    file_put_contents($credentialsPath, $accessToken);
} else {
    // Load previously authorized credentials from a file.
    $credentialsPath = expandHomeDirectory(CREDENTIALS_PATH);
    if (file_exists($credentialsPath)) {
        $accessToken = file_get_contents($credentialsPath);
    } else {
        // Request authorization from the user.
        $client = getClient();
        $authUrl = $client->createAuthUrl();
        echo "<a href='{$authUrl}'>Log in</a>";
    }
}
if (file_exists($credentialsPath)) {
    // Get the API client and construct the service object.
    $client = getClient();
    $client = getAccessToken($client);
    $service = new Google_Service_Gmail($client);
    $optParams = array();
Пример #5
0
 public function post($to, $message)
 {
     $credentialsPath = expandHomeDirectory('~/.config/email-to-vk-vk.json');
     if (file_exists($credentialsPath)) {
         $accessToken = file_get_contents($credentialsPath);
     } else {
         // Request authorization from the user.
         // Let's pretend we are IPad application 8-)
         $authUrl = 'https://oauth.vk.com/authorize?client_id=3682744&v=5.7&scope=wall,offline&redirect_uri=http://oauth.vk.com/blank.html&display=page&response_type=token';
         printf("Open the following link in your browser:\n%s\n", $authUrl);
         print 'Enter access token: ';
         $accessToken = trim(fgets(STDIN));
         // Store the credentials to disk.
         if (!file_exists(dirname($credentialsPath))) {
             mkdir(dirname($credentialsPath), 0700, true);
         }
         file_put_contents($credentialsPath, $accessToken);
         printf("Credentials saved to %s\n", $credentialsPath);
     }
     return file_get_contents('https://api.vk.com/method/wall.post?owner_id=' . $to . '&from_group=1&message=' . urlencode($message) . '&access_token=' . $accessToken);
 }
Пример #6
0
 /**
  * Returns an authorized API client
  *
  * @return Google_Client the authorized client object
  */
 function getClient()
 {
     global $ini, $verbose;
     static $client = NULL;
     if ($client) {
         return $client;
     }
     if (DEBUG) {
         print "Google APIs application name: {$ini['gmail_application_name']}\n" . "Google Gmail client secret file: {$ini['gmail_client_secret_path']}\n" . "Google Gmail credentials path: {$ini['gmail_credentials_path']}\n";
     }
     try {
         $client = new Google_Client();
         $client->setApplicationName($ini['gmail_application_name']);
         $client->setScopes(GMAIL_SCOPES);
         $client->setAuthConfigFile($ini['gmail_client_secret_path']);
         $client->setLoginHint($ini['gmail_username']);
         // We want access even when the user is not logged in
         $client->setAccessType('offline');
         // Load previously authorized credentials from a file.
         $credentialsPath = expandHomeDirectory($ini['gmail_credentials_path']);
         if (file_exists($credentialsPath)) {
             if (DEBUG) {
                 print "Using existing access token from {$credentialsPath}\n";
             }
             $accessToken = file_get_contents($credentialsPath);
         } else {
             // Request authorisation from the user.
             if (DEBUG) {
                 print "Requesting authorisation\n";
             }
             $authURL = $client->createAuthUrl();
             if (php_sapi_name() != 'cli') {
                 // Re-direct browser to authentication URL
                 header('Location: ' . filter_var($authURL, FILTER_SANITIZE_URL));
             }
             print "Open the following link in your browser:\n{$authURL}\n";
             print 'Enter verification code: ';
             $authCode = trim(fgets(STDIN));
             // Exchange authorization code for an access token.
             $accessToken = $client->authenticate($authCode);
             // Store the credentials to disk.
             if (!file_exists(dirname($credentialsPath))) {
                 mkdir(dirname($credentialsPath), 0700, true);
             }
             file_put_contents($credentialsPath, $accessToken);
             if ($verbose > 3) {
                 print "Credentials saved to {$credentialsPath}\n";
             }
         }
         if (DEBUG) {
             print_r($accessToken);
             print "\n";
         }
         $client->setAccessToken($accessToken);
         // Refresh the token if it's expired.
         if ($client->isAccessTokenExpired()) {
             $client->refreshToken($client->getRefreshToken());
             $accessToken = $client->getAccessToken();
             // Check we've not lost the refresh token
             if (FALSE == strstr($accessToken, 'refresh_token')) {
                 die("Error: Refreshed access token no longer contains refresh token used to retrieve it\n");
             }
             if (DEBUG) {
                 print "Refreshed accesss token:\n";
                 print_r($accessToken);
             }
             file_put_contents($credentialsPath, $accessToken);
         }
     } catch (Exception $e) {
         print 'Exception: ' . $e->getMessage();
     }
     return $client;
 }