Facebook Graph API:远程服务器返回错误:(400)错误的请求,而不是内部错误

时间:2018-11-06 10:33:41

标签: facebook-graph-api bad-request

我有一个向Facebook Graph API发出Get请求以获取企业帐户的代码。

这是代码:

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)

URI如下:

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..

应该是这样的返回错误:

{
  "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"
  }
}

但是错误:The remote server returned an error: (400) Bad Request是我得到的

有人可以帮助我吗?

非常感谢!

1 个答案:

答案 0 :(得分:0)

我找到了一个主意,并从try / catch语句中捕获了内部异常

应该是这样的:

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