MSN庞大的客户群体是不可忽视的,虽然Live有OpenID,但是很少有人会去开通,并且记得住的不多。记得上次有人在他的BLOG上说很多上网的人其实很白痴,连登录Gmail都要用hao123。包括在我使用OpenID的网站(OpenTags.org)上,真正使用OpenID登录的并不多,Google、Yahoo、Live的用户才多。Google、Yahoo虽然支持OpenID,但是都被阉割了,只是有选择的支持sreg。关于Google、Yahoo,下次再说,今天说Windows Live ID。

Windows Live ID,注意是ID不是OpenID,如果使用Login的方式会得到一个userid,但是这个userid只是一个识别符号,不是真正的Live ID,这个userid只能在你的网络识别Live ID,即一个appid对应一个userid,如果换了appid,即换站userid也跟着变。如果要再作第三方的认证就不合适了。我这里介绍的是取得真正的Live ID,即通过获取Live ID的联系人信息来获取Live ID。

如果要Login方式的userid可以不用再往下面看了,说一下获取Login方式的userid很简单,下载SDK包,然后里面有个sample,看下就懂了,记得要修改Application-Key.xml。

要取得真正的Live ID,按下列步骤即可:

申请(New Project >> Live Services: Existing APIs)


下载 Windows Live ID Web Authentication SDK 1.2 webauth-php-1.2.tar.gz

修改Application-Key.xml


$LoginUrl = '登录地址';
$AuthUrl = '验证地址';
$RealmUrl = '退出后返回的地址';

$DEBUG = true;
$KEYFILE = './Live/Application-Key.xml';
$COOKIE = 'webauthtoken';
$COOKIETTL = time() + (10 * 365 * 24 * 60 * 60);

include_once('./Live/lib/windowslivelogin.php');
$wll = WindowsLiveLogin::initFromXml($KEYFILE);
$wll->setDebug($DEBUG);

if(!empty($_REQUEST['action'])){
switch($_REQUEST['action']) {
case 'logout':
setcookie($COOKIE);
$LocationUrl = $RealmUrl;
break;
case "clearcookie":
setcookie($COOKIE);
$LocationUrl = $AuthUrl;
break;
default:
$user = $wll->processConsent($_REQUEST);
if(!empty($user)){
setcookie($COOKIE, $user->getToken(), $COOKIETTL);
$LocationUrl = $AuthUrl;
}else{
$LocationUrl = $LoginUrl;
}
break;
}
LocationHtml($LocationUrl);
}
$token = $_COOKIE[$COOKIE];
if($token){
$user = $wll->processConsentToken($token);
if(!$user->isValid()){
$LocationUrl = $AuthUrl.'?AuthType=Live&action=clearcookie';
}else{
$url = 'https://livecontacts.services.live.com/users/@L@'.$user->getLocationID().'/rest/livecontacts/owner';
$owner = getLiveID($url, array('Authorization: DelegatedToken dt="'.$user->getDelegationToken().'"'));
$openid = $owner->WindowsLiveID;
$email = $owner->WindowsLiveID;
$fullname = $owner->Profiles->Personal->LastName.' '.$owner->Profiles->Personal->FirstName;
$nickname = $owner->Profiles->Personal->DisplayName;
echo('<pre>');
print_r($owner);
echo('</pre>');
}
}else{
$LocationUrl = $wll->getConsentUrl('Contacts.View', 'zh-CN');
}


function getLiveID($url, $header){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);
try{
$xml = new SimpleXMLElement($data);
}catch(Exception $e){
return false;
}
return $xml;
}

function LocationHtml($url, $e=null){
if(empty($url))
exit;
echo('<html>');
echo('<head>');
echo('<title>Redirect to: '.$url.'</title>');
echo('<meta http-equiv="refresh" content="1;url='.$url.'">');
echo('</head>');
echo('<body bgcolor="#FFFFFF" onload="window.location.href=\''.$url.'\'">');
echo('<table border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF" height="100%" width="100%">');
echo('<tr>');
echo('<td align="center" valign="middle">');
echo('<a href="'.$url.'" style="color:#000000;text-decoration:none">');
echo($url);
echo('</a>');
echo('</td>');
echo('</tr>');
echo('</table>');
if(!empty($e)){
echo('<script type="text/javascript">');
if(is_array($e))
echo('alert("'.implode('\n', $e).'");');
else
echo('alert("'.$e.'");');
echo('</script>');
}
echo('</body>');
echo('</html>');
exit;
}

注意:getLiveID要CURL支持,如果不支持请用其他办法。

8 Comments

Leave a Reply