0

我一直在寻找关于向 Facebook 应用程序发布分数的答案,但我仍然无法让它工作。我正在使用的代码在这里 -

private const string FACEBOOK_POST_SCORE_URL = "https://graph.facebook.com/me/scores?access_token={0}";

public void PostScoreAsync(Action<FacebookResponse> response, FacebookScore score)
    {

        try
        {
            // Append the user's access token to the URL
            Uri fullUri = new Uri(string.Format(FACEBOOK_POST_SCORE_URL, AccessToken));

            string json = JsonConvert.SerializeObject(score);

            var request = (HttpWebRequest)WebRequest.Create(fullUri);
            request.Method = "POST";
            request.ContentType = "application/json; charset=utf-8";

            using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
            {
                writer.Write(json);
            }

            request.BeginGetResponse(WebRequestCallback, new FacebookResult
            {
                Request = request,
                Response = response
            });

        }
        catch (ThreadAbortException)
        {
            throw;
        }
        catch (WebException ex)
        {
            if (response != null)
                response(FacebookResponse.NetworkError);
        }
        catch (Exception ex)
        { 
            if (response != null)
                response(FacebookResponse.OtherError);
        }

    }

我们正在使用 webViews 而不是 iOS / Android Facebook SDK,因为我们正在 Mono 中构建跨平台应用程序。

显然,我拥有访问令牌,并且该应用程序似乎拥有执行我想做的事情的完全权限,这是我在登录后允许的。任何想法表示赞赏!

4

1 回答 1

0

我最终(从一位同事那里)发现 Facebook 图形 api 不会采用 json 编码参数,所以我们对它进行了排序 -

string parameters = "score=" + score.Score;

var request = (HttpWebRequest)WebRequest.Create(fullUri);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";

using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
{
    writer.Write(parameters);
}

现在它工作正常 - 希望这会帮助其他人没有同样的问题。

于 2012-09-06T10:54:00.383 回答