Ejemplo n.º 1
0
        //
        // GET: /Connect/Callback - Callback url from the api.xero.com authorisation endpoint

        public ActionResult Callback()
        {
            Debug.Write("Processing: /Connect/Callback");

            ApiRepository apiRepository = new ApiRepository();

            ITokenRepository <AccessToken>  accessTokenRepository  = new HttpSessionAccessTokenRepository(Session);
            ITokenRepository <RequestToken> requestTokenRepository = new HttpSessionRequestTokenRepository(Session);

            string verificationCode = Request.Params["oauth_verifier"];

            // Call api.xero.com/oauth/AccessToken
            IOAuthSession oauthSession = apiRepository.GetOAuthSession();

            IToken requestToken = requestTokenRepository.GetToken("");

            if (requestToken == null)
            {
                throw new ApplicationException("The request token could not be retrived from the current http session. Is session state and cookies enabled?");
            }

            AccessToken accessToken = oauthSession.ExchangeRequestTokenForAccessToken(requestToken, verificationCode);

            accessTokenRepository.SaveToken(accessToken);

            GetAndStoreAuthorisedOrganisationName(apiRepository);

            return(new RedirectResult("~/"));
        }
        //
        // GET: /Connect/Callback - Callback url from the api.xero.com authorisation endpoint

        public ActionResult Callback()
        {
            Debug.Write("Processing: /Connect/Callback");

            ApiRepository apiRepository = new ApiRepository();

            ITokenRepository<AccessToken> accessTokenRepository = new HttpSessionAccessTokenRepository(Session);
            ITokenRepository<RequestToken> requestTokenRepository = new HttpSessionRequestTokenRepository(Session);

            string verificationCode = Request.Params["oauth_verifier"];

            // Call api.xero.com/oauth/AccessToken
            IOAuthSession oauthSession = apiRepository.GetOAuthSession();

            IToken requestToken = requestTokenRepository.GetToken("");

            if (requestToken == null)
            {
                throw new ApplicationException("The request token could not be retrived from the current http session. Is session state and cookies enabled?");
            }

            AccessToken accessToken = oauthSession.ExchangeRequestTokenForAccessToken(requestToken, verificationCode);

            accessTokenRepository.SaveToken(accessToken);

            GetAndStoreAuthorisedOrganisationName(apiRepository);

            return new RedirectResult("~/");
        }
Ejemplo n.º 3
0
        //
        // GET: /Connect/Index - Main OAuth Connection Endpoint

        public ActionResult Index()
        {
            Debug.Write("Processing: /Connect/Index");

            ApiRepository apiRepository = new ApiRepository();
            ITokenRepository <AccessToken>  accessTokenRepository  = new HttpSessionAccessTokenRepository(Session);
            ITokenRepository <RequestToken> requestTokenRepository = new HttpSessionRequestTokenRepository(Session);

            // Do we already have a session token in sessionstate? - is it still usable?
            if (accessTokenRepository.GetToken("") != null)
            {
                if (apiRepository.TestConnectionToXeroApi(accessTokenRepository))
                {
                    return(new RedirectResult("~/"));
                }

                // The current session token+secret doesn't work - probably due to it expiring in 30mins.
                accessTokenRepository.SaveToken(null);
            }


            // Call api.xero.com/oauth/AccessToken
            IOAuthSession oauthSession = apiRepository.GetOAuthSession();
            RequestToken  requestToken = oauthSession.GetRequestToken();

            requestTokenRepository.SaveToken(requestToken);

            Trace.WriteLine("OAuth Request Token: " + requestToken.Token);
            Trace.WriteLine("OAuth Request Secret: " + requestToken.TokenSecret);

            string authorisationUrl = oauthSession.GetUserAuthorizationUrlForToken(requestToken);

            Trace.WriteLine("Redirecting browser to user authorisation uri:" + authorisationUrl);

            return(new RedirectResult(authorisationUrl));
        }
        //
        // GET: /Connect/Index - Main OAuth Connection Endpoint
        
        public ActionResult Index()
        {
            Debug.Write("Processing: /Connect/Index");

            ApiRepository apiRepository = new ApiRepository();
            ITokenRepository<AccessToken> accessTokenRepository = new HttpSessionAccessTokenRepository(Session);
            ITokenRepository<RequestToken> requestTokenRepository = new HttpSessionRequestTokenRepository(Session);

            // Do we already have a session token in sessionstate? - is it still usable?
            if (accessTokenRepository.GetToken("") != null)
            {
                if (apiRepository.TestConnectionToXeroApi(accessTokenRepository))
                {
                    return new RedirectResult("~/");
                }

                // The current session token+secret doesn't work - probably due to it expiring in 30mins.
                accessTokenRepository.SaveToken(null);
            }
            

            // Call api.xero.com/oauth/AccessToken
            IOAuthSession oauthSession = apiRepository.GetOAuthSession();
            RequestToken requestToken = oauthSession.GetRequestToken();

            requestTokenRepository.SaveToken(requestToken);

            Trace.WriteLine("OAuth Request Token: " + requestToken.Token);
            Trace.WriteLine("OAuth Request Secret: " + requestToken.TokenSecret);

            string authorisationUrl = oauthSession.GetUserAuthorizationUrlForToken(requestToken);

            Trace.WriteLine("Redirecting browser to user authorisation uri:" + authorisationUrl);

            return new RedirectResult(authorisationUrl);
        }