Thursday 29 March 2012

adding webservice with authorization in linkedIn. In silverlight (getting verifier)

Step 1:
we need to create a web service with the fuction:

In OauthlinkedIn.cs

public string AuthorizationLinkGet()
        {
//here you need to change your callback url.
            string oauth_callback="http://localhost/DataAccess.asmx";
            string ret = null;          
            string response = oAuthWebRequest(Method.POST, REQUEST_TOKEN, String.Empty,oauth_callback);
            if (response.Length > 0)            {
                //response contains token and token secret.  We only need the token.
                //oauth_token=36d1871d-5315-499f-a256-7231fdb6a1e0&oauth_token_secret=34a6cb8e-4279-4a0b-b840-085234678ab4&oauth_callback_confirmed=true
                NameValueCollection qs = HttpUtility.ParseQueryString(response);               
                if (qs["oauth_token"] != null)
                {
                    this.Token = qs["oauth_token"];
                    this.TokenSecret = qs["oauth_token_secret"];
                 
                    ret = AUTHORIZE + "?oauth_token=" + this.Token;                 

                }
            }


In default.asmx.cs (In webservice)



public string CreateAuthorization()
        {        
            string auth_link = _oauth.AuthorizationLinkGet();                     
            string auth_token = _oauth.Token;
            string auth_tokensecret = _oauth.TokenSecret;
            //string auth_verify = _oauth.AccessTokenGet(auth_token);
            string auth_verifier = _oauth.Verifier;             
            return auth_token;
        }
we can call this function in silverlight.


public partial class MainPage : UserControl
    {
        public const string AUTHORIZE = "https://api.linkedin.com/uas/oauth/authorize";
        public string oauth_callback ="http://localhost/DataAccess.asmx";
        public MainPage()
        {
            InitializeComponent();
            Service1SoapClient proxy = new Service1SoapClient();
            proxy.CreateAuthorizationCompleted+=new EventHandler<CreateAuthorizationCompletedEventArgs>(proxy_createauthorizationcompleted);
           proxy.CreateAuthorizationAsync();
        }

        private void proxy_createauthorizationcompleted(object sender, CreateAuthorizationCompletedEventArgs e)
        {
            try
            {               
               string  ret = AUTHORIZE + "?oauth_token=" + e.Result.ToString();
               System.Windows.Browser.HtmlPage.Window.Navigate(new Uri(ret));
               //NavigationService.Equals(ret, oauth_callback);               
              //  HttpContext.Current.Response.Redirect(ret);
                textBox1.Text = "Oauth_token :  " + e.Result.ToString();
            }
            catch
            {
                textBox1.Text = e.Result.ToString();
            }
        }
    }

result:
you  will redirect to linked in page, if you click on the continue, it will call back to localhost.

if you check in fiddler. you will get the verifier also. 
 




No comments:

Post a Comment