1

Google 애널리틱스의 경우 Analytics-Laravel 4으로이 패키지를 사용 중이며 모든 단계가 올바르게 수행됩니다. 내가 두 번 구성, 클라이언트 ID, service_account 및 개인 키하지만 여전히이 발생하는 오류를 모두 검사 한OAuth2 토큰을 새로 고침하는 중 오류가 발생했습니다 : '{ "오류": "invalid_grant"}'

Error refreshing the OAuth2 token, message: '{ "error" : "invalid_grant" }' 

: 나는 예를 들어 사이트 ID를 얻을 수하려고 할 때이 오류에 직면 해있다. 내가이 문제를 해결할 수 있는지 확인해야하는 사항은 무엇입니까?!

+0

패키지의 [업스트림 버전] (https://github.com/thujohn/analytics-l4)을 사용해보십시오. 행운을 누릴 수 있습니다. – luciddreamz

+0

@luciddreamz 나는 이미 최신 버전을 가지고있다. :/ – omarsafwany

답변

1

이전에이 패키지를 사용하지 않았지만 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 
} 

?> 
1

를 만료되면 액세스 토큰을 새로해야한다. 서버의 로컬 시간이 Google의 oAuth 서버와 몇 초 차이가 나면 오류 메시지가 표시됩니다.

콘솔에서 "날짜"를 실행하여 시간을 확인할 수 있습니다.

"sudo ntpdate ntp.ubuntu.com"을 실행하면 우리를 위해 해결되었습니다.

+0

또한 "$ now = time()"을 "$ now = time() - $ offset"으로 변경하여 소스의 AssertionCredentials.php에서 시간을 조정할 수 있지만 소스에 대한 모든 업데이트로 덮어 쓰게됩니다 – nbarth

+0

나는 당신이 말한 것을 할 계획이지만, 나는 당신이'$ offset '을 어디서 얻었는지 궁금합니다. – GianFS

관련 문제