was successfully added to your cart.

Oauth 2.0 de Google API Client Services

La respuesta a vuestras preguntas está en esta URL:

https://developers.google.com/oauthplayground

Aquí obtendréis el access_token y el refresh_token.

Ahora, con el siguiente código:

header('Content-type: text/html; charset=utf-8');	
include_once(__DIR__.'/../init.php');
session_start();
// Create the client object and set the authorization configuration
// from the client_secretes.json you downloaded from the developer console.
$client = new Google_Client();
$client->setAuthConfig(__DIR__.'/../config/analytics-file-oauth.json');

//Permisos de ficheros.
$client->addScope(Google_Service_Analytics::ANALYTICS_READONLY);

//Tipo de acceso
$client->setAccessType("offline");
$client->setIncludeGrantedScopes(true);

//Cogemos nuestro access_token.
$client->setAccessToken(file_get_contents(__DIR__.'/../config/accesstoken'));

//Verificamos si está caducado.
if ($client->isAccessTokenExpired()) {    
  	$refreshToken = file_get_contents(__DIR__.'/../config/refreshtoken');
  	$client->refreshToken($refreshToken);
  	unlink(__DIR__.'/../config/accesstoken');
	touch(__DIR__.'/../config/accesstoken');
	file_put_contents(__DIR__.'/../config/accesstoken', $client->getAccessToken());
}

$analytics = new Google_Service_Analytics($client);

// Get the first view (profile) id for the authorized user.
$profile = getFirstProfileId($analytics);

// Get the results from the Core Reporting API and print the results.
$results = getResults($analytics, $profile);
printResults($results);

Leeros un poco el codigo para entender donde están guardados el accesstoken y el refreshtoken.

Con este código refrescareis el access_token para poder hacer las consultas que queráis cuando queráis.

Saludos.

 

David Escudero | Programador freelance, desarrollador paginas web
Resumen de privacidad

Esta web utiliza cookies para que podamos ofrecerte la mejor experiencia de usuario posible. La información de las cookies se almacena en tu navegador y realiza funciones tales como reconocerte cuando vuelves a nuestra web o ayudar a nuestro equipo a comprender qué secciones de la web encuentras más interesantes y útiles.