Пример #1
0
        internal protected virtual object Api(string path, IDictionary <string, object> parameters, HttpMethod httpMethod, Type resultType)
        {
            var mergedParameters = FacebookUtils.Merge(null, parameters);

            if (!mergedParameters.ContainsKey("access_token") && !string.IsNullOrEmpty(AccessToken))
            {
                mergedParameters["access_token"] = AccessToken;
            }

            Uri    requestUrl;
            string contentType;

            byte[] postData = BuildRequestData(path, mergedParameters, httpMethod, out requestUrl, out contentType);

            var jsonString = MakeRequest(httpMethod, requestUrl, postData, contentType);

            var json = JsonSerializer.Current.DeserializeObject(jsonString);

            FacebookApiException facebookApiException =
                ExceptionFactory.GetGraphException(json) ??
                ExceptionFactory.CheckForRestException(DomainMaps, requestUrl, json);

            if (facebookApiException != null)
            {
                throw facebookApiException;
            }

            return(resultType == null ? json : JsonSerializer.Current.DeserializeObject(jsonString, resultType));
        }
Пример #2
0
        public static FacebookApiException TryGetRestException(IDictionary <string, Uri> domainMaps, Uri requestUri, object json)
        {
            FacebookApiException error = null;

            // HACK: We have to do this because the REST Api doesn't return
            // the correct status codes when an error has occurred.
            if (FacebookUtils.IsUsingRestApi(domainMaps, requestUri))
            {
                // If we are using the REST API we need to check for an exception
                error = GetRestException(json);
            }

            return(error);
        }
