how can I run WLC Sample Code to retrieve WL contacts List properly?
-
Saturday, March 29, 2008 8:09 AM
I get a c# code sample from blow url:
http://msdn2.microsoft.com/en-us/library/bb463974.aspx
how can I modify the code for run it properly?
I think may be I must make some modify :
at first I want to run it by using then Sample Live id : WLCRest@hotmail.com
but I don't know the detail of Modify,such as:
Code Snippetpublic const string strSampleWLID = "WLCRest@hotmail.com";
public const string strSampleSignedIntLid = "4758200975490831962";//need Modify? how to?
public const string strSampleHostName = "livecontacts.services.live.com";//need Modify? how to?cumulus.services.live.com?// Authentication information
public const string strSampleDelegatedToken = "";//how to assign it?
public const string strSampleAuthHeaderValue = "DelegatedToken dt=\"" + Program.strSampleDelegatedToken + "\"";// Define configuration information.
public const int iSamplePort = 443;//need Modify? how to?
public const string strSamplePOSTDataFile = "SamplePOST.xml";//need Modify XML Source?
public const string strSamplePUTDataFile = "SamplePUT.xml";//need Modify XML Source?
public const string strSampleErrorDataFile = "ErrorResponse.xml";my next Scenario is by using my own Live ID,in this case how can I modify the code to implment it?
thanks a lot at first
All Replies
-
Tuesday, April 01, 2008 9:20 AMHere is some sample code that I had. It should help you get started with sending receiving requests from Live contacts. Put in a valid username and password for it to work. You'll need to copy paste the code in a new console application and add references to:
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Net;
Code Snippetclass Program
{
static void Main(string[] args)
{
string username = "someusername@live.com";//Put a username here
string password = "somepassword"; //Put password here
string ticket = new TicketAcquirer(username, password).GetTicket();
if (null == ticket)
{
System.Console.WriteLine("Unable to authenticate. Possibly invalid username / passwd");
return;
}
else
System.Console.WriteLine("Authenticated: " + username);
List<string> lst = new LiveContacts(username, ticket).GetAllContactIds();
System.Console.WriteLine("Got " + lst.Count.ToString() + " contacts");
}
}
public class LiveContacts
{
private string _username = "";
private string _ticket = "";
public LiveContacts(string username, string ticket)
{
this._username = username;
this._ticket = ticket;
}
/// <summary>
/// Returns the URI Path for the address book
/// </summary>
/// <param name="username"></param>
/// <returns></returns>
private String getURIPath()
{
// Define account information.
string uri = "https://cumulus.services.live.com/" + _username + "/LiveContacts/";
return uri;
}
/// <summary>
/// Creates a web request object with the appropriate properties set to talk to the live server
/// </summary>
/// <param name="uriPath">Path of the URI for the web request</param>
/// <param name="ticket">Ticket acquired for this session</param>
/// <returns>Web request object</returns>
private HttpWebRequest getHttpWebRequest(String uriPath)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uriPath);
request.Headers.Add("Authorization", "WLID1.0 t=\"" + _ticket + "\""); // Add the authentication header
request.AllowAutoRedirect = false;
request.UserAgent = "Sample Live App";
request.ContentType = "text/xml";
request.Pipelined = false;
request.ProtocolVersion = HttpVersion.Version10;
request.CookieContainer = new CookieContainer();
return request;
}
public List<String> GetAllContactIds()
{
List<String> contactids = new List<string>();
HttpWebRequest request = getHttpWebRequest(getURIPath());
request.Method = "GET";
HttpWebResponse response;
response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode != HttpStatusCode.OK || response.ContentLength == 0)
{
throw new ApplicationException("Get ALL faield");
}
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(response.GetResponseStream());
XmlNode contactsNode = xmldoc.SelectSingleNode("LiveContacts/Contacts");
if (null == contactsNode)
return new List<String>();
XmlNodeList contacts = contactsNode.ChildNodes;
foreach (XmlNode contact in contacts)
{
contactids.Add(contact.SelectSingleNode("ID").InnerText);
}
return contactids;
}
};
public class TicketAcquirer
{
private const string applicationId = "10"; // An arbitrary value that will be defined in the next non-alpha release
private string soapEnvelope = null;
public TicketAcquirer(String userName, String password)
{
soapEnvelope = @"<s:Envelope
xmlns:s = ""http://www.w3.org/2003/05/soap-envelope""
xmlns:wsse = ""http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd""
xmlns:saml = ""urn:oasis:names:tc:SAML:1.0:assertion""
xmlns:wsp = ""http://schemas.xmlsoap.org/ws/2004/09/policy""
xmlns:wsu = ""http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd""
xmlns:wsa = ""http://www.w3.org/2005/08/addressing""
xmlns:wssc = ""http://schemas.xmlsoap.org/ws/2005/02/sc""
xmlns:wst = ""http://schemas.xmlsoap.org/ws/2005/02/trust"">
<s:Header>
<wlid:ClientInfo xmlns:wlid = ""http://schemas.microsoft.com/wlid"">
<wlid:ApplicationID>" + applicationId + @"</wlid:ApplicationID>
</wlid:ClientInfo>
<wsa:Action s:mustUnderstand = ""1"">http://schemas.xmlsoap.org/ws/2005/02/trust/RST/Issue</wsa:Action>
<wsa:To s:mustUnderstand = ""1"">https://dev.login.live.com/wstlogin.srf</wsa:To>
<wsse:Security>
<wsse:UsernameToken wsu:Id = ""user"">
<wsse:Username>" + userName + @"</wsse:Username>
<wsse:Password>" + password + @"</wsse:Password>
</wsse:UsernameToken>
</wsse:Security>
</s:Header>
<s:Body>
<wst:RequestSecurityToken Id = ""RST0"">
<wst:RequestType>http://schemas.xmlsoap.org/ws/2005/02/trust/Issue</wst:RequestType>
<wsp:AppliesTo>
<wsa:EndpointReference>
<wsa:Address>http://live.com</wsa:Address>
</wsa:EndpointReference>
</wsp:AppliesTo>
<wsp:PolicyReference URI = ""MBI""></wsp:PolicyReference>
</wst:RequestSecurityToken>
</s:Body>
</s:Envelope>
";
}
/* methods */
public string GetTicket()
{
const string url = @"https://dev.login.live.com/wstlogin.srf";
WebRequest request = WebRequest.Create(url);
request.Method = "POST";
request.ContentType = "application/soap+xml; charset=UTF-8";
request.Timeout = 10 * 1000; // Wait for at most 10 seconds
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(soapEnvelope);
request.GetRequestStream().Write(bytes, 0, bytes.Length);
request.GetRequestStream().Close();
WebResponse response;
response = request.GetResponse();
string xml;
using (System.IO.StreamReader reader = new System.IO.StreamReader(response.GetResponseStream()))
xml = reader.ReadToEnd();
response.Close();
XmlDocument document = new XmlDocument();
document.LoadXml(xml);
XmlNamespaceManager nsManager = new XmlNamespaceManager(document.NameTable);
nsManager.AddNamespace("wsse", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
XmlNode node = document.SelectSingleNode(@"//wsse:BinarySecurityToken/text()", nsManager);
if (node == null)
return null; // The wsse:BinarySecurityToken element is missing. Examine the xml for error information
else
return node.Value;
}
} -
Thursday, April 03, 2008 7:07 PM
Hi, Microshaoft:
You may want to start reading the tutorial from http://dev.live.com/contacts/ You need to familiarize yourself with the concepts of "Delegation Token based authentication", RESTful http request, etc. All of these buzzwords may seem intimedating but they are actually rather straightforward stuff. After ramping up these basic knowledge, the sample code will make a lot more sense and you will be able to easily modify them to do what you want.
Thanks.
-
Thursday, November 18, 2010 11:11 AM
Hi,
Is it working?
Is not working for me, any help

