繁体   English   中英

发布facebook得分400错误的请求

[英]post facebook score 400 bad request

我尝试使用以下代码在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)错误的请求。

我究竟做错了什么?

实际上,我在与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拒绝了该时间戳。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM