[英]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.