I am using windows live sdk to import Windows live contacts. Windows live contacts can be imported by using JavaScript, REST api but i am using server side scenario to retrieve contacts.
First of all we have to create an application on windows live which you can create from this link. press create application button by which you can get API key and secret.
Login.aspx
Callback.aspx.cs
I think that this post will help you.
Thanks.
First of all we have to create an application on windows live which you can create from this link. press create application button by which you can get API key and secret.
Login.aspx
| <script type="text/javascript"> function GoToMSN() { top.location.href = "https://oauth.live.com/authorize?client_id =YOURCLIENTID &scope=REQUIREDSCOPRES &response_type=code& redirect_uri=http://contacts.YOURSITEURL.com/ callback.aspx"; }</script> <img alt="" title="Login to your msn account and import your contacts." src="msn.jpg" onclick="javascript:GoToMSN();"/> |
| namespace OAuthTest { using System; using System.Collections.Generic; using System.Collections.Specialized; using System.IO; using System.Linq; using System.Net; using System.Runtime.Serialization; using System.Runtime.Serialization.Json; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Xml; using System.Web.Script.Serialization; using System.Collections; using System.Data; public partial class Callback : System.Web.UI.Page { private const string wlCookie = "wl_auth"; private const string clientId = "XXXXXXXXXXXXXX"; private const string callback = "http://yoursite.com/callback1.aspx"; private const string clientSecret = "XXXXXXXXXXXXXXXXXX"; private const string oauthUrl = "https://oauth.live.com/token"; public static int k = 0; public static DataTable dtMSNContacts = new DataTable(); DataColumn dcNO = new DataColumn("SerialNo", System.Type.GetType("System.String")); DataColumn dcId = new DataColumn("MSNId", System.Type.GetType("System.String")); DataColumn dcName = new DataColumn("Friends Name", System.Type.GetType("System.String")); DataColumn dcDOB = new DataColumn("Friends DOB", System.Type.GetType("System.String")); { HttpContext context = HttpContext.Current; string verifier = Request.QueryString["Code"]; OAuthToken token; OAuthError error; if (!string.IsNullOrEmpty(verifier)) { RequestAccessTokenByVerifier(verifier, out token, out error); gvContacts.DataSource = dtMSNContacts; gvContacts.DataBind(); return; } } private static void RequestAccessTokenByVerifier(string verifier, out OAuthToken token, out OAuthError error) { string content = String.Format("client_id={0}&redirect_uri={1}&client_secret={2}&code={3}&grant_type=authorization_code", HttpUtility.UrlEncode(clientId), HttpUtility.UrlEncode(callback), HttpUtility.UrlEncode(clientSecret), HttpUtility.UrlEncode(verifier)); RequestAccessToken(content, out token, out error); } private static void RequestAccessToken(string postContent, out OAuthToken token, out OAuthError error) { token = null; error = null; HttpWebRequest request = WebRequest.Create(oauthUrl) as HttpWebRequest; request.Method = "POST"; try { using (StreamWriter writer = new StreamWriter(request.GetRequestStream())) { writer.Write(postContent); } HttpWebResponse response = request.GetResponse() as HttpWebResponse; if (response != null) { DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(OAuthToken)); token = serializer.ReadObject(response.GetResponseStream()) as OAuthToken; if (token != null) { RequestContacts(token.AccessToken); //return; } } } catch (WebException e) { HttpWebResponse response = e.Response as HttpWebResponse; if (response != null) { DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(OAuthError)); error = serializer.ReadObject(response.GetResponseStream()) as OAuthError; } } catch (IOException) { } if (error == null) { error = new OAuthError("request_failed", "Failed to retrieve user access token."); } } private static void RequestContacts(string AccessToken) { string content = String.Format("access_token={0}", HttpUtility.UrlEncode(AccessToken)); string url = "https://apis.live.net/v5.0/me/contacts?" + content; //string url = "https://apis.live.net/v5.0/me/contacts?access_token=" + AccessToken; HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; //request.Method = "POST"; request.Method = WebRequestMethods.Http.Get; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); StreamReader reader = new StreamReader(response.GetResponseStream()); string tmp = reader.ReadToEnd(); JavaScriptSerializer ser = new JavaScriptSerializer(); Dictionary<string, object> dictionary = ser.Deserialize<Dictionary<string, object>>(tmp); DisplayDictionary(dictionary); response.Close(); } private static bool DisplayDictionary(Dictionary<string, object> dict) { bool bSuccess = false; //indentLevel++; DataRow dr = dtMSNContacts.NewRow(); string dob = ""; foreach (string strKey in dict.Keys) { string strOutput = "";// "".PadLeft(indentLevel * 8) + strKey + ":"; //tbOutput.AppendText("\r\n" + strOutput); object o = dict[strKey]; if (o is Dictionary<string, object>) { DisplayDictionary((Dictionary<string, object>)o); } else if (o is ArrayList) { foreach (object oChild in ((ArrayList)o)) { if (oChild is string) { strOutput = ((string)oChild); //tbOutput.AppendText(strOutput + ","); } else if (oChild is Dictionary<string, object>) { DisplayDictionary((Dictionary<string, object>)oChild); //tbOutput.AppendText("\r\n"); } } } else { if (o != null) { strOutput = o.ToString(); if (strKey == "id") { k++; dr[0] = k; dr[1] = strOutput; } else if (strKey == "name") { dr[2] = strOutput; } else if (strKey == "birth_day") { dob = strOutput; } else if (strKey == "birth_month") { dob += "/" + strOutput; } dr[3] = dob; } } } if (dr[0].ToString() != "") { dtMSNContacts.Rows.Add(dr); } return bSuccess; } } [DataContract] public class OAuthToken { [DataMember(Name = OAuthConstants.AccessToken)] public string AccessToken { get; set; } [DataMember(Name = OAuthConstants.RefreshToken)] public string RefreshToken { get; set; } [DataMember(Name = OAuthConstants.ExpiresIn)] public string ExpiresIn { get; set; } [DataMember(Name = OAuthConstants.Scope)] public string Scope { get; set; } } public static class OAuthConstants { #region OAuth 2.0 standard parameters public const string ClientID = "client_id"; public const string ClientSecret = "client_secret"; public const string Callback = "redirect_uri"; public const string ClientState = "state"; public const string Scope = "scope"; public const string Code = "code"; public const string AccessToken = "access_token"; public const string ExpiresIn = "expires_in"; public const string RefreshToken = "refresh_token"; public const string ResponseType = "response_type"; public const string GrantType = "grant_type"; public const string Error = "error"; public const string ErrorDescription = "error_description"; public const string Display = "display"; #endregion } [DataContract] public class OAuthError { public OAuthError(string code, string desc) { this.Code = code; this.Description = desc; } [DataMember(Name = OAuthConstants.Error)] public string Code { get; private set; } [DataMember(Name = OAuthConstants.ErrorDescription)] public string Description { get; private set; } } } |
Thanks.