简体   繁体   English

如何使用控制台应用程序使用Facebook graph api在我自己的facebook页面上发布照片?

[英]How can I post photo on my own facebook page with facebook graph api using console application?

I want to share photo via graph api using console application but I can't. 我想使用控制台应用程序通过图形API共享照片,但是我不能。

    public static void Main(string[] args)
    {

        var facebookClient = new FacebookClient();
        var facebookService = new FacebookService(facebookClient);
        var getAccountTask = facebookService.GetAccountAsync(FacebookSettings.AccessToken);
        Task.WaitAll(getAccountTask);
        var account = getAccountTask.Result;
        Console.WriteLine($"{account.Id} {account.Name}");

        var postOnWallTask = facebookService.PostOnWallAsync(FacebookSettings.AccessToken,"Hello from C# .NET Core!");
        Task.WaitAll(postOnWallTask);
        //Console.WriteLine("Hellooooooo");
        RunAsync().Wait();
        //Console.WriteLine("byeeee");
        Console.ReadLine();
    }
    public static async Task RunAsync()
    {
        // Use HttpClient
        using (var client = new HttpClient())
        {

            string uri = "https://graph.facebook.com/v3.3/me/photos";

            //string link = "http://www.msdevz.com/news/article.aspx?id=5899&o=3";
            string picture = "https://images.unsplash.com/photo-1518791841217-8f162f1e1131?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&w=1000&q=80.png";
            string caption = "test photo";
            // Formulate querystring for graph post
            StringContent queryString = new StringContent("&picture=" + picture + "&caption=" + caption);

            // Post to facebook /{page-id}/feed edge

            var responsePost = "";
            try
            {
                HttpResponseMessage response = await client.PostAsync(new Uri(uri), queryString);
                if (response.IsSuccessStatusCode)
                {
                    Console.WriteLine("post is shared on your page");
                    // Get the URI of the created resource.
                    string postId = await response.Content.ReadAsStringAsync();
                    Console.WriteLine(postId);
                    Console.ReadLine();
                }
            }
            catch (Exception ex)
            {
                responsePost = "Facebook Posting Error Message: " + ex.Message;
            }

        }
    }

I think there is a problem with postAsync function. 我认为postAsync函数有问题。 I am not getting error, but this code does not share photos on my page. 我没有收到错误消息,但是此代码未共享页面上的照片。

Uploading a photo is explained here: https://developers.facebook.com/docs/graph-api/reference/user/photos 上传照片的说明如下: https : //developers.facebook.com/docs/graph-api/reference/user/photos

According to that page, you need to add the photo as a parameter with key 'url'. 根据该页面,您需要使用“ url”键将照片添加为参数。

Can you try something like this? 你可以尝试这样的事情吗?

string uri = "https://graph.facebook.com/v3.3/me/photos";
string picture = "https://images.unsplash.com/photo-1518791841217-8f162f1e1131?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&w=1000&q=80.png";
var parameters = new Dictionary<string, string> { { "url", picture } };
var encodedContent = new FormUrlEncodedContent (parameters);
HttpResponseMessage response = await client.PostAsync(new Uri(uri), encodedContent);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM