如何在Facebook粉丝页面专辑上传图片如果相册不存在则创建新的&使用C#

时间:2015-06-18 11:59:10

标签: c# facebook facebook-c#-sdk

我想通过提供文件夹的路径来上传Facebook粉丝页面相册中的图片,如果相册不存在,我想创建新相册并将图片上传到其中。

我已将错误分配给我在代码中获得的异常。

  1. (OAuthException - #2500)未知路径组件:/ Page ID / photos

  2. 工作但不发布到相册而是直接发布。

  3. (OAuthException - #100)(#100)来源格式不正确

  4. (OAuthException - #100)(#100)相册所有者的ID无效

  5. 请参阅以下代码注释,了解上述例外情况。

        int flag = 0;
        string albumId = string.Empty;
        var app = new FacebookClient(AccessToken);
    
       //Get all the albums
        dynamic albums = app.Get("PageID/albums");
        foreach(dynamic albumInfo in albums.data)
        {
            if (albumInfo.name == "Album Name")
            {
                // Find the desired album id 
                albumId= albumInfo.id;
                link = albumInfo.link;
                flag = 1;
                break;
            }
        }
    
        if (flag == 1)
        {
    
            flag = 1;
            string path = txtDir.Text;
            var files = Directory.GetFiles(path, "*.*", SearchOption.AllDirectories)
                .Where(s => s.EndsWith(".jpeg") || s.EndsWith(".jpg"));
    
            // Search created album and upload images from folder
            foreach (string fpath in files)
            {
                byte[] photo = File.ReadAllBytes(fpath);
                dynamic parameters = new ExpandoObject();
                parameters.access_token = AccessToken;
                parameters.message = "Message";
                string f = Path.GetFileName(fpath);
                var mediaObject = new FacebookMediaObject
                {
                    ContentType = "image/jpg",
                    FileName = Path.GetFileName(fpath)
    
                };
                mediaObject.SetValue(photo);
                parameters.source = mediaObject;
    
    
                1.  //-------------- Not Working    
                //var result = app.Post("/" + albumId + "/PageID/photos", parameters);
    
                2. //---------------Working but directly posting to photos & not to album
                //var result = app.Post("/PageID/photos/" + albumId + "/", parameters);
    
                3. //---------------Not Working 
                //var result = app.Post("/PageID/photos/" + albumId + "/", parameters);
    
    
                //---------------Working for Account not for page
               //var result = app.Post("/" + albumId + "/photos", parameters);
    
            }
        }
       else
       {
           //Code to Create Album
            FacebookClient facebookClient = new FacebookClient(AccessToken);
            var albumParameters = new Dictionary<string, object>();
            string albumName = "Album Name";
            string albumMessage = "Album Message";
            albumParameters["message"] = albumMessage;
            albumParameters["name"] = albumName;
    
            4.   //Not Working
            //facebookClient.Post("/PageId/albums", albumParameters);
    
             //Working for facebook account not page
             //facebookClient.Post("/me/albums", albumParameters);
    
             // Get all albums
             dynamic albums = app.Get("PageID/albums");
             // Search created album and upload images from folder
             foreach (dynamic albumdata in album.data)
             {
                if (albumdata.name == albumName)
                {
                    string albumid = albumdata.id;
    
                    string path = txtDir.Text;
                    //Get all the files from the directory
                    var files = Directory.GetFiles(path, "*.*", SearchOption.AllDirectories)
                        .Where(s => s.EndsWith(".jpeg") || s.EndsWith(".jpg"));
                    foreach (string fpath in files)
                    {
                        byte[] photo = File.ReadAllBytes(fpath);
                        dynamic parameters = new ExpandoObject();
                        parameters.access_token = AccessToken;
                        parameters.message = "Message";
                        var mediaObject = new FacebookMediaObject
                        {
                            ContentType = "image/jpg",
                            FileName = Path.GetFileName(fpath)
                        };
                        mediaObject.SetValue(photo);
                        parameters.source = mediaObject;
    
                       1.  //-------------- Not Working    
                       //var result = app.Post("/" + albumId + "/PageID/photos", parameters);
    
                      2. //---------------Working but directly posting to photos & not to album
                       //var result = app.Post("/PageID/photos/" + albumId + "/", parameters);
    
                      3. //---------------Not Working 
                      //var result = app.Post("/PageID/photos/" + albumId + "/", parameters);
    
    
                      //---------------Working for Account not for page
                      //var result = app.Post("/" + albumId + "/photos", parameters);
    
    
                    }
                }
            }
       }
    

0 个答案:

没有答案