如何使用C#查询Facebook评论插件评论信息? (跟进)

时间:2017-02-15 10:52:29

标签: facebook-graph-api c#-4.0 facebook-comments

这是this question的后续内容。

使用以下代码:

mapPartitions

// this is to manually initiate web fetch (as opposed to Facebook client) public static String GetUrlText(String url) { var request = (HttpWebRequest)WebRequest.Create(url); request.UserAgent = DefaultUserAgent; if (UseProxyHost) { request.Proxy = Proxy; request.Credentials = CredentialCache.DefaultNetworkCredentials; } using (var response = request.GetResponse()) using (var responseStream = response.GetResponseStream()) { Debug.Assert(responseStream != null, "responseStream != null"); using (var sr = new StreamReader(responseStream)) { string ret = sr.ReadToEnd(); return ret; } } } // initialization string oauthUrl = $"https://graph.facebook.com/oauth/access_token?type=client_cred&client_id={appId}&client_secret={appSecret}"; string accessToken = client.DownloadString(oauthUrl).Split('=')[1]; var fbClient = new FacebookClient(accessToken); var pageUrl = HttpUtility.UrlEncode("https://developers.facebook.com/docs/plugins/comments"); var getUrl = $"https://graph.facebook.com/v2.6/?fields=og_object{{comments}}&id={pageUrl}&access_token={accessToken}"; var urlText = GetUrlText(getUrl); // getUrl can skip accessToken var fbComments = fbClient.Get(getUrl); urlText都会收到如下所示的JSON内容。

我可以在Google Chrome中获得正确的结果(fbComments的复制粘贴),但我在IE11和C#代码中收到400错误,其中包含以下内容:

pageUrl

问题:如何正确请求数据,以便在IE11和C#中都能正常工作?

1 个答案:

答案 0 :(得分:0)

我弄明白为什么它不起作用。我的请求需要以下标题:

Accept-Language: en-US,en;q=0.8,ro;q=0.6

整个请求构造代码:

var request = (HttpWebRequest)WebRequest.Create(url);
// can be omitted (included to look like a real browser)
request.UserAgent = DefaultUserAgent;
// required
request.Headers.Add("Accept-Language", "en-US,en;q=0.8,ro;q=0.6");