我正在使用PHP LightOpenID库(see on gitorious)。它为我们处理所有的认证流程。你不需要打扰令牌和东西。
这里,我显示链接“与谷歌登录”页面:
<?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>
当链路上的点击,谷歌的页面会出现要求他进行认证和/或授权你找回他的电子邮件。
然后他将重定向到登陆页面$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的说明操作。