简体   繁体   中英

post facebook score 400 bad request

I am try to update a user score on a Facebook leaderboard using the following code:

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();

When I try to get the response the application throws an exception:

The remote server returned an error: (400) Bad Request.

What am I doing wrong?

I'm actually in the middle of something similar with LinkedIn. Wrap it in a try block and try this exception handler:

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);
}

That'll at least log what they're returning. In my case, I couldn't reproduce the problem on my system, but QA's servers were getting a ton of 400 Bad Requests. Turned out they were invalid timestamps because the system clocks were 7 minutes slow, and LinkedIn was rejecting the timestamp.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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