Example #1
0
        public ActionResult MakeOAuthRequest(string accessToken, string accessTokenSecret, string endpointUri, string endpointAction)
        {
            OAuthProxyResponseModel retVal = new OAuthProxyResponseModel();

            OAuthKeyConfiguration oauthConfiguration    = OAuthKeyConfiguration.GetInstance();
            EndpointConfiguration endpointConfiguration = EndpointConfiguration.GetInstance();
            EndpointModel         endpointModel         = new EndpointModel();

            endpointModel.ServiceUri       = endpointUri;
            endpointModel.RequestTokenUri  = endpointConfiguration.RequestTokenUri;
            endpointModel.AuthorizationUri = endpointConfiguration.AuthorizationUri;
            endpointModel.AccessTokenUri   = endpointConfiguration.AccessTokenUri;

            if (!endpointUri.EndsWith(@"/") && !endpointAction.StartsWith(@"/"))
            {
                endpointUri += "/";
            }

            DefaultOAuthToken oauthToken = new DefaultOAuthToken();

            oauthToken.Token  = accessToken;
            oauthToken.Secret = accessTokenSecret;

            WebRequest request = HttpWebRequest.Create(endpointUri + endpointAction);

            request.Method      = "GET";
            request.ContentType = "application/json";
            OAuthClient oauthClient = new OAuthClient(oauthConfiguration.ConsumerKey, oauthConfiguration.ConsumerSecret, endpointModel);

            retVal.Response = oauthClient.ExecuteAuthorizedRequest(endpointUri + endpointAction, "application/json", string.Empty, oauthToken);

            return(View("MakeOAuthRequest", retVal));
        }
Example #2
0
        /// <summary>
        /// Get a requset token from an oauth server
        /// </summary>
        /// <param name="callbackUrl">The url to use to callback to after authorization</param>
        /// <param name="realm">The realm requesting access to</param>
        /// <returns>A token and secret</returns>
        public override IOAuthToken GetRequestToken(Realm realm, string callbackUrl)
        {
            DefaultOAuthToken retVal = null;

            if (realm != null && !string.IsNullOrEmpty(callbackUrl))
            {
                RestClient          restClient    = new RestClient(this.OAuthEndpoints.ServiceUri);
                OAuth1Authenticator authenticator = OAuth1Authenticator.ForRequestToken(this.ConsumerKey, this.ConsumerSecret, callbackUrl);
                authenticator.Realm      = realm.ToString();
                restClient.Authenticator = authenticator;
                RestRequest request  = new RestRequest(this.OAuthEndpoints.RequestTokenUri, Method.GET);
                var         response = restClient.Execute(request);

                var    qs          = HttpUtility.ParseQueryString(response.Content);
                string token       = qs[Constants.TokenParameter];
                string tokenSecret = qs[Constants.TokenSecretParameter];

                if (!string.IsNullOrEmpty(token) && !string.IsNullOrEmpty(tokenSecret))
                {
                    retVal        = new DefaultOAuthToken();
                    retVal.Token  = token;
                    retVal.Secret = tokenSecret;
                }
            }

            return(retVal);
        }
Example #3
0
        /// <summary>
        /// Get an access token without using a callback url.  This only works for the auto grant path.(which is only enabled for Vistaprint users
        /// </summary>
        /// <param name="realm"></param>
        /// <returns>An access token and secret</returns>
        public IOAuthToken GetInlineAccessToken(Realm realm)
        {
            DefaultOAuthToken retVal = null;

            DefaultOAuthToken requestToken = this.GetRequestToken(realm, Constants.InlineCallback) as DefaultOAuthToken;

            if (requestToken != null && requestToken.IsValid())
            {
                TokenVerification tokenVerification = this.InlineAuthorize(requestToken);

                if (tokenVerification != null)
                {
                    retVal = this.ExchangeRequestTokenForAccessToken(requestToken, tokenVerification.VerifierCode) as DefaultOAuthToken;
                }
            }

            return(retVal);
        }
Example #4
0
        /// <summary>
        /// Exchange an request token for an access token
        /// </summary>
        /// <param name="token">The request token</param>
        /// <param name="verifierCode">The verifier code</param>
        /// <returns>An access token and secret</returns>
        public override IOAuthToken ExchangeRequestTokenForAccessToken(IOAuthToken token, string verifierCode)
        {
            DefaultOAuthToken retVal = null;

            if (token != null && !string.IsNullOrEmpty(verifierCode))
            {
                RestClient  restClient = new RestClient(this.OAuthEndpoints.ServiceUri);
                RestRequest request    = new RestRequest(this.OAuthEndpoints.AccessTokenUri);
                restClient.Authenticator = OAuth1Authenticator.ForAccessToken(this.ConsumerKey, this.ConsumerSecret, token.Token, token.Secret, verifierCode);
                var response = restClient.Execute(request);

                var qs = HttpUtility.ParseQueryString(response.Content);
                retVal        = new DefaultOAuthToken();
                retVal.Token  = qs[Constants.TokenParameter];
                retVal.Secret = qs[Constants.TokenSecretParameter];
            }

            return(retVal);
        }