Search code examples
facebook-graph-apibad-request

Facebook Graph API: The remote server returned an error: (400) Bad Request instead of error inside


I have a code that make a Get request to Facebook Graph API to get business account.

This is code:

var facebookBusinessAccountId = ConfigurationManager.AppSettings["facebook_business_account_id"];
                            facebookRestUrl += "&access_token=" + acct.AccessToken;

                            facebookRestUrl = facebookRestUrl.Replace("{0}", facebookBusinessAccountId);

                            var request = WebRequest.Create(facebookRestUrl);
                            request.Method = "GET";
                            request.ContentType = "application/json";

                            var response = request.GetResponse(); (Error here)

The URI look like:

https://graph.facebook.com/v3.1/1x8xx4005xxxxxx?fields=business_discovery.username(yess_cub){biography,id,ig_id,profile_picture_url,username,website}&access_token=EAAJ..

It should be return error like this:

{
  "error": {
    "message": "Invalid user id",
    "type": "OAuthException",
    "code": 110,
    "error_subcode": 2207013,
    "is_transient": false,
    "error_user_title": "Cannot find User",
    "error_user_msg": "The user with username: yess_cub cannot be found.",
    "fbtrace_id": "BMEAcyYOZf9"
  }
}

But error : The remote server returned an error: (400) Bad Request is what I get

Could anyone please help me?

Thanks alot!


Solution

  • I found an idea and catch the inner exception from try/catch statement

    It should be like this:

    catch (WebException ex)
    {
       using (StreamReader reader = new StreamReader(ex.Response.GetResponseStream()))
       {
          string jsonMessageString = reader.ReadToEnd();
       }
    }