Пример #3
0
 /// <summary>
 /// Initializes a new instance of the <see cref="FacebookAsyncResult"/> class.
 /// </summary>
 /// <param name="result">The result.</param>
 /// <param name="asyncState">State of the async.</param>
 /// <param name="asyncWaitHandle">The async wait handle.</param>
 /// <param name="completedSynchronously">if set to <c>true</c> [completed synchronously].</param>
 /// <param name="isCompleted">if set to <c>true</c> [is completed].</param>
 /// <param name="error">The error.</param>
 public FacebookAsyncResult(
     object result,
     object asyncState,
     System.Threading.WaitHandle asyncWaitHandle,
     bool completedSynchronously,
     bool isCompleted,
     FacebookApiException error)
 {
     _result                 = result;
     _asyncState             = asyncState;
     _asyncWaitHandle        = asyncWaitHandle;
     _completedSynchronously = completedSynchronously;
     _isCompleted            = isCompleted;
     _error = error;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="FacebookAsyncResult"/> class.
 /// </summary>
 /// <param name="result">The result.</param>
 /// <param name="asyncState">State of the async.</param>
 /// <param name="asyncWaitHandle">The async wait handle.</param>
 /// <param name="completedSynchronously">if set to <c>true</c> [completed synchronously].</param>
 /// <param name="isCompleted">if set to <c>true</c> [is completed].</param>
 /// <param name="error">The error.</param>
 public FacebookAsyncResult(
     object result,
     object asyncState,
     System.Threading.WaitHandle asyncWaitHandle,
     bool completedSynchronously,
     bool isCompleted,
     FacebookApiException error)
 {
     this.result = result;
     this.asyncState = asyncState;
     this.asyncWaitHandle = asyncWaitHandle;
     this.completedSynchronously = completedSynchronously;
     this.isCompleted = isCompleted;
     this.error = error;
 }
Пример #5
0
        /// <summary>
        /// Checks for rest exception.
        /// </summary>
        /// <param name="domainMaps">
        /// The domain maps.
        /// </param>
        /// <param name="requestUri">
        /// The request uri.
        /// </param>
        /// <param name="json">
        /// The json string.
        /// </param>
        /// <returns>
        /// Returns <see cref="FacebookApiException"/> if it is a rest exception otherwise null.
        /// </returns>
        internal static FacebookApiException CheckForRestException(IDictionary <string, Uri> domainMaps, Uri requestUri, string json)
        {
            Contract.Requires(requestUri != null);

            FacebookApiException error = null;

            // HACK: We have to do this because the REST Api doesn't return
            // the correct status codes when an error has occurred.
            if (FacebookUtils.IsUsingRestApi(domainMaps, requestUri))
            {
                // If we are using the REST API we need to check for an exception
                var resultObject = JsonSerializer.Current.DeserializeObject(json);
                error = GetRestException(resultObject);
            }

            return(error);
        }
Пример #6
0
        /// <summary>
        /// Gets the graph exception if possible.
        /// </summary>
        /// <param name="result">The web request result object to check for exception information.</param>
        /// <returns>A Facebook API exception or null.</returns>
        internal static FacebookApiException GetGraphException(object result)
        {
            // Note: broke down GetGraphException into different method for unit testing.
            FacebookApiException resultException = null;

            if (result != null)
            {
                var responseDict = result as IDictionary <string, object>;
                if (responseDict != null)
                {
                    if (responseDict.ContainsKey("error"))
                    {
                        var error = responseDict["error"] as IDictionary <string, object>;
                        if (error != null)
                        {
                            var errorType    = error["type"] as string;
                            var errorMessage = error["message"] as string;

                            // Check to make sure the correct data is in the response
                            if (!String.IsNullOrEmpty(errorType) && !String.IsNullOrEmpty(errorMessage))
                            {
                                // We don't include the inner exception because it is not needed and is always a WebException.
                                // It is easier to understand the error if we use Facebook's error message.
                                if (errorType == "OAuthException")
                                {
                                    resultException = new FacebookOAuthException(errorMessage, errorType);
                                }
                                else if (errorType == "API_EC_TOO_MANY_CALLS" || (errorMessage != null && errorMessage.Contains("request limit reached")))
                                {
                                    resultException = new FacebookApiLimitException(errorMessage, errorType);
                                }
                                else
                                {
                                    resultException = new FacebookApiException(errorMessage, errorType);
                                }
                            }
                        }
                    }
                }
            }

            return(resultException);
        }
Пример #7
0
        /// <summary>
        /// Gets the rest exception if possible.
        /// </summary>
        /// <param name="result">The web request result object to check for exception information.</param>
        /// <returns>The Facebook API exception or null.</returns>
        internal static FacebookApiException GetRestException(object result)
        {
            // The REST API does not return a status that causes a WebException
            // even when there is an error. For this reason we have to parse a
            // successful response to see if it contains error information.
            // If it does have an error message we throw a FacebookApiException.
            FacebookApiException resultException = null;

            if (result != null)
            {
                var resultDict = result as IDictionary <string, object>;
                if (resultDict != null)
                {
                    if (resultDict.ContainsKey("error_code"))
                    {
                        string error_code = resultDict["error_code"].ToString();
                        string error_msg  = null;
                        if (resultDict.ContainsKey("error_msg"))
                        {
                            error_msg = resultDict["error_msg"] as string;
                        }

                        // Error Details: http://wiki.developers.facebook.com/index.php/Error_codes
                        if (error_code == "190")
                        {
                            resultException = new FacebookOAuthException(error_msg, error_code);
                        }
                        else if (error_code == "4" || error_code == "API_EC_TOO_MANY_CALLS" || (error_msg != null && error_msg.Contains("request limit reached")))
                        {
                            resultException = new FacebookApiLimitException(error_msg, error_code);
                        }
                        else
                        {
                            resultException = new FacebookApiException(error_msg, error_code);
                        }
                    }
                }
            }

            return(resultException);
        }
Пример #8
0
        internal static FacebookApiException GetGraphException(WebExceptionWrapper exception)
        {
            Contract.Requires(exception != null);

            FacebookApiException resultException = null;

            try
            {
                if (exception.HasResponse)
                {
                    object response = null;
                    string json     = null;
                    using (var stream = exception.GetResponseStream())
                    {
                        if (stream != null)
                        {
                            using (var reader = new StreamReader(stream))
                            {
                                json = reader.ReadToEnd();
                            }
                        }
                    }

                    if (json != null)
                    {
                        response = JsonSerializer.Current.DeserializeObject(json);
                    }

                    resultException = GetGraphException(response);
                }
            }
            catch
            {
                resultException = null;

                // We dont want to throw anything associated with
                // trying to build the FacebookApiException
            }

            return(resultException);
        }
Пример #9
0
        /// <summary>
        /// Gets the rest exception if possible.
        /// </summary>
        /// <param name="result">The web request result object to check for exception information.</param>
        /// <returns>The Facebook API exception or null.</returns>
        internal static FacebookApiException GetRestException(object result)
        {
            // The REST API does not return a status that causes a WebException
            // even when there is an error. For this reason we have to parse a
            // successful response to see if it contains error information.
            // If it does have an error message we throw a FacebookApiException.
            FacebookApiException resultException = null;
            if (result != null)
            {
                var resultDict = result as IDictionary<string, object>;
                if (resultDict != null)
                {
                    if (resultDict.ContainsKey("error_code"))
                    {
                        string error_code = resultDict["error_code"].ToString();
                        string error_msg = null;
                        if (resultDict.ContainsKey("error_msg"))
                        {
                            error_msg = resultDict["error_msg"] as string;
                        }

                        // Error Details: http://wiki.developers.facebook.com/index.php/Error_codes
                        if (error_code == "190")
                        {
                            resultException = new FacebookOAuthException(error_msg, error_code);
                        }
                        else if (error_code == "4" || error_code == "API_EC_TOO_MANY_CALLS" || (error_msg != null && error_msg.Contains("request limit reached")))
                        {
                            resultException = new FacebookApiLimitException(error_msg, error_code);
                        }
                        else
                        {
                            resultException = new FacebookApiException(error_msg, error_code);
                        }
                    }
                }
            }

            return resultException;
        }
Пример #10
0
        /// <summary>
        /// Gets the graph exception if possible.
        /// </summary>
        /// <param name="result">The web request result object to check for exception information.</param>
        /// <returns>A Facebook API exception or null.</returns>
        internal static FacebookApiException GetGraphException(object result)
        {
            // Note: broke down GetGraphException into different method for unit testing.
            FacebookApiException resultException = null;
            if (result != null)
            {
                var responseDict = result as IDictionary<string, object>;
                if (responseDict != null)
                {
                    if (responseDict.ContainsKey("error"))
                    {
                        var error = responseDict["error"] as IDictionary<string, object>;
                        if (error != null)
                        {
                            var errorType = error["type"] as string;
                            var errorMessage = error["message"] as string;

                            // Check to make sure the correct data is in the response
                            if (!String.IsNullOrEmpty(errorType) && !String.IsNullOrEmpty(errorMessage))
                            {
                                // We don't include the inner exception because it is not needed and is always a WebException.
                                // It is easier to understand the error if we use Facebook's error message.
                                if (errorType == "OAuthException")
                                {
                                    resultException = new FacebookOAuthException(errorMessage, errorType);
                                }
                                else if (errorType == "API_EC_TOO_MANY_CALLS" || (errorMessage != null && errorMessage.Contains("request limit reached")))
                                {
                                    resultException = new FacebookApiLimitException(errorMessage, errorType);
                                }
                                else
                                {
                                    resultException = new FacebookApiException(errorMessage, errorType);
                                }
                            }
                        }
                        else
                        {
                            long? errorNumber = null;
                            if (responseDict["error"] is long)
                                errorNumber = (long)responseDict["error"];
                            if (errorNumber == null && responseDict["error"] is int)
                                errorNumber = (int)responseDict["error"];
                            string errorDescription = null;
                            if (responseDict.ContainsKey("error_description"))
                                errorDescription = responseDict["error_description"] as string;
                            if (errorNumber != null && !string.IsNullOrEmpty(errorDescription))
                            {
                                if (errorNumber == 190)
                                {
                                    resultException = new FacebookOAuthException(errorDescription, "API_EC_PARAM_ACCESS_TOKEN");
                                }
                                else
                                {
                                    resultException = new FacebookApiException(errorDescription, errorNumber.Value.ToString());
                                }
                            }
                        }
                    }
                }
            }

            return resultException;
        }
Пример #11
0
        internal static Exception GetException(HttpHelper httpHelper, object result)
        {
            if (result == null)
            {
                return(null);
            }

            var responseDict = result as IDictionary <string, object>;

            if (responseDict == null)
            {
                return(null);
            }

            FacebookApiException resultException = null;

            if (httpHelper != null)
            {
                var response    = httpHelper.HttpWebResponse;
                var responseUri = response.ResponseUri;

                // legacy rest api
                if (responseUri.Host == "api.facebook.com" ||
                    responseUri.Host == "api-read.facebook.com" ||
                    responseUri.Host == "api-video.facebook.com" ||
                    responseUri.Host == "api.beta.facebook.com" ||
                    responseUri.Host == "api-read.beta.facebook.com" ||
                    responseUri.Host == "api-video.facebook.com")
                {
                    if (responseDict.ContainsKey("error_code"))
                    {
                        string errorCode = responseDict["error_code"].ToString();
                        string errorMsg  = null;
                        if (responseDict.ContainsKey("error_msg"))
                        {
                            errorMsg = responseDict["error_msg"] as string;
                        }

                        // Error Details: http://wiki.developers.facebook.com/index.php/Error_codes
                        if (errorCode == "190")
                        {
                            resultException = new FacebookOAuthException(errorMsg, errorCode);
                        }
                        else if (errorCode == "4" || errorCode == "API_EC_TOO_MANY_CALLS" ||
                                 (errorMsg != null && errorMsg.Contains("request limit reached")))
                        {
                            resultException = new FacebookApiLimitException(errorMsg, errorCode);
                        }
                        else
                        {
                            resultException = new FacebookApiException(errorMsg, errorCode);
                        }
                        return(resultException);
                    }

                    return(null);
                }
            }

            // graph api error
            if (responseDict.ContainsKey("error"))
            {
                var error = responseDict["error"] as IDictionary <string, object>;
                if (error != null)
                {
                    var errorType    = error["type"] as string;
                    var errorMessage = error["message"] as string;
                    int errorCode    = 0;

                    if (error.ContainsKey("code"))
                    {
                        int.TryParse(error["code"].ToString(), out errorCode);
                    }

                    // Check to make sure the correct data is in the response
                    if (!string.IsNullOrEmpty(errorType) && !string.IsNullOrEmpty(errorMessage))
                    {
                        // We don't include the inner exception because it is not needed and is always a WebException.
                        // It is easier to understand the error if we use Facebook's error message.
                        if (errorType == "OAuthException")
                        {
                            resultException = new FacebookOAuthException(errorMessage, errorType, errorCode);
                        }
                        else if (errorType == "API_EC_TOO_MANY_CALLS" || (errorMessage.Contains("request limit reached")))
                        {
                            resultException = new FacebookApiLimitException(errorMessage, errorType);
                        }
                        else
                        {
                            resultException = new FacebookApiException(errorMessage, errorType, errorCode);
                        }
                    }
                }
                else
                {
                    long?errorNumber = null;
                    if (responseDict["error"] is long)
                    {
                        errorNumber = (long)responseDict["error"];
                    }
                    if (errorNumber == null && responseDict["error"] is int)
                    {
                        errorNumber = (int)responseDict["error"];
                    }
                    string errorDescription = null;
                    if (responseDict.ContainsKey("error_description"))
                    {
                        errorDescription = responseDict["error_description"] as string;
                    }
                    if (errorNumber != null && !string.IsNullOrEmpty(errorDescription))
                    {
                        if (errorNumber == 190)
                        {
                            resultException = new FacebookOAuthException(errorDescription, "API_EC_PARAM_ACCESS_TOKEN");
                        }
                        else
                        {
                            resultException = new FacebookApiException(errorDescription, errorNumber.Value.ToString(CultureInfo.InvariantCulture));
                        }
                    }
                }
            }

            return(resultException);
        }
Пример #12
0
        internal static Exception GetException(HttpHelper httpHelper, object result)
        {
            if (result == null)
                return null;

            var responseDict = result as IDictionary<string, object>;
            if (responseDict == null)
                return null;

            FacebookApiException resultException = null;

            if (httpHelper != null)
            {
                var response = httpHelper.HttpWebResponse;
                var responseUri = response.ResponseUri;

                // legacy rest api
                if (responseUri.Host == "api.facebook.com" ||
                    responseUri.Host == "api-read.facebook.com" ||
                    responseUri.Host == "api-video.facebook.com" ||
                    responseUri.Host == "api.beta.facebook.com" ||
                    responseUri.Host == "api-read.beta.facebook.com" ||
                    responseUri.Host == "api-video.facebook.com")
                {
                    if (responseDict.ContainsKey("error_code"))
                    {
                        string errorCode = responseDict["error_code"].ToString();
                        string errorMsg = null;
                        if (responseDict.ContainsKey("error_msg"))
                            errorMsg = responseDict["error_msg"] as string;

                        // Error Details: http://wiki.developers.facebook.com/index.php/Error_codes
                        if (errorCode == "190")
                            resultException = new FacebookOAuthException(errorMsg, errorCode);
                        else if (errorCode == "4" || errorCode == "API_EC_TOO_MANY_CALLS" ||
                                 (errorMsg != null && errorMsg.Contains("request limit reached")))
                            resultException = new FacebookApiLimitException(errorMsg, errorCode);
                        else
                            resultException = new FacebookApiException(errorMsg, errorCode);
                        return resultException;
                    }
                    return null;
                }
            }

            // graph api error
            if (responseDict.ContainsKey("error"))
            {
                var error = responseDict["error"] as IDictionary<string, object>;
                if (error != null)
                {
                    var errorType = error["type"] as string;
                    var errorMessage = error["message"] as string;

                    // Check to make sure the correct data is in the response
                    if (!string.IsNullOrEmpty(errorType) && !string.IsNullOrEmpty(errorMessage))
                    {
                        // We don't include the inner exception because it is not needed and is always a WebException.
                        // It is easier to understand the error if we use Facebook's error message.
                        if (errorType == "OAuthException")
                            resultException = new FacebookOAuthException(errorMessage, errorType);
                        else if (errorType == "API_EC_TOO_MANY_CALLS" || (errorMessage.Contains("request limit reached")))
                            resultException = new FacebookApiLimitException(errorMessage, errorType);
                        else
                            resultException = new FacebookApiException(errorMessage, errorType);
                    }
                }
                else
                {
                    long? errorNumber = null;
                    if (responseDict["error"] is long)
                        errorNumber = (long)responseDict["error"];
                    if (errorNumber == null && responseDict["error"] is int)
                        errorNumber = (int)responseDict["error"];
                    string errorDescription = null;
                    if (responseDict.ContainsKey("error_description"))
                        errorDescription = responseDict["error_description"] as string;
                    if (errorNumber != null && !string.IsNullOrEmpty(errorDescription))
                    {
                        if (errorNumber == 190)
                            resultException = new FacebookOAuthException(errorDescription, "API_EC_PARAM_ACCESS_TOKEN");
                        else
                            resultException = new FacebookApiException(errorDescription, errorNumber.Value.ToString(CultureInfo.InvariantCulture));
                    }
                }
            }

            return resultException;
        }