问题
From my ASP.Net Core MVC app, I am trying to fetch OEmbed JSON from Facebook using server-side request from my local machine, but I'm still getting an unsupported browser page. What else do I need to pass?
oEmbedUrl is "https://www.facebook.com/plugins/post/oembed.json/?url=" + a post URL.
var request = new HttpRequestMessage(HttpMethod.Get, oEmbedUrl);
request.Headers.AcceptLanguage.TryParseAdd("en");
request.Headers.UserAgent.TryParseAdd("Mozilla / 5.0(Windows; U; WindowsNT 5.1; en - US; rv1.8.1.6) Gecko / 20070725 Firefox / 2.0.0.6");
request.Headers.Referrer = new Uri("http://www.google.com");
request.Headers.TransferEncoding.TryParseAdd("gzip");
request.Headers.TransferEncoding.TryParseAdd("deflate");
var client = _clientFactory.CreateClient();
var response = await client.SendAsync(request);
if (response.IsSuccessStatusCode)
{
return await response.Content.ReadAsStringAsync();
}
回答1:
I did a simple test and it seems to work. Make sure the post is public and all parameters are URL encoded.
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(@"https://www.facebook.com/plugins/video/oembed.json/?url=https%3A%2F%2Fwww.facebook.com%2Ffacebook%2Fvideos%2F10153231379946729%2F");
request.UserAgent = "Mozilla / 5.0(Windows; U; WindowsNT 5.1; en - US; rv1.8.1.6) Gecko / 20070725 Firefox / 2.0.0.6";
request.Referer = "http://www.google.com";
request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
using (Stream stream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(stream))
{
string json = reader.ReadToEnd();
}
来源:https://stackoverflow.com/questions/61902621/facebook-oembed-returns-unsupported-browser-page-for-httprequestmessage-with-use