@HAbRAhabp

Как сделать токен Google вечным?

Как сделать токен при авторизации вечным? или хотя бы большим чем 30 минут? Как я авторизую:
require_once 'Google/autoload.php';
require_once 'Google/Client.php';
require_once 'Google/Service/AdSense.php';


// Configure token storage on disk.
// If you want to store refresh tokens in a local disk file, set this to true.
define('STORE_ON_DISK', true, true);
define('TOKEN_FILENAME', 'tokens.dat', true);

// Set up authentication.
$client = new Google_Client();
$client->addScope('https://www.googleapis.com/auth/adsense.readonly');
$client->setAccessType('offline');

// Be sure to replace the contents of client_secrets.json with your developer
// credentials.
$client->setAuthConfigFile('client_secrets.json');

if (isset($_GET['code'])) {
  $client->authenticate($_GET['code']);
  // Note that "getAccessToken" actually retrieves both the access and refresh
  // tokens, assuming both are available.
  $_SESSION['access_token'] = $client->getAccessToken();
  if (STORE_ON_DISK) {
    file_put_contents(TOKEN_FILENAME, $_SESSION['access_token']);
  }
  $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
  header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
  exit;
}
if (STORE_ON_DISK && file_exists(TOKEN_FILENAME) &&
      filesize(TOKEN_FILENAME) > 0) {
  // Note that "setAccessToken" actually sets both the access and refresh token,
  // assuming both were saved.
  $client->setAccessToken(file_get_contents(TOKEN_FILENAME));
} else {
  // If we're doing disk storage, generate a URL that forces user approval.
  // This is the only way to guarantee we get back a refresh token.
  if (STORE_ON_DISK) {
    $client->setApprovalPrompt('force');
  }
  $authUrl = $client->createAuthUrl();
}

// Create service.
try {
$service = new Google_Service_AdSense($client);
} catch (\Google_Auth_Exception $e) {
    header("Location: ".$client->createAuthUrl());
    die();
}
// дальнейшие манипуляции с апи адсенса

$client->setAccessType('offline'); ни к чему не приводит.
  • Вопрос задан
  • 984 просмотра
Решения вопроса 1
nazarpc
@nazarpc
Open Source enthusiast
Полагаю что никак, для того OAuth2 и придуман.
Когда access_token умирает используйте refresh_token для получения нового access_token.
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы