前段时间项目中要实现一个类似开心网的发送邮箱邀请好友的功能,要读取各邮箱的通讯录及MSN好友列表,在网上查阅了一些资料,也找了一些源码,但都没有读取到。现已实现,拿出来同大家一起分享,提供给项目中有这种需要的朋友使用。
帖子链接:http://topic.csdn.net/u/20100203/15/1204be1a-6225-4168-9ce8-9491555f4e13.html
microsoft提供了msn的api(windows live contact api),在MSDN上只有1.0的版本http://msdn.microsoft.com/en-us/library/bb463982.aspx的说明文档,现在都用不了,应该升级了api,
后来看了一上http://dev.live.com/contacts/(windows live)上的api,根据它的说明文档按步就班能读取到msn好友列表,具体实现的方法如下:
第一步:申请一个微软云计算的一个Project,访问https://lx.azure.microsoft.com/Cloud/Provisioning/Default.aspx,申请一个类型为Live Services的Project,
Application ID: 0000000048011C34
Domain(s): www.example.com 
Return URL: http://www.example.com/
Secret Key: nazgnMZRWbbgaleiDpvUhG69FKBb5L2v
第二步:下载 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.example.com
第六步:修改Sample1中的Web.Config文件,如下,用申请来的ApplicationIDSecret Key替换文件中相应的值,并根据你的主机及虚拟目录名称,把ReturnUrl改为http://www.dpe.com/delAuth/sample1/delauth-handler.aspx Policyurl的值改为http://www.dpe.com/delAuth/sample1/policy.html
第七步:把Sample1中的Default.aspx设置为首页,按如下界面中的URL打开网址

其中Click Here中的URL是动态构造的。
点击Click Here就转向Live网站进行登录,登录后自动返回到Web.Config中配置的returnUrl网址,用户登录后,我们可以通过代码取得
WindowsLiveLogin.ConsentToken的值,具体代码如下:

C# code

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页面中,我们可以写如下代码

C# code

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;

}


到这里就已经完成了获取MSN好友列表的功能,返回xml文件。申请application ID和key时好像有时效性,还在找寻其它的解决方案。 后来,经朋友的指导,在google的code下有msn机器人源码
msn Protocol说明:http://www.hypothetic.org/docs/msn/
google code上的msn机器人源码:http://code.google.com/p/msnp-sharp/
把winform改成web后的源码下载链接:http://download.csdn.net/source/2055373
完整版本,整合了包括读取邮箱通讯录、MSN好友列表的的功能,目前读取邮箱通讯录支持如下邮箱:gmail、hotmail、live、tom、yahoo、sina、163、126、yeah、sohu,源码下载地址(刚上传上去):http://liuyun1987.download.csdn.net/
以上资源均已通过测试,能正确的返回结果。拿出来同大家一起分享,在项目中有类似需求的朋友你们可以拿过来借鉴一下,谢谢!

posted on 2010-09-07 14:03  freedom831215  阅读(619)  评论(0编辑  收藏  举报