我需要我的用戶已經註銷後從谷歌Analytics(分析)獲取數據。基本上他們會去我的網站,授權我的應用程序訪問他們的GA數據,並在午夜時分獲取數據並在我的服務中處理它。
我目前的流動
這裏讀書很多的任何訊息和問題之後就是我想出了:
前端部分看起來有點這樣的:
<script src="https://apis.google.com/js/client:platform.js?onload=start" async defer></script>
<script>
function start() {
gapi.load('auth2', function() {
auth2 = gapi.auth2.init({
client_id: '{{CLIENT_ID}}.apps.googleusercontent.com',
scope: 'https://www.googleapis.com/auth/analytics',
access_type: 'offline'
});
});
}
$('#signinButton').click(function() {
auth2.grantOfflineAccess({
'redirect_uri': 'postmessage'
}).then(signInCallback);
});
function signInCallback(authResult) {
window.console.log('signInCallback: This code must be exchanged by the server, it grants access to request a refresh token: ',authResult, arguments);
if (authResult['code']) {
// Hide the sign-in button now that the user is authorized, for example:
$('#signinButton').attr('style', 'display: none').after('<p>The auth code is: ' + authResult['code'] + '</p>');
$.get('http://mysite.dev/oauth_callback.php?code=' + authResult['code'])
.fail(function(err){console.error(err)})
.done(function(data) {
var message;
if (typeof data.error == 'undefined') {
message = '<div style="border: green solid 1px;">Finished the request and got ' + data + '</div>';
} else {
message = '<div style="border: red solid 1px;">' + data.message + '</div>';
}
$('#signinButton').after(message);
});
} else {
// There was an error.
}
}
</script>
我oauth_callback.php
檢查來自谷歌的響應並將其存儲在數據庫中(請忽略$ .get和$ post位)。
從我的測試中,我發現auth_code以及refresh_token和其他部分都正確存儲在我的數據庫中。現在
,我手動觸發我的午夜腳本看起來有點像這樣:
public function get_client() {
// Create and configure a new client object.
$client = new \Google_Client();
$path_to_config = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'my-client-secret.apps.googleusercontent.com.json';
$scopes = array(
'https://www.googleapis.com/auth/analytics'
);
$client->setAuthConfigFile($path_to_config);
$client->setApplicationName("My app");
$client->setAccessType('offline');
return $client;
}
public function fetch_visits() {
foreach ($this->_accounts as $account_id => $auth_json_string) {
$client = $this->get_client();
$ga_code_array = json_decode($auth_json_string, true);
$ga_code_string = $auth_json_string;
$client->setAccessToken($auth_json_string);
var_dump($client);
if ($client->isAccessTokenExpired()) {
$client->setAccessType('offline');
var_dump('Not authed because the access token expired. Requesting another one');
$client->refreshToken($auth_json_string);;
$is_authed_now = $client->isAccessTokenExpired();
var_dump($is_authed_now);
}
die('sadasdsdsa');
}
}
錯誤
有了這個代碼即時得到臭名昭著: Google_Auth_Exception: Error refreshing the OAuth2 token, message: '{ "error" : "invalid_grant""
錯誤,我追蹤到OAuth庫,基本上谷歌返回一個HTTP代碼400(而不是200),它告訴我$ auth_json_string是不正確的,但這是谷歌在用戶授權我的應用程序後給我的字符串,所以我不真的知道問題在哪裏。
請記住,這是一個服務器到服務器的應用程序,當我的服務運行時用戶不會登錄,這就是爲什麼我存儲auth json對象。
你能看到我在做什麼錯嗎?
看看我的答案在這裏 - http://stackoverflow.com/a/35638759/468862 - 讓我知道是否有幫助。 但是,您可能需要使用刷新令牌而不是訪問令牌,因爲訪問令牌只能持續一個小時 - 因此是錯誤。 此外,我將不得不找到一個鏈接到文檔,但我敢肯定你不能使用JS庫來獲得離線訪問。再次看看我的答案,它是服務器端。 –