locked
how to use the windows live apis from a C# client application? RRS feed

  • Question

  • Hello,

    i am trying to incorporate the WL contacts api in my WPF client application on windows 7. how do i go about accessing the API since this is not in javascript but uses C# on desktop.

    thanks

    -F

    Saturday, June 18, 2011 6:35 PM

Answers

  • using System;

    using System.Collections.Generic;

    using System.ComponentModel;

    using System.Data;

    using System.Drawing;

    using System.Linq;

    using System.Text;

    using System.Windows.Forms;

    using System.Web.Script.Serialization;

    using System.IO;

    using System.Net;

    using System.Diagnostics;

     

    namespace DesktopDemo2

    {

        public partial class Form1 : Form

        {

            string access_token = null;

            static string scope = "wl.basic";

            static string client_id = "YOUR CLIENT ID GOES HERE";

            private static string signInUrl = String.Format(@"https://oauth.live.com/authorize?client_id={0}&redirect_uri=https://oauth.live.com/desktop&response_type=token&scope={1}", client_id, scope);

            private static string requestUrl = @"https://apis.live.net/v5.0/me?access_token=";

     

            public Form1()

            {

                InitializeComponent();

                txtConsole.Text = "Signin URL: \r\n" + signInUrl + "\r\n\r\n";

                webBrowser.Navigate(signInUrl);

            }

     

            private void webBrowser_Navigated(object sender, WebBrowserNavigatedEventArgs e)

            {

                if (e.Url.Fragment.Contains("access_token"))

                {

                    access_token = e.Url.Fragment.Remove(0, 14);

                    requestUrl += access_token;

                    txtConsole.Text += "Response URL: \r\n" + e.Url + "\r\n\r\n";

                    txtConsole.Text += "Request URL: \r\n" + requestUrl + "\r\n\r\n";

                    makeRequest(requestUrl);

                }

            }

     

            private void makeRequest(string requestUrl)

            {

                WebRequest wr;

                wr = WebRequest.Create(requestUrl);

                Stream objStream;

                objStream = wr.GetResponse().GetResponseStream();

                string jsonResponse = parseRequest(objStream);

                txtConsole.Text += jsonResponse;

     

                Dictionary<string, string> dict = deserializeJson(jsonResponse);

                webBrowser.Navigate(dict["link"]);

            }

     

            // Turn the response stream into a string.

            private string parseRequest(Stream s)

            {

                StreamReader objReader = new StreamReader(s);

                string sLine = "";

                string jsonResponse = "";

                while (sLine != null)

                {

                    sLine = objReader.ReadLine();

                    if (sLine != null)

                    {

                        jsonResponse += sLine;

                    }

                }

                return jsonResponse;

            }

     

            private Dictionary<string, string> deserializeJson(string json)

            {

                var jss = new JavaScriptSerializer();

                var d = jss.Deserialize<Dictionary<string, string>>(json);

                return d;

            }

        }

    }

     

    Sunday, June 19, 2011 12:46 PM

All replies

  • yes I would also like to know how to do this!!
    Sunday, June 19, 2011 1:48 AM
  • using System;

    using System.Collections.Generic;

    using System.ComponentModel;

    using System.Data;

    using System.Drawing;

    using System.Linq;

    using System.Text;

    using System.Windows.Forms;

    using System.Web.Script.Serialization;

    using System.IO;

    using System.Net;

    using System.Diagnostics;

     

    namespace DesktopDemo2

    {

        public partial class Form1 : Form

        {

            string access_token = null;

            static string scope = "wl.basic";

            static string client_id = "YOUR CLIENT ID GOES HERE";

            private static string signInUrl = String.Format(@"https://oauth.live.com/authorize?client_id={0}&redirect_uri=https://oauth.live.com/desktop&response_type=token&scope={1}", client_id, scope);

            private static string requestUrl = @"https://apis.live.net/v5.0/me?access_token=";

     

            public Form1()

            {

                InitializeComponent();

                txtConsole.Text = "Signin URL: \r\n" + signInUrl + "\r\n\r\n";

                webBrowser.Navigate(signInUrl);

            }

     

            private void webBrowser_Navigated(object sender, WebBrowserNavigatedEventArgs e)

            {

                if (e.Url.Fragment.Contains("access_token"))

                {

                    access_token = e.Url.Fragment.Remove(0, 14);

                    requestUrl += access_token;

                    txtConsole.Text += "Response URL: \r\n" + e.Url + "\r\n\r\n";

                    txtConsole.Text += "Request URL: \r\n" + requestUrl + "\r\n\r\n";

                    makeRequest(requestUrl);

                }

            }

     

            private void makeRequest(string requestUrl)

            {

                WebRequest wr;

                wr = WebRequest.Create(requestUrl);

                Stream objStream;

                objStream = wr.GetResponse().GetResponseStream();

                string jsonResponse = parseRequest(objStream);

                txtConsole.Text += jsonResponse;

     

                Dictionary<string, string> dict = deserializeJson(jsonResponse);

                webBrowser.Navigate(dict["link"]);

            }

     

            // Turn the response stream into a string.

            private string parseRequest(Stream s)

            {

                StreamReader objReader = new StreamReader(s);

                string sLine = "";

                string jsonResponse = "";

                while (sLine != null)

                {

                    sLine = objReader.ReadLine();

                    if (sLine != null)

                    {

                        jsonResponse += sLine;

                    }

                }

                return jsonResponse;

            }

     

            private Dictionary<string, string> deserializeJson(string json)

            {

                var jss = new JavaScriptSerializer();

                var d = jss.Deserialize<Dictionary<string, string>>(json);

                return d;

            }

        }

    }

     

    Sunday, June 19, 2011 12:46 PM
  • Thanks for the code snippet Dare!

    I was hoping for a stronger typing using some client libraries so that i dont have to deal with the raw http response.  if you look at the Google Contacts API set, they provide .Net client libraries for interacting with the  gmail contacts.

    http://code.google.com/intl/en/apis/contacts/docs/3.0/developers_guide_dotnet.html#retrieving_without_query

     

    -F

    Thursday, July 14, 2011 2:55 AM
  • Microsoft released some PHP server-side code here:

    http://msdn.microsoft.com/en-us/library/hh243649.aspx

    I just released a translation of the PHP code to C#, feel free to use it. 

    http://liveconnectcsharp.codeplex.com/


    Monday, July 18, 2011 9:53 AM