Facebook Oembed returns unsupported browser page for HttpRequestMessage with UserAgent and Referrer

|▌冷眼眸甩不掉的悲伤 提交于 2020-06-16 04:18:11

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!