protected AccessToken RetrieveAccessToken(string authorizationCode, Uri redirectUri) { if (string.IsNullOrEmpty(authorizationCode)) { throw new ArgumentNullException("authorizationCode"); } if (redirectUri == null || string.IsNullOrEmpty(redirectUri.AbsoluteUri)) { throw new ArgumentNullException("redirectUri"); } IRestResponse <TAccessTokenResult> response; try { response = ExecuteRetrieveAccessToken(authorizationCode, redirectUri); } catch (Exception exception) { var authentictionException = new AuthenticationException(string.Format("Failed to retrieve an Access Token from {0}.", Name), exception); var errorMessage = string.Format("{0}", authentictionException.RecursiveErrorMessages()); TraceSource.TraceError(errorMessage); throw authentictionException; } if (response == null || response.StatusCode != HttpStatusCode.OK || response.ErrorException != null) { var errorMessage = string.Format( "Failed to obtain an Access Token from {0} OR the the response was not an HTTP Status 200 OK. Response Status: {1}. Response Description: {2}. Error Content: {3}. Error Message: {4}.", string.IsNullOrEmpty(Name) ? "--no name--" : Name, response == null ? "-- null response --" : response.StatusCode.ToString(), response == null ? string.Empty : response.StatusDescription, response == null ? string.Empty : response.Content, response == null ? string.Empty : response.ErrorException == null ? "--no error exception--" : response.ErrorException.RecursiveErrorMessages()); TraceSource.TraceError(errorMessage); throw new AuthenticationException(errorMessage); } return(MapAccessTokenResultToAccessToken(response.Data)); }
protected override UserInformation RetrieveUserInformation(AccessToken accessToken) { if (accessToken == null) { throw new ArgumentNullException("accessToken"); } if (string.IsNullOrEmpty(accessToken.PublicToken)) { throw new ArgumentException("accessToken.PublicToken"); } IRestResponse <MeResult> response; try { var restRequest = new RestRequest("me"); restRequest.AddParameter("access_token", accessToken.PublicToken); var restClient = RestClientFactory.CreateRestClient(BaseUrl); TraceSource.TraceVerbose("Retrieving user information. Facebook Endpoint: {0}", restClient.BuildUri(restRequest).AbsoluteUri); response = restClient.Execute <MeResult>(restRequest); } catch (Exception exception) { var authenticationException = new AuthenticationException("Failed to retrieve any Me data from the Facebook Api.", exception); var errorMessage = authenticationException.RecursiveErrorMessages(); TraceSource.TraceError(errorMessage); throw new AuthenticationException(errorMessage, exception); } if (response == null || response.StatusCode != HttpStatusCode.OK || response.Data == null) { var errorMessage = string.Format( "Failed to obtain some 'Me' data from the Facebook api OR the the response was not an HTTP Status 200 OK. Response Status: {0}. Response Description: {1}. Error Message: {2}.", response == null ? "-- null response --" : response.StatusCode.ToString(), response == null ? string.Empty : response.StatusDescription, response == null ? string.Empty : response.ErrorException == null ? "--no error exception--" : response.ErrorException.RecursiveErrorMessages()); TraceSource.TraceError(errorMessage); throw new AuthenticationException(errorMessage); } var id = response.Data.Id < 0 ? 0 : response.Data.Id; var name = (string.IsNullOrEmpty(response.Data.FirstName) ? string.Empty : response.Data.FirstName) + " " + (string.IsNullOrEmpty(response.Data.LastName) ? string.Empty : response.Data.LastName).Trim(); return(new UserInformation { Id = id.ToString(), Name = name, Email = response.Data.Email, Locale = response.Data.Locale, UserName = response.Data.Username, Picture = string.Format("https://graph.facebook.com/{0}/picture", id) }); }