Authentication 如何存储google api(OAuth 2)权限?
我正在使用“GoogleAPI php客户端”库中提供的示例(http://code.google.com/p/google-api-php-client/)使用谷歌服务在我的网站上实现用户登录和授权。 除了添加我的客户ID等,我没有对示例进行任何更改 授权本身工作正常:用户可以登录,我可以获取提供的信息。 但是,当离开页面时,将再次调用整个授权过程;用户不会被记住,需要再次授予权限,这是一种恼人的行为,我所知道的谷歌登录并不典型 例如:在stackoverflow上,我使用我的google帐户登录。 每当我再次访问这个网站时,我都会自动登录,或者(如果注销)只需再次登录-我不必再次确认一般权限。 但是,使用我的站点上的示例,会强制用户在再次访问站点时允许访问 在使用示例时,我是否犯过错误? 我该怎么做才能避免一次又一次的许可请求Authentication 如何存储google api(OAuth 2)权限?,authentication,login,google-api,oauth-2.0,Authentication,Login,Google Api,Oauth 2.0,我正在使用“GoogleAPI php客户端”库中提供的示例(http://code.google.com/p/google-api-php-client/)使用谷歌服务在我的网站上实现用户登录和授权。 除了添加我的客户ID等,我没有对示例进行任何更改 授权本身工作正常:用户可以登录,我可以获取提供的信息。 但是,当离开页面时,将再次调用整个授权过程;用户不会被记住,需要再次授予权限,这是一种恼人的行为,我所知道的谷歌登录并不典型 例如:在stackoverflow上,我使用我的google帐户
提前感谢您的帮助 Google Drive SDK文档包括一个完整的PHP示例应用程序,您可以将其用作入门参考:
基本上,一旦用户登录并检索access token和refresh token,您就可以将这些凭据存储在数据库中并重新使用,而不是每次都要求用户进行身份验证。首次使用此代码检索access_代码并将其保存到数据库:
<?php
require 'google-api-php-client/src/Google_Client.php';
require 'google-api-php-client/src/contrib/Google_DriveService.php';
require 'google-api-php-client/src/contrib/Google_Oauth2Service.php';
session_start();
$client = new Google_Client();
$client->setClientId(CLIENT_ID);
$client->setClientSecret(CLIENT_SECRET);
$client->setRedirectUri(REDIRECT_URI);
$client->setScopes(array(
'https://www.googleapis.com/auth/drive',
'https://www.googleapis.com/auth/userinfo.email',
'https://www.googleapis.com/auth/userinfo.profile'));
$client->setUseObjects(true);
$service = new Google_DriveService($client);
$client->authenticate();
$_SESSION['token'] = $client->getAccessToken();
const ACCESS_TOKEN=$_SESSION['token'];
//code here to save in database
?>
一旦ACCESS_令牌保存在数据库中,将代码更改为:
<?php
require 'google-api-php-client/src/Google_Client.php';
require 'google-api-php-client/src/contrib/Google_DriveService.php';
require 'google-api-php-client/src/contrib/Google_Oauth2Service.php';
session_start();
$client = new Google_Client();
$client->setClientId(CLIENT_ID);
$client->setClientSecret(CLIENT_SECRET);
$client->setRedirectUri(REDIRECT_URI);
$client->setScopes(array(
'https://www.googleapis.com/auth/drive',
'https://www.googleapis.com/auth/userinfo.email',
'https://www.googleapis.com/auth/userinfo.profile'));
$client->setUseObjects(true);
$service = new Google_DriveService($client);
//ACCESS_TOKEN is already saved in database, is being saved on first time login.
$_SESSION['access_token'] = ACCESS_TOKEN;
if (isset($_SESSION['access_token'])) {
$client->setAccessToken($_SESSION['access_token']);
}
if ($client->getAccessToken())
{
$userinfo = $service->about->get();
echo '<script>console.log('.json_encode($userinfo).');</script>';
$userinfoService = new Google_OAuth2Service($client);
$user = $userinfoService->userinfo->get();
echo '<script>console.log('.json_encode($user).');</script>';
}
?>
这对我来说很好。
根据考沙尔的回答:
<?php
require_once 'globals.php';
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_DriveService.php';
$client = new Google_Client();
// Get your credentials from the APIs Console
$client->setClientId('YOUR_ID');
$client->setClientSecret('YOUR_SECRET');
$client->setRedirectUri('REDIRECT_URI');
$client->setScopes(array('https://www.googleapis.com/auth/drive'));
$service = new Google_DriveService($client);
$client->setUseObjects(true);
//if no token in the session
if ($_SESSION['google_token'] == '') {
//get stored token from DB
$sToken = $oDb->getOne("SELECT `google_token` FROM `users` WHERE `u_id` = " . (int)$_SESSION['user_id']);
//if no stored token in DB
if ($sToken == '') {
//autentificate user
$client->authenticate();
//get new token
$token = $client->getAccessToken();
//set token in session
$_SESSION['google_token'] = $token;
// set token in DB
$oDb->Query("UPDATE `users` SET `google_token`='$token' WHERE `u_id` = " . (int)$_SESSION['user_id']);
} else {
$_SESSION['google_token'] = $sToken;
}
}
$client->setAccessToken($_SESSION['google_token']);
//do what you wanna do with clients drive here
?>
谢谢你,克劳迪奥!这个链接看起来很有趣。看来,我需要一些“傻瓜代币”-指南:-)同时我发现,$client->setAccessType(“在线”)$客户端->设置批准提示(“自动”)代码>解决了问题的一部分。我尝试将令牌客户端存储为cookie,但没有成功…在此服务器上找不到请求的URL。如果我想存储刷新令牌,因为我想在用户未登录时在后台调用API,这将如何改变情况。