Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 谷歌分析访问令牌在3600秒(1小时)后过期_Php_Authentication_Terminal_Access Token_Google Analytics Api - Fatal编程技术网

Php 谷歌分析访问令牌在3600秒(1小时)后过期

Php 谷歌分析访问令牌在3600秒(1小时)后过期,php,authentication,terminal,access-token,google-analytics-api,Php,Authentication,Terminal,Access Token,Google Analytics Api,我正在使用Google analytics API(PHP)检索我的帐户信息,我检索refrech令牌时使用了: if (isset($_SESSION['token'])) { // echo $_SESSION['token'].'<br />'; $authObj = json_decode($_SESSION['token']); $accessToken = $authObj->access_token; $refreshToken = $authObj->

我正在使用Google analytics API(PHP)检索我的帐户信息,我检索refrech令牌时使用了:

if (isset($_SESSION['token'])) {

// echo $_SESSION['token'].'<br />';

$authObj = json_decode($_SESSION['token']);
$accessToken = $authObj->access_token;
$refreshToken = $authObj->refresh_token;
$tokenType = $authObj->token_type;
$expiresIn = $authObj->expires_in;

echo 'access_token = ' . $accessToken;
echo '<br />';
echo 'refresh_token = ' . $refreshToken;
echo '<br />';
echo 'token_type = ' . $tokenType;
echo '<br />';
echo 'expires_in = ' . $expiresIn;
echo '<br />';
}
这是完美的,但在告诉我访问令牌已被导出一小时后,它通过了一个错误

我怎么知道访问令牌过期了,我发现了这个,这家伙解决了他的问题,但他没有发布答案。 我如何使用相同的refrech令牌生成一个新的,而不需要再次通过授权过程,因为我正在通过终端运行代码

我在下面贴了一段我项目的片段

谢谢

require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_AnalyticsService.php';
$scriptUri = 'http://domainname.com';

session_start();

$client = new Google_Client();
$client->setApplicationName('Google Analytics project');

$client->setClientId('************.apps.googleusercontent.com');
$client->setClientSecret('S**********************1T');

$client->setRedirectUri($scriptUri);

$client->setDeveloperKey('**************************-1c');
$client->setScopes(array('https://www.googleapis.com/auth/analytics.readonly'));

$client->setAccessType('offline'); 
$client->setUseObjects(true);


if (isset($_GET['code'])) {  
$client->authenticate();  
$_SESSION['token'] = $client->getAccessToken(); 

$redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];  
header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}

if (isset($_SESSION['token'])) {    
$authObj = json_decode($_SESSION['token']);
$accessToken = $authObj->access_token;
$refreshToken = $authObj->refresh_token;
$tokenType = $authObj->token_type;
$expiresIn = $authObj->expires_in;
}


if (!$client->getAccessToken()) {  

$authUrl = $client->createAuthUrl();  
print "<a class='login' href='$authUrl'>Connect Me!</a>";
} else {  
// Create analytics service object. See next step below.
$analytics = new Google_AnalyticsService($client);
runMainDemo($analytics);
}




function runMainDemo(&$analytics) {  

try {    
// Step 2. Get the user's first profile ID.    
$profileId = getProfileId($analytics);    
if (isset($profileId)) {      
    // Step 3. Query the Core Reporting API.      
    $results = getResults($analytics, $profileId);      
    // Step 4. Output the results.      
    printResults($results);    
    }  
} 
catch (apiServiceException $e) {    
// Error from the API.    
print 'There was an API error : ' . $e->getCode() . ' : ' . $e->getMessage();  
} 
catch (Exception $e) {    
print 'There wan a general error : ' . $e->getMessage();  
}
}



function getProfileId(&$analytics) {
    //My code to retrieve data
}
require_once'google-api-php-client/src/google_-client.php';
需要一次“谷歌api php客户端/src/contrib/google_AnalyticsService.php”;
$scriptUri='1!'http://domainname.com';
会话_start();
$client=新的Google_客户端();
$client->setApplicationName(“谷歌分析项目”);
$client->setClientId('************.apps.googleusercontent.com');
$client->setClientSecret('S******************************1T');
$client->setRedirectUri($scriptUri);
$client->setDeveloperKey(“******************************-1c”);
$client->setScopes(阵列)https://www.googleapis.com/auth/analytics.readonly'));
$client->setAccessType('offline');
$client->setUseObjects(true);
如果(isset($_GET['code']){
$client->authenticate();
$\会话['token']=$client->getAccessToken();
$redirect='http://'.$\u服务器['http\u主机].$\u服务器['PHP\u SELF'];
标题('Location:'.filter_var($redirect,filter_SANITIZE_URL));
}
如果(isset($_会话['token']){
$authObj=json_解码($_会话['token']);
$accessToken=$authObj->access\u令牌;
$refreshttoken=$authObj->refresh\u令牌;
$tokenType=$authObj->token\u类型;
$expiresIn=$authObj->expires\u in;
}
如果(!$client->getAccessToken()){
$authUrl=$client->createAuthUrl();
打印“”;
}否则{
//创建分析服务对象。请参阅下面的下一步。
$analytics=新的谷歌分析服务($client);
runMainDemo($analytics);
}
函数runMainDemo(&$analytics){
试试{
//步骤2.获取用户的第一个配置文件ID。
$profileId=getProfileId($analytics);
if(isset($profileId)){
//步骤3.查询核心报告API。
$results=getResults($analytics,$profileId);
//步骤4.输出结果。
打印结果($结果);
}  
} 
捕获(apiServiceException$e){
//来自API的错误。
打印“出现API错误:”。$e->getCode()。:”。$e->getMessage();
} 
捕获(例外$e){
打印“出现一般错误:”。$e->getMessage();
}
}
函数getProfileId(&$analytics){
//我的代码用于检索数据
}

响应包含令牌的有效期。因此,由您存储到期日,并在发出请求之前检查它

在psuedocode中:

$expiry = getDate().now + $expires_in;
//later
if(getDate().now > $expiry) {
   //our token is expired. get a new one
} 
避免在本地再次执行身份验证过程的唯一方法是让服务器代理为您执行身份验证请求,并将结果返回给客户端。通过代理请求,您基本上在某处有一个webapp服务器(heroku、engineyard、ec2、您的后院w/e),您向其发送令牌请求。它反过来向google发出身份验证请求,获取新的令牌,并将结果返回给您

这一点的全部目的是为了安全起见,迫使您定期重新授权。如果有办法绕过它,它就不会很安全。

这是我的解决方案

function getProfileId(&$analytics) {
    
      for ($i=0;i<10;$i++){
           
             **My code to retrieve data**
             // I called the method 'refreshToken' as following

             $GLOBALS['client']->refreshToken($GLOBALS['refreshToken']); 
       }
}
函数getProfileId(&$analytics){
对于($i=0;irefreshToken($GLOBALS['refreshToken']);
}
}
$GLOBALS用于调用外部变量,有关$GLOBALS如何工作的更多信息,请查看此项


因此,每次我通过循环时,我都会生成一个新的访问令牌,这样我就永远不会用完访问令牌。

嗨,艾伦,你的代码看起来很棒!我应该把你的代码放在哪里,以及我应该放在if语句括号中的内容?谢谢!这是伪代码,而不是真实代码。在if语句中,你需要再打一次电话给谷歌以获取新的访问令牌代币
function getProfileId(&$analytics) {
    
      for ($i=0;i<10;$i++){
           
             **My code to retrieve data**
             // I called the method 'refreshToken' as following

             $GLOBALS['client']->refreshToken($GLOBALS['refreshToken']); 
       }
}