发布facebook得分400错误请求

时间:2012-10-02 16:54:00

标签: c# facebook-graph-api

我尝试使用以下代码更新Facebook排行榜上的用户分数:

string _url = "https://graph.facebook.com/MY_APP_ID/scores";
var _parameters="score=100&access_token=MY_APP_TOKEN_WITH_PUBLISH_ACTION";
WebRequest _request = WebRequest.Create(_url);
_request.Method = "POST";                 
var _dataArray = Encoding.ASCII.GetBytes(_parameters.ToString());                
_request.ContentLength = _dataArray.Length;

using (Stream _dataStream = _request.GetRequestStream())
{
    _dataStream.Write(_dataArray, 0, _dataArray.Length);
}
WebResponse _response = _request.GetResponse();

当我尝试获取响应时,应用程序抛出异常:

  

远程服务器返回错误:(400)错误请求。

我做错了什么?

1 个答案:

答案 0 :(得分:1)

我实际上正处于与LinkedIn类似的事情中。将其包装在try块中并尝试此异常处理程序:

catch (WebException webEx) {
    StringBuilder sb = new StringBuilder();

    sb.AppendLine(webEx.Message);

    sb.AppendLine();
    sb.AppendLine("REQUEST: ");
    sb.AppendLine();

    sb.AppendLine(string.Format("Request URL: {0} {1}", webRequest.Method, webRequest.Address));
    sb.AppendLine("Headers:");
    foreach (string header in webRequest.Headers) {
        sb.AppendLine(header + ": " + webRequest.Headers[header]);
    }

    sb.AppendLine();
    sb.AppendLine("RESPONSE: ");
    sb.AppendLine();

    sb.AppendLine(string.Format("Status: {0}", webEx.Status));

    if (null != webEx.Response) {
        HttpWebResponse response = (HttpWebResponse) webEx.Response;

        sb.AppendLine(string.Format("Status Code: {0} {1}", (int) response.StatusCode, response.StatusDescription));
        if (0 != webEx.Response.ContentLength) {
            using (var stream = webEx.Response.GetResponseStream()) {
                if (null != stream) {
                    using (var reader = new StreamReader(stream)) {
                        sb.AppendLine(string.Format("Response: {0}", reader.ReadToEnd()));
                    }
                }
            }
        }
    }

    _log.Warn(sb.ToString(), webEx);

    throw new Exception(webEx.Message, webEx);
}

这至少会记录他们返回的内容。在我的情况下,我无法在我的系统上重现问题,但是QA的服务器收到了大量的400个错误请求。原来他们是无效的时间戳,因为系统时钟慢了7分钟,LinkedIn拒绝了时间戳。