技术开发 频道

SNS网站中怎样获取MSN联系人信息

  第二步:

  下载 Windows Live ID Delegated Authentication SDK 1.2 DEMO,网址为:http://www.microsoft.com/downloads/details.aspx?FamilyId=A2466ABF-9629-42D8-B991-1D3FAF2FE872&displaylang=en

  其中有C#、Java、Perl、PHP、Python、Ruby、VB等多种版本任你选择。

  第三步:

  安装下载的文件到指定目录内,默认为C:\Program Files\Windows Live ID\DelAuth

  第四步:

  在IIS中新建一个虚拟目录,名称为DelAuth

  第五步:

  修改hosts文件, 你可以修改一下C:\WINDOWS\system32\drivers\etc中的hosts文件,把127.0.0.1映射到一个您刚才申请Project时填写的域名 www.dpe.com

  第六步:

  修改Sample1中的Web.Config文件,如下,用申请来的ApplicationID和Secret Key替换文件中相应的值,并根据你的主机及虚拟目录名称,把ReturnUrl改为http://www.dpe.com/delAuth/sample1/delauth-handler.aspx ,Policyurl的值改为http://www.dpe.com/delAuth/sample1/policy.html ,原始的web.Config文件如下:

  第七步:

  把Sample1中的Default.aspx设置为首页,按如下界面中的URL打开网址

  其中Click Here中的URL是动态构造的,如果有兴趣,相应的构造代码大家可以研究一下。

  点击Click Here就转向Live网站进行登录,登录后自动返回到Web.Config中配置的returnUrl网址,用户登录后,我们可以通过代码取得

  WindowsLiveLogin.ConsentToken的值,具体代码如下:

WindowsLiveLogin wll = new WindowsLiveLogin(true);
WindowsLiveLogin.ConsentToken token
= wll.ProcessConsent(req.Form);

 

  第八步:

  取得MSN中的联系人信息

  微软提供了允许我们通过REST方式访问contracts服务,需要具备以下两个参数:

  The Delegated Authentication token (DAT),即WindowsLiveLogin.ConsentToken

  The Location ID (lid),可以通过WindowsLiveLogin.ConsentToken获得

  显然,我们前七步做的工作主要是获得以上的两个参数的值,用户登录后,在returnUrl页面中,我们可以写如下代码

public string GetContacts(WindowsLiveLogin.ConsentToken ct)
{
string lid = ct.LocationID;
string delegatedToken = ct.DelegationToken;
// Construct the request URI.
string uri = "https://livecontacts.services.live.com/@L@" + lid + "/rest/LiveContacts/Contacts/";
HttpWebRequest request
= (HttpWebRequest)WebRequest.Create(uri);
request.UserAgent
= "Windows Live Data Interactive SDK";
request.ContentType
= "application/xml; charset=utf-8";
request.Method
= "GET";
// Add the delegation token to a request header.
request.Headers.Add("Authorization", "DelegatedToken dt=\"" + delegatedToken + "\"");
//Issue the HTTP GET request to Windows Live Contacts.
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
//The response body is an XML stream. Read the stream into an XmlDocument.
XmlDocument contacts = new XmlDocument();
contacts.LoadXml(
new StreamReader(response.GetResponseStream()).ReadToEnd());
//Use the document. For example, display contacts.InnerXml.
return contacts.InnerXml;
//Close the response.
//response.Close();
}

 

  我们可以针对返回的XML字符串进行分析,获取你需要的Contact格式,并加以利用。

  附件中的代码包完整实现了这一功能,大家可以下载

  http://files.cnblogs.com/lichl/DelAuth.rar

0
相关文章