我正在开发一个需要经常访问Google数据API的网络应用,因此我决定使用“OAuth with Federated Login(混合协议)”方法让用户登录该应用。我得到了http://googlecodesamples.com/hybrid/工作(经过一些调整以便兼容PHP 5.3),并且能够获得访问令牌。下一步是什么?我如何使用此访问令牌?
似乎我需要为用户创建一个本地会话来浏览应用程序的其余部分。这需要完全独立于Google登录,或者您将如何处理?
相关:此应用程序还需要一个REST API,我计划使用OAuth。关于如何将其与实际应用程序的身份验证联系起来的任何建议?
答案 0 :(得分:8)
我正在使用PHP LightOpenID库(see on gitorious)。它为我们处理所有身份验证流程。你不需要打扰令牌和东西。
此处显示“使用Google登录”链接的页面:
<?php
require_once 'openid.php';
$openid = new LightOpenID;
$openid->identity = 'https://www.google.com/accounts/o8/id';
$openid->required = array('contact/email');
$openid->returnUrl = 'http://my-website.com/landing-login.php'
?>
<a href="<?php echo $openid->authUrl() ?>">Login with Google</a>
点击链接时,会出现一个Google页面,要求他进行身份验证和/或授权您检索他的电子邮件。
然后他将重定向到着陆页$openid->returnUrl
。该页面的代码应为:
<?php
require_once 'openid.php';
$openid = new LightOpenID;
if ($openid->mode) {
if ($openid->mode == 'cancel') {
// User has canceled authentication
} elseif($openid->validate()) {
// Yeah !
$data = $openid->getAttributes();
$email = $data['contact/email'];
} else {
// The user has not logged in via Google
}
} else {
// The user does not come from the link of the first page
}
?>
如果您想从用户那里检索更多信息,您必须在第一页中将它们添加到$openid->required
。例如:
$openid->required = array(
'contact/email',
'namePerson/first',
'namePerson/last'
);
如果用户接受,将允许您在第二页中获取他的名字和姓氏:
$name = $data['namePerson/first'] . " " . $data['namePerson/last'];
然后,对于Oauth部分,您可以按照this LightOpenID issue的说明进行操作。