이전에이 패키지를 사용하지 않았지만 google-api-php-client을 사용하고 있습니다.하지만 어쨌든 새로 고침 토큰을 설정하지 않으면이 오류가 발생합니다.
액세스 토큰을 한 번만 가져야한다는 것을 알아야합니다. 또한 액세스 유형을 오프라인으로 설정해야합니다.이 토큰은 액세스 토큰이 만료 될 때마다 새 코드를받지 않고 자동으로 새 액세스 토큰을 얻는 데 사용할 새로 고침 토큰을 제공합니다.
Google의 콘솔에서 웹 애플리케이션의 클라이언트 ID는 입니다. 리디렉션 URI를 코드를 수신 할 웹 페이지에 설정하고 해당 코드를 사용하여 액세스 토큰을 추출해야합니다. 여기
는 google-api-php-client를 사용하여 코드 예제, 나는 그것이 도움이되기를 바랍니다 : 당신은 한 번만 다음 코드를 실행하고 검색하고 액세스 토큰을 저장해야합니다. 위의 코드에서 액세스 토큰을 받고 후에
<?php
require_once('google-api-php-client-master/src/Google/Client.php');
session_start();
$client = new Google_Client();
$client->setApplicationName('APP_NAME');
$client->setClientId(YOUR_CLIENT_ID);
$client->setClientSecret('YOUR_CLIENT_SECRET');
$client->setRedirectUri('YOUR_REDIRECT_URI');
$client->setDeveloperKey('YOUR_DEV_KEY');
$client->setScopes(array('https://www.googleapis.com/auth/analytics.readonly'));
$client->setAccessType("offline");
// Step 1: Create an auth url
if (isset($_GET['ref']) && $_GET['ref'] == "1") {
$authUrl = $client->createAuthUrl();
return Redirect::to($authUrl);
}
// Step 2: The user accepted your access now you need to exchange it.
if (isset($_GET['code'])) {
$client->authenticate($_SESSION['code']); //Authenticate the client
$token = $client->getAccessToken(); //Get the access token
var_dump($token); //Store the token in your DB or config file
die();
}
?>
(새로 고침 토큰을 포함해야하는) 당신의 DB 또는 설정 파일에 저장합니다.
이제 다음 코드는 클라이언트를 인증하고 그것은 서버의 시간이 될 수있는 getAccessToken 기능을 통해 <?php
require_once('google-api-php-client-master/src/Google/Client.php');
require_once('google-api-php-client-master/src/Google/Service/Analytics.php');
$client = new Google_Client();
$client->setApplicationName('APP_NAME');
$client->setClientId(YOUR_CLIENT_ID);
$client->setClientSecret('YOUR_CLIENT_SECRET');
$client->setRedirectUri('YOUR_REDIRECT_URI');
$client->setDeveloperKey('YOUR_DEV_KEY');
$client->setScopes(array('https://www.googleapis.com/auth/analytics.readonly'));
$client->setAccessType("offline"); //Make sure the access type is offline to get a refresh token
$config = CoreConfig::find(1); //Getting the first record from the config table
$client->setAccessToken($config->google_access_token); //Retrieve the access token that you stored and set it to the client object
//Check this the token is expired
if($client->isAccessTokenExpired()) {
$token = json_decode($config->google_access_token, true); //Get the token stored, and convert JSON to array
$client->refreshToken($token['refresh_token']); //Set the refresh token
$newtoken = $client->getAccessToken(); //Call the getAccessToken() function to get a new access token for you
$config->update(array('google_access_token' => $newtoken)); //Store the new token in your DB
}
if ($client->getAccessToken()) {
$analytics = new Google_Service_Analytics($client);
//Do something with the $analytics object
}
?>
패키지의 [업스트림 버전] (https://github.com/thujohn/analytics-l4)을 사용해보십시오. 행운을 누릴 수 있습니다. – luciddreamz
@luciddreamz 나는 이미 최신 버전을 가지고있다. :/ – omarsafwany