Monday 2 April 2012

Getting access token in Linkedin.

this is the code in silverlight:

namespace Ntier.SocailConnect.SearchEngine
{
    public partial class MainPage : UserControl
    {
        public const string AUTHORIZE = "https://api.linkedin.com/uas/oauth/authorize";
        public string oauth_callback = "http://localhost:2421/Ntier.SocailConnect.SearchEngineTestPage.html";//"http://localhost/DataAccess.asmx";
        public static string verifier;
        public string oauth_token;
       // public Service1SoapClient proxy;
        public MainPage()
        {
            //string verifier;
            InitializeComponent();
          Service1SoapClient  proxy = new Service1SoapClient();           
                proxy.CreateAuthorizationCompleted += new EventHandler<CreateAuthorizationCompletedEventArgs>(proxy_createauthorizationcompleted);
               // proxy.CreateAuthorizationAsync();
         
            IDictionary<string, string> QueryString = HtmlPage.Document.QueryString;
            if (QueryString.ContainsKey("oauth_verifier"))
            {
                verifier = QueryString["oauth_verifier"];               
            }
            if (verifier == null)
            {
                proxy.CreateAuthorizationAsync();
            }

            if (verifier != null)
            {
                proxy.getverifiterAsync(verifier);               
            }

            if (verifier != null)
            {
                proxy.GetAccessTokenCompleted += new EventHandler<GetAccessTokenCompletedEventArgs>(proxy_GetAccessTokenCompleted);
                proxy.GetAccessTokenAsync();

            }           
        }     

        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();  
               oauth_token = e.Result.ToString();             

            }
            catch
            {
                textBox1.Text = e.Result.ToString();
            }
        }
        private void proxy_GetAccessTokenCompleted(object sender, GetAccessTokenCompletedEventArgs e)
        {
            try
            {
                textBox1.Text = e.ToString();
            }

            catch
            {
                textBox1.Text = "nothing to show";
            }
        }  

    }
}



the code in webservice

private oAuthLinkedIn _oauth = new oAuthLinkedIn();
        [WebMethod]
        public string CreateAuthorization()
        {         
            string auth_link = _oauth.AuthorizationLinkGet();           
            //string oauth_callback = "http://localhost/Ntier.SocialConnect.DataAccess/DataAccess.asmx";
            //string auth_token  = _oauth.Token;
            Application["auth_token"] = _oauth.Token;
            //string auth_tokensecret = _oauth.TokenSecret;
            Application["auth_tokensecret"] = _oauth.TokenSecret;
            //string auth_verify = _oauth.AccessTokenGet(auth_token);
           // string auth_verifier = _oauth.Verifier;
            //string response = oAuthWebRequest(Method.POST, REQUEST_TOKEN, String.Empty);
            //Session["oauthlink"] = auth_link;
         // string auth_access=  _oauth.AccessTokenGet(auth_token);
            return Application["auth_token"].ToString();
        }

        [WebMethod]
        public string GetAccessToken()
        {
            _oauth.Token = Application["auth_token"].ToString();
            _oauth.TokenSecret = Application["auth_tokensecret"].ToString();
            _oauth.Verifier = Application["auth_verifier"].ToString();       

            _oauth.AccessTokenGet(Application["auth_token"].ToString());
            //string oauth_access = Application["auth_token"].ToString();
               Application["oauth_access"] = _oauth.Token;
            //string oauth_access_secret = Application["auth_tokensecret"].ToString();
             //Application["oauth_access_secret"] = _oauth.TokenSecret;
           return oauth_access;
        }

put web method in webservice to get the verifier from silverlight to webservice:

[WebMethod]
        public void getverifiter(string o_verifier)
        {
           //Application["auth_verifier"] = Convert.ToString(o_verifier);
            Application["auth_verifier"] = o_verifier;
          
        }

No comments:

Post a Comment