我需要我的用户已经注销后从谷歌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库来获得离线访问。再次看看我的答案,它是服务器端。 –