本文整理汇总了PHP中Google_Client::getRefreshToken方法的典型用法代码示例。如果您正苦于以下问题:PHP Google_Client::getRefreshToken方法的具体用法?PHP Google_Client::getRefreshToken怎么用?PHP Google_Client::getRefreshToken使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Google_Client
的用法示例。
在下文中一共展示了Google_Client::getRefreshToken方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: getGoogleApiClient
/**
* Returns an authorized API client.
* @return Google_Client the authorized client object
*/
private function getGoogleApiClient()
{
$client = new Google_Client();
$client->setApplicationName($this->CFG['GOOGLE_API_APPLICATION_NAME']);
$client->setScopes(Google_Service_Calendar::CALENDAR);
$client->setAuthConfigFile($this->CFG['GOOGLE_API_CLIENT_SECRET_PATH']);
$client->setAccessType('offline');
// Load previously authorized credentials from a file.
$credentialsPath = $this->CFG['GOOGLE_API_CREDENTIALS_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%s\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: oauth2callback
public function oauth2callback()
{
$this->tpl_name = "";
require_once '../tf_app/ext/google/autoload.php';
session_start();
// ******************************************************** //
// Get these values from https://console.developers.google.com
// Be sure to enable the Analytics API
// ******************************************************** //
$client_id = '334086275725-f15a3bl6bm7kbhfkm66u9va1si5bb9ia.apps.googleusercontent.com';
$client_secret = 'FbKWozz_ZNmlk5PUEXPC2BUO';
$redirect_uri = 'http://localhost.me:8006/ajax_v3/oauth2callback';
$client = new Google_Client();
$client->setApplicationName("test-calendar");
$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$client->setRedirectUri($redirect_uri);
$client->setAccessType('offline');
// Gets us our refreshtoken
$client->setScopes(array('https://www.googleapis.com/auth/calendar'));
//For loging out.
if (isset($_GET['logout'])) {
unset($_SESSION['token']);
}
// Step 2: The user accepted your access now you need to exchange it.
if (isset($_GET['code'])) {
$client->authenticate($_GET['code']);
$_SESSION['token'] = $client->getAccessToken();
$redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}
// $_SESSION["token"]["access_token"] = "ya29.xAFLnfAz9X-h7LWyUF6258cMbOzcUljurQPw0UMPdnpP15fiz5SWY0yJXvKvfaupQuYCPA";
// Step 1: The user has not authenticated we give them a link to login
if (!isset($_SESSION['token'])) {
$authUrl = $client->createAuthUrl();
print "<a class='login' href='{$authUrl}'>Connect Me!</a>";
}
// Step 3: We have access we can now create our service
if (isset($_SESSION['token'])) {
// $client->refreshToken($refreshToken);
$client->setAccessToken($_SESSION['token']);
$refresh_token = $client->getRefreshToken();
$_SESSION['refresh_token'] = $refresh_token;
$token = json_decode($_SESSION['token']);
// echo "Access Token = " . $token->access_token . '<br/>';
// echo "Refresh Token = " . $token->refresh_token . '<br/>';
// echo "Token type = " . $token->token_type . '<br/>';
// echo "Expires in = " . $token->expires_in . '<br/>';
// echo "ID Token = " . $token->id_token . '<br/>';
// echo "Created = " . $token->created . '<br/>';
print "<a class='logout' href='http://localhost.me:8006/ajax_v3/oauth2callback?logout=1'>LogOut</a><br>";
// print_r($_SESSION);
$service = new Google_Service_Calendar($client);
$calendarList = $service->calendarList->listCalendarList();
while (true) {
foreach ($calendarList->getItems() as $calendarListEntry) {
echo $calendarListEntry->getSummary() . "<br>\n";
// get events
$events = $service->events->listEvents($calendarListEntry->id);
foreach ($events->getItems() as $event) {
echo "-----" . $event->getSummary() . "<br>";
}
}
$pageToken = $calendarList->getNextPageToken();
if ($pageToken) {
$optParams = array('pageToken' => $pageToken);
$calendarList = $service->calendarList->listCalendarList($optParams);
} else {
break;
}
}
// reservation
if ($_REQUEST["type"] == "r") {
$rsv = $this->Space_reserve->list_(1, 1000, 'sr.state = 1 and sr.date > "' . date('Y.m.d', strtotime('-1 month')) . '"');
$len_rsv = sizeof($rsv);
for ($r = 0; $r < $len_rsv; $r++) {
$rsv[$r]["s"] = substr_replace(date("Y-m-dH:i:00", strtotime(str_replace(".", "-", $rsv[$r]["date"]) . $rsv[$r]["start"])), 'T', 10, 0);
$rsv[$r]["e"] = substr_replace(date("Y-m-dH:i:00", strtotime(str_replace(".", "-", $rsv[$r]["date"]) . $rsv[$r]["end"])), 'T', 10, 0);
$rsv[$r]["summary"] = "[" . $rsv[$r]["opt"] . "F " . explode_ext($rsv[$r]["name"], '(') . "] " . $rsv[$r]["purpose"];
$rsv[$r]["location"] = $rsv[$r]["opt"] . "F " . explode_ext($rsv[$r]["name"], '(');
}
$_calId = '6er2bdtm0ojetsnn09uj6plbq8@group.calendar.google.com';
$fo = $rsv;
}
// event
if ($_REQUEST["type"] == "e") {
$ev = $this->Event->list_(1, 10000, 'e.state = 1 and open = 1 and (e.date > "' . date('Y.m.d', strtotime('-1 month')) . '" or e.date_e > "' . date('Y.m.d', strtotime('-1 month')) . '")');
$len_ev = sizeof($ev);
for ($r = 0; $r < $len_ev; $r++) {
$ev[$r]["s"] = substr_replace(date("Y-m-dH:i:00", strtotime(str_replace(".", "-", $ev[$r]["date"]) . $ev[$r]["start"])), 'T', 10, 0);
$ev[$r]["e"] = substr_replace(date("Y-m-dH:i:00", strtotime(str_replace(".", "-", $ev[$r]["date_e"]) . $ev[$r]["end"])), 'T', 10, 0);
$ev[$r]["summary"] = "[Event] " . $ev[$r]["title"];
if ($ev[$r]["space_id"] == 0) {
$ev[$r]["location"] = $ev[$r]["space_etc"];
} else {
$ev[$r]["location"] = "D.CAMP " . $ev[$r]["opt"] . "F " . explode_ext($ev[$r]["sname"], '(');
}
}
$_calId = '3h37pgh9qvtvd9r7jun88elhfk@group.calendar.google.com';
$fo = $ev;
//.........这里部分代码省略.........
示例3: refreshToken
public function refreshToken($accessToken)
{
$this->_googleClient->setAccessToken($accessToken);
// Refresh the token if it's expired.
if ($this->_googleClient->isAccessTokenExpired()) {
$this->_googleClient->refreshToken($this->_googleClient->getRefreshToken());
}
return $this->_googleClient->getAccessToken();
}
示例4: isLoggedIn
/**
* @return string
*/
public function isLoggedIn()
{
if (\Session::has('token')) {
$this->client->setAccessToken(\Session::get('token'));
} else {
return false;
}
if ($this->client->isAccessTokenExpired()) {
\Session::set('token', $this->client->getRefreshToken());
}
return !$this->client->isAccessTokenExpired();
}
示例5: cmdLineAuth
/**
* Auth over command line
*/
public function cmdLineAuth()
{
$authUrl = $this->client->createAuthUrl();
//Request authorization
print "Please visit:\n{$authUrl}\n\n";
print "Please enter the auth code:\n";
$authCode = trim(fgets(STDIN));
// Exchange authorization code for access token
$accessToken = $this->client->authenticate($authCode);
$this->client->setAccessToken($accessToken);
$this->accessToken = $accessToken;
$this->refreshToken = $this->client->getRefreshToken();
}
示例6: refreshAuthToken
private function refreshAuthToken(\Google_Client $client)
{
if ($client->isAccessTokenExpired()) {
$client->refreshToken($client->getRefreshToken());
file_put_contents(CREDENTIALS_FILE, $client->getAccessToken());
}
}
示例7: getClient
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;
}
示例8: getClient
function getClient()
{
$client = new Google_Client();
$client->setApplicationName(APPLICATION_NAME);
$client->setScopes(SCOPES);
$client->setAuthConfigFile(CLIENT_SECRET_PATH);
$client->setAccessType('offline');
$credentialsPath = CREDENTIALS_PATH;
if (file_exists($credentialsPath)) {
$accessToken = file_get_contents($credentialsPath);
} else {
$authUrl = $client->createAuthUrl();
printf("Open the following link in your browser:\n%s\n", $authUrl);
print 'Enter verification code: ';
$authCode = trim(fgets(STDIN));
$accessToken = $client->authenticate($authCode);
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);
if ($client->isAccessTokenExpired()) {
$client->refreshToken($client->getRefreshToken());
file_put_contents($credentialsPath, $client->getAccessToken());
}
return $client;
}
示例9: loadByStaff
/**
* Load Google and Calendar Service data by Staff
*
* @param AB_Staff $staff
* @return bool
*/
public function loadByStaff(AB_Staff $staff)
{
$this->staff = $staff;
if ($staff->get('google_data')) {
try {
$this->client->setAccessToken($staff->get('google_data'));
if ($this->client->isAccessTokenExpired()) {
$this->client->refreshToken($this->client->getRefreshToken());
$staff->set('google_data', $this->client->getAccessToken());
$staff->save();
}
$this->service = new Google_Service_Calendar($this->client);
return true;
} catch (Exception $e) {
$this->errors[] = $e->getMessage();
}
}
return false;
}
示例10: fetchAction
public function fetchAction()
{
$em = $this->getDoctrine()->getEntityManager();
// TODO move to a cron job
// TODO move google credentials to symfony parameters file
$configDirectories = array(__DIR__ . '/../Resources/config');
$locator = new FileLocator($configDirectories);
$clientSecret = $locator->locate('client_secret.json');
$credentialsPath = $locator->locate('calendar-api-quickstart.json');
// Get the API client and construct the service object.
$client = new \Google_Client();
$client->setApplicationName('Google Calendar API Quickstart');
$client->setScopes(implode(' ', array(\Google_Service_Calendar::CALENDAR_READONLY)));
$client->setAuthConfigFile($clientSecret);
$client->setAccessType('offline');
// Load previously authorized credentials from a file.
if (file_exists($credentialsPath)) {
$accessToken = file_get_contents($credentialsPath);
} else {
//TODO this should be accomplished with an error status
return $this->render('TechlancasterWebBundle:Default:calendar.html.twig', array('message' => 'Invalid Credentials'));
}
$client->setAccessToken($accessToken);
// Refresh the token if it's expired.
if ($client->isAccessTokenExpired()) {
$client->refreshToken($client->getRefreshToken());
file_put_contents($credentialsPath, $client->getAccessToken());
}
$service = new \Google_Service_Calendar($client);
$calendarId = '6l7e832ee9bemt1i9c42vltrug@group.calendar.google.com';
$optParams = array('maxResults' => 29, 'orderBy' => 'startTime', 'singleEvents' => true, 'timeMin' => date('c'));
$results = $service->events->listEvents($calendarId, $optParams);
$events = array();
$connection = $em->getConnection();
$platform = $connection->getDatabasePlatform();
$connection->executeUpdate($platform->getTruncateTableSQL('event', false));
/**
* @var $googleEvent Google_Service_Calendar_Event
*/
foreach ($results->getItems() as $googleEvent) {
$event = new Event();
$event->setId($googleEvent->getId());
$event->setDescription($googleEvent->getDescription());
$event->setLocation($googleEvent->getLocation());
$event->setStart(new \DateTime($googleEvent->getStart()->dateTime));
$event->setEnd(new \DateTime($googleEvent->getEnd()->dateTime));
$event->setSummary($googleEvent->getSummary());
$em->persist($event);
$events[] = $event;
}
$em->flush();
return $this->render('TechlancasterWebBundle:Default:calendar.html.twig', array('message' => json_encode($events)));
}
示例11: authenticate
/**
* @param null $code
* @param User $user
* @return bool
*/
public function authenticate($code = null, User $user)
{
try {
$this->client->authenticate($code);
} catch (\Exception $e) {
return false;
}
$session = new Session();
$session->set('access_token', $this->getAccessToken());
$refreshToken = $this->client->getRefreshToken();
$user->setGoogleRefreshToken($refreshToken);
$this->em->flush();
return true;
}
示例12: __construct
/**
* Returns an authorized API client.
* @return \Google_Client the authorized client object
*/
public function __construct()
{
$client = new \Google_Client();
$client->setApplicationName(APPLICATION_NAME);
$client->setScopes(SCOPES);
$client->setAuthConfigFile(CLIENT_SECRET_PATH);
$client->setAccessType('offline');
if (!file_exists(CREDENTIALS_PATH)) {
throw new \Exception('No credentials set');
}
$accessToken = file_get_contents(CREDENTIALS_PATH);
$client->setAccessToken($accessToken);
if ($client->isAccessTokenExpired()) {
$client->refreshToken($client->getRefreshToken());
file_put_contents(CREDENTIALS_PATH, $client->getAccessToken());
}
$this->client = $client;
$this->service = new \Google_Service_Calendar($client);
}
示例13: setAuthorization
public function setAuthorization()
{
$client = new \Google_Client();
$client->setAccessType('offline');
$client->setApplicationName(GoogleShopping::getConfigValue('application_name'));
$client->setClientId(GoogleShopping::getConfigValue('client_id'));
$client->setClientSecret(GoogleShopping::getConfigValue('client_secret'));
$client->setRedirectUri(URL::getInstance()->absoluteUrl('/googleshopping/oauth2callback'));
$client->setScopes('https://www.googleapis.com/auth/content');
$oAuthToken = GoogleShopping::getConfigValue('oauth_access_token');
$code = $this->getRequest()->query->get('code');
$redirection = '/admin/module/GoogleShopping';
//Manage redirection after auth
if ($url = $this->getRequest()->query->get('redirect')) {
$redirection = $url;
$this->getSession()->set('google_action_url', $redirection);
}
if (isset($oAuthToken)) {
$client->setAccessToken($oAuthToken);
if ($client->isAccessTokenExpired()) {
$client->refreshToken(GoogleShopping::getConfigValue('oauth_refresh_token'));
$newToken = $client->getAccessToken();
GoogleShopping::setConfigValue('oauth_access_token', $newToken);
}
return $this->generateRedirect($redirection);
} elseif (isset($code)) {
$client->authenticate($code);
$token = $client->getAccessToken();
$refreshToken = $client->getRefreshToken();
GoogleShopping::setConfigValue('oauth_access_token', $token);
if ($refreshToken) {
GoogleShopping::setConfigValue('oauth_refresh_token', $refreshToken);
}
return $this->generateRedirect($redirection);
} else {
return $this->generateRedirect($client->createAuthUrl());
}
}
示例14: getClient
/**
* Returns an authorized API client.
* @return Google_Client the authorized client object
*/
function getClient()
{
$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 = $this->expandHomeDirectory(CREDENTIALS_PATH);
//$credentialsPath = '/root/.credentials/calendar-php-quickstart.json';
var_dump(CREDENTIALS_PATH);
var_dump($credentialsPath);
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%s\n", $authUrl);
print 'Enter verification code: ';
//$authCode = '4/baev8vvxfmBSuzvgTG0YGgXf2PsPviyxu2lUnqRN1Es';
$authCode = '4/4FWQ2Ib5_5iTnlwep_Zr_xlbm9UAkh3R6lSI8s-uQAc';
// 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;
}
示例15: generateClient
function generateClient($expectedAccessToken)
{
global $path;
# Get OAuth2 information
$oauthFile = fopen($path . "oauthinfo.json", "r");
$rawJson = fread($oauthFile, filesize($path . "oauthinfo.json"));
$decodedJson = json_decode($rawJson, true)['web'];
# Decode information
$clientID = $decodedJson['client_id'];
# pull from json sheet
$clientSecret = $decodedJson['client_secret'];
# also pull from json sheet
$redirectUri = 'http://localhost/admin.php';
# change this every time we install application, this uri MUST be registed in the google decelopers console
$client = new Google_Client();
$client->setClientId($clientID);
$client->setClientSecret($clientSecret);
$client->setRedirectUri($redirectUri);
$client->setScopes(array('https://spreadsheets.google.com/feeds'));
$client->setAccessType("offline");
if (filesize($path . "accessToken.json") != 0) {
# If accessToken.json has information
$credFile = fopen($path . "accessToken.json", "r");
$accessArray = fread($credFile, filesize($path . "accessToken.json"));
$client->setAccessToken($accessArray);
$client->refreshToken($client->getRefreshToken());
$credFile = fopen($path . "accessToken.json", "w");
fwrite($credFile, $client->getAccessToken());
return $client;
} else {
if ($expectedAccessToken) {
print "Site has been invalidly configured, please contact your webmaster";
exit;
} else {
return $client;
}
}
}