H
H
HAbRAhabp2016-02-19 16:53:56
PHP
HAbRAhabp, 2016-02-19 16:53:56

How to make Google token forever?

How to make a token permanent during authorization? Or at least more than 30 minutes? How do I authorize:

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');leads to nothing.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nazar Mokrinsky, 2016-02-19
@HAbRAhabp

I believe that in any way, for this OAuth2 was invented.
When an access_token dies use refresh_token to get a new access_token.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question