如何使用GoogleAPI客户端刷新令牌?我一直在玩GoogleAnalyticsAPI(V3),并且遇到了一些错误。首先,所有的设置都是正确的,并与我的测试帐户一起工作。但是当我想从另一个配置文件ID(相同的Googleaccont/GA帐户)获取数据时,我会得到一个403错误。奇怪的是,来自某些GA帐户的数据将返回数据,而其他帐户将生成此错误。我已经撤销了令牌并再次进行了身份验证,现在我似乎可以从我的所有帐户中获取数据。问题解决了?不。由于访问密钥将过期,我将再次遇到相同的问题。如果我对事情的理解是正确的,人们可以使用新的Token获得一个新的身份验证Tooken。问题是,当我跑:$client->refreshToken(refresh_token_key) 返回以下错误:Error refreshing the OAuth2 token, message: '{ "error" : "invalid_grant" }'我检查了刷新方法背后的代码,并将请求跟踪到“apoAuth2.php”文件中。所有参数都正确发送。Grant_type在方法中被硬编码为“REFRESH_TOKEN”,所以我很难理解出了什么问题。参数数组如下所示:Array ( [client_id] => *******-uqgau8uo1l96bd09eurdub26c9ftr2io.apps.googleusercontent.com [client_secret] => ******** [refresh_token] => 1\/lov250YQTMCC9LRQbE6yMv-FiX_Offo79UXimV8kvwY [grant_type] => refresh_token )程序如下。$client = new apiClient();$client->setClientId($config['oauth2_client_id']);$client->setClientSecret($config['oauth2_client_secret']);$client->setRedirectUri($config['oauth2_redirect_uri']);$client->setScopes('https://www.googleapis.com/auth/analytics.readonly');$client->setState('offline');$client->setAccessToken($config['token']); // The access JSON object.$client->refreshToken($config['refreshToken']); // Will return error here这是一个错误,还是我完全误解了什么?
3 回答
九州编程
TA贡献1785条经验 获得超4个赞
$client->refreshToken($refreshToken)
//pull token from database$tokenquery="SELECT * FROM token WHERE type='original'";$tokenresult = mysqli_query($cxn,$tokenquery);if($tokenresult!=0){ $tokenrow=mysqli_fetch_array($tokenresult); extract($tokenrow);}$time_created = json_decode($token)->created;$t=time();$timediff=$t-$time_created;echo $timediff."<br>";$refreshToken= json_decode($token)->refresh_token;//start google client note:$client = new Google_Client();$client->setApplicationName('');$client->setScopes(array());$client->setClientId('');$client->setClientSecret('');$client->setRedirectUri('');$client->setAccessType('offline');$client->setDeveloperKey('');//resets token if expiredif(($timediff>3600)&&($token!='')){ echo $refreshToken."</br>"; $refreshquery="SELECT * FROM token WHERE type='refresh'"; $refreshresult = mysqli_query($cxn,$refreshquery); //if a refresh token is in there... if($refreshresult!=0) { $refreshrow=mysqli_fetch_array($refreshresult); extract($refreshrow); $refresh_created = json_decode($token)->created; $refreshtimediff=$t-$refresh_created; echo "Refresh Time Diff: ".$refreshtimediff."</br>"; //if refresh token is expired if($refreshtimediff>3600) { $client->refreshToken($refreshToken); $newtoken=$client->getAccessToken(); echo $newtoken."</br>"; $tokenupdate="UPDATE token SET token='$newtoken' WHERE type='refresh'"; mysqli_query($cxn,$tokenupdate); $token=$newtoken; echo "refreshed again"; } //if the refresh token hasn't expired, set token as the refresh token else { $client->setAccessToken($token); echo "use refreshed token but not time yet"; } } //if a refresh token isn't in there... else { $client->refreshToken($refreshToken); $newtoken=$client->getAccessToken(); echo $newtoken."</br>"; $tokenupdate="INSERT INTO token (type,token) VALUES ('refresh','$newtoken')"; mysqli_query($cxn,$tokenupdate); $token=$newtoken; echo "refreshed for first time"; } }//if token is still good.if(($timediff<3600)&&($token!='')){ $client->setAccessToken($token);}$service = new Google_DfareportingService($client);
缥缈止盈
TA贡献2041条经验 获得超4个赞
if (isset($_GET['code'])) { $client->authenticate(); $_SESSION['access_token'] = $client->getAccessToken();}
$google_token= json_decode($_SESSION['access_token']);$client->refreshToken($google_token->refresh_token);
$_SESSION['access_token']= $client->getAccessToken()
- 3 回答
- 0 关注
- 1067 浏览
添加回答
举报
0/150
提交
取消