使用 Google API 客户端刷新访问令牌
与 Google Analytics API (V3) 交互时,处理令牌过期至关重要访问数据。 Google API 客户端提供了refreshToken() 方法来使用刷新令牌获取新的访问令牌。但是,尝试使用此方法时,您可能会遇到“invalid_grant”错误。
了解令牌过期
访问令牌的寿命有限,通常为一小时。令牌过期后,必须获取新的访问令牌。 freshToken() 方法可用于检索新的访问令牌。
调试“invalid_grant”错误
“invalid_grant”错误表示刷新令牌正在使用的内容无效或已过期。要解决此问题,请验证以下内容:
代码示例
下面是一个简化的示例,演示如何刷新访问令牌并将其存储在数据库中:
<?php use Google\Client; use Google\Service\Analytics; // Set up your client and credentials $client = new Client(); $client->setClientId('YOUR_CLIENT_ID'); $client->setClientSecret('YOUR_CLIENT_SECRET'); $client->setRedirectUri('YOUR_REDIRECT_URI'); $client->setScopes('https://www.googleapis.com/auth/analytics.readonly'); $client->setState('offline'); // Retrieve the original access token (with a refresh token) from your database $original_access_token = json_decode($token, true); $refresh_token = $original_access_token['refresh_token']; // Check if the original access token has expired $time_created = $original_access_token['created']; $time_now = time(); $time_diff = $time_now - $time_created; // Refresh the access token if it has expired if ($time_diff > 3600) { // Check if a refresh token exists in the database $refresh_token_query = "SELECT * FROM token WHERE type='refresh'"; $refresh_token_result = mysqli_query($cxn, $refresh_token_query); // If a refresh token exists, use it to get a new access token if ($refresh_token_result != 0) { $refresh_token_row = mysqli_fetch_array($refresh_token_result); $refresh_token_created = json_decode($refresh_token_row['token'], true)['created']; $refresh_token_time_diff = $time_now - $refresh_token_created; // If the refresh token has expired, update it if ($refresh_token_time_diff > 3600) { $client->refreshToken($refresh_token); $new_access_token = $client->getAccessToken(); // Update the refresh token in the database $refresh_token_update_query = "UPDATE token SET token='$new_access_token' WHERE type='refresh'"; mysqli_query($cxn, $refresh_token_update_query); // Update the original access token in the database $original_access_token_update_query = "UPDATE token SET token='$new_access_token' WHERE type='original'"; mysqli_query($cxn, $original_access_token_update_query); } else { // Use the existing refresh token to get a new access token $client->refreshToken($refresh_token); $new_access_token = $client->getAccessToken(); // Update the original access token in the database $original_access_token_update_query = "UPDATE token SET token='$new_access_token' WHERE type='original'"; mysqli_query($cxn, $original_access_token_update_query); } } else { // If a refresh token does not exist, retrieve a new one $client->refreshToken($refresh_token); $new_access_token = $client->getAccessToken(); // Insert the new refresh token into the database $refresh_token_insert_query = "INSERT INTO token (type, token) VALUES ('refresh', '$new_access_token')"; mysqli_query($cxn, $refresh_token_insert_query); // Update the original access token in the database $original_access_token_update_query = "UPDATE token SET token='$new_access_token' WHERE type='original'"; mysqli_query($cxn, $original_access_token_update_query); } } // Use the new or refreshed access token to make API calls $service = new Analytics($client);
以上是如何解决刷新 Google Analytics API 访问令牌时出现的'invalid_grant”错误?的详细内容。更多信息请关注PHP中文网其他相关文章!