Пример #1
0
        private FlickrUserDataResult retrieve_user_data(FlickrToken accessToken, string userid)
        {
            // dont need to do check again, verify_login would throw already

            IRestResponse<FlickrUserDataResult> response;
            try
            {
                var restClient = _restClientFactory.CreateRestClient(ApiBaseUrl);

                restClient.Authenticator = OAuth1Authenticator.ForProtectedResource(_consumerKey
                    , _consumerSecret
                    , accessToken.Token
                    , accessToken.TokenSecret);

                var request = new RestRequest("rest");
                request.AddParameter("nojsoncallback", 1);
                request.AddParameter("format", "json");
                request.AddParameter("user_id", userid);
                request.AddParameter("method", "flickr.people.getInfo");

                response = restClient.Execute<FlickrUserDataResult>(request);
            }
            catch (Exception exception)
            {
                throw new AuthenticationException(
                    "Failed to retrieve People.GetInfo json data from the Flickr Api.", exception);
            }

            if (response == null ||
                response.StatusCode != HttpStatusCode.OK ||
                response.Data == null)
            {
                throw new AuthenticationException(
                    string.Format(
                        "Failed to retrieve People.GetInfo json data OR the the response was not an HTTP Status 200 OK. Response Status: {0}. Response Description: {1}",
                        response == null ? "-- null response --" : response.StatusCode.ToString(),
                        response == null ? string.Empty : response.StatusDescription));
            }

            return response.Data;
        }
Пример #2
0
        private FlickrLoginResult verify_login(FlickrToken accessToken)
        {
            if (accessToken == null)
            {
                throw new ArgumentNullException("accessToken");
            }

            if (string.IsNullOrEmpty(accessToken.Token))
            {
                throw new ArgumentException("accessToken.Token");
            }

            if (string.IsNullOrEmpty(accessToken.TokenSecret))
            {
                throw new ArgumentException("accessToken.TokenSecret");
            }

            IRestResponse<FlickrLoginResult> response;
            try
            {
                var restClient = _restClientFactory.CreateRestClient(ApiBaseUrl);

                restClient.Authenticator = OAuth1Authenticator.ForProtectedResource(_consumerKey
                    , _consumerSecret
                    , accessToken.Token
                    , accessToken.TokenSecret);

                var request = new RestRequest("rest");
                request.AddParameter("nojsoncallback", 1);
                request.AddParameter("format", "json");
                request.AddParameter("method", "flickr.test.login");

                response = restClient.Execute<FlickrLoginResult>(request);
            }
            catch (Exception exception)
            {
                throw new AuthenticationException(
                    "Failed to retrieve Login json data from the Flickr Api.", exception);
            }

            if (response == null ||
                response.StatusCode != HttpStatusCode.OK ||
                response.Data == null)
            {
                throw new AuthenticationException(
                    string.Format(
                        "Failed to retrieve Login json data OR the the response was not an HTTP Status 200 OK. Response Status: {0}. Response Description: {1}",
                        response == null ? "-- null response --" : response.StatusCode.ToString(),
                        response == null ? string.Empty : response.StatusDescription));
            }

            return response.Data;
        }
Пример #3
0
        private FlickrToken retrieve_request_token(IAuthenticationServiceSettings authenticationServiceSettings)
        {
            IRestResponse response;
            var callBackUri = string.Format("{0}&state={1}", authenticationServiceSettings.CallBackUri,
                                            authenticationServiceSettings.State);

            try
            {
                var restClient = _restClientFactory.CreateRestClient(OAuthBaseUrl);
                restClient.Authenticator = OAuth1Authenticator.ForRequestToken(_consumerKey
                    , _consumerSecret
                    , callBackUri);

                var request = new RestRequest("oauth/request_token", Method.POST);
                response = restClient.Execute(request);
            }
            catch (Exception exception)
            {
                throw new AuthenticationException("Failed to obtain a Request Token from Flickr.", exception);
            }

            if (response == null ||
                response.StatusCode != HttpStatusCode.OK)
            {
                throw new AuthenticationException(
                    string.Format(
                        "Failed to obtain a Request Token from Flickr OR the the response was not an HTTP Status 200 OK. Response Status: {0}. Response Description: {1}",
                        response == null ? "-- null response --" : response.StatusCode.ToString(),
                        response == null ? string.Empty : response.StatusDescription));
            }

            // Grab the params which should have the request token info.
            var querystringParameters = HttpUtility.ParseQueryString(response.Content);
            var oAuthToken = querystringParameters[OAuthTokenKey];
            var oAuthTokenSecret = querystringParameters[OAuthTokenSecretKey];

            if (string.IsNullOrEmpty(oAuthToken) ||
                string.IsNullOrEmpty(oAuthTokenSecret))
            {
                throw new AuthenticationException(
                    "Retrieved a Flickr Request Token but it doesn't contain both the oauth_token and oauth_token_secret parameters.");
            }

            _requestToken = new FlickrToken
            {
                Token = oAuthToken,
                TokenSecret = oAuthTokenSecret
            };

            return _requestToken;
        }