我尝试使用Google oauth在我的Android应用上验证用户身份.然后我想将它发送到我的应用服务器,以便它可以随时与Google日历连接.
我试着用
GoogleAuthUtil.getToken(getApplicationContext(), mAccountName, mScope);
关注本文:https: //developers.google.com/accounts/docs/CrossClientAuth
当我将其与范围一起使用时,
mScope = "oauth2:https://www.googleapis.com/auth/calendar https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile";
我会获得一个令牌,该令牌有效一小时
但是当我尝试获取授权代码时(因此我可以获得一个有效期较长的刷新令牌,使用
mScope2 ="oauth2:server:client_id:{CLIENT_ID}.apps.googleusercontent.com"+ ":api_scope:https://www.googleapis.com/auth/calendar https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile";
我收到"invalid_scope"或"Unknown"异常.
我究竟做错了什么?
编辑:好的,在谷歌API控制台上创建一个新的应用程序并将plus.login添加到范围后我得到一个代码,但由于某种原因我的服务器无法解析此令牌.绑定解析服务器时会收到有关重定向URL的错误.顺便说一句,当我使用相同的参数进行Web流程时,它可以工作.
我几乎搜索了谷歌第一页的每一个结果.但似乎无法找到答案.我正在使用Google API的refresh_token并接收:
Error refreshing the OAuth2 token, message: '{ "error" : "invalid_grant" }
Run Code Online (Sandbox Code Playgroud)
我在做什么 首先:我正在创建并存储与google api的持久连接:
$client = new Google_Client();
$client->setClientId('xxxxxx-s73q0pa41aq3i2kcpatmpas6e6kkp99h.apps.googleusercontent.com');
$client->setClientSecret('xxxxxxxxxxxx');
$client->setRedirectUri('http://xxxxxxxx/generaterefreshtoken.php');
$client->setScopes(array('https://www.googleapis.com/auth/drive'));
$client->setAccessType('offline');
if (isset($_GET['code'])) {
$client->authenticate();
$_SESSION['token'] = $client->getAccessToken();
$redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}
if (isset($_SESSION['token'])) {
$client->setAccessToken($_SESSION['token']);
}
if (isset($_REQUEST['logout'])) {
unset($_SESSION['token']);
$client->revokeToken();
}
if ($client->getAccessToken()) {
$jsonarray = json_decode($client->getAccessToken());
$arrGoogleAuth['access_token']=$jsonarray->access_token;
$arrGoogleAuth['refresh_token']=$jsonarray->refresh_token;
//filewrite
$myFile = "refreshtoken.conf";
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, $client->getAccessToken());
fclose($fh);
/* …Run Code Online (Sandbox Code Playgroud)