Image not uploaded after publishing using facebook c# SDK - c#

I am using Facebook c# SDK to post status & images on users wall.
I am able to post status but image is not getting posted
Here is my code:
[HttpPost]
public ActionResult PostPhotoOnWall(HttpPostedFileBase file)
{
var client = new FacebookClient();
// Post to user's wall
var postparameters = new Dictionary<string, object>();
postparameters["access_token"] = Session["access_token"].ToString();
postparameters["picture"] = "http://localhost:8691/Content/themes/base/images/12WIPJ50240-2V91.jpg";
var result = client.Post("/me/feed", postparameters);
return View("PostPhoto");
}
On user wall status is posted without image.
Can any one help me .

I Solved It, Bellow is the code
[HttpPost]
public ActionResult PostPhotoOnWall(HttpPostedFileBase file)
{
var filename = Path.GetFileName(file.FileName);
var client = new FacebookClient();
// Post to user's wall
var postparameters = new Dictionary<string, object>();
var media = new FacebookMediaObject
{
FileName = filename,
ContentType = "image/jpeg"
};
var path = Path.Combine(Server.MapPath("~/Content/themes/base/images"),filename);
file.SaveAs(path);
byte[] img = System.IO.File.ReadAllBytes(path);
media.SetValue(img);
postparameters["source"] = media;
postparameters["access_token"] = Session["access_token"].ToString();
// postparameters["picture"] = "http://localhost:8691/Content/themes/base/images/12WIPJ50240-2V91.jpg";
var result = client.Post("/me/photos", postparameters);
return View("PostPhoto");
}

Related

Reading Multipart Data from Xamarin

We have a requirement of sending the jpeg files of a given directory to a Xamarin App.
Following is the code in the Web API.
public HttpResponseMessage DownloadMutipleFiles()
{
name = "DirectoryName";
var content = new MultipartContent();
var ids = new List<int> { 1,2};
var objectContent = new ObjectContent<List<int>>(ids, new System.Net.Http.Formatting.JsonMediaTypeFormatter());
content.Add(objectContent);
var file1Content = new StreamContent(new FileStream(#"D:\Photos\" + name+"\\"+ "BL1408037_20191031124058_0.jpg", FileMode.Open));
file1Content.Headers.ContentType = System.Net.Http.Headers.MediaTypeHeaderValue.Parse("image/jpeg");
content.Add(file1Content);
var file2Content = new StreamContent(new FileStream(#"D:\Photos\" + name + "\\" + "BL1408037_20191031124058_1.jpg", FileMode.Open));
file2Content.Headers.ContentType = System.Net.Http.Headers.MediaTypeHeaderValue.Parse("image/jpeg");
content.Add(file2Content);
var response = new HttpResponseMessage(HttpStatusCode.OK);
response.Content = content;
return response;
}
Can some one help out with how to read from Xamarin app? Thanks in advance
This is the function I was able to use to send an image as a multi part data file! I just took the byte array given to me by the Xamarin Essentials image picker and passed it into this function:
public async Task SubmitImage(byte[] image, string imageName)
{
using (var client = new HttpClient())
{
string url = $"..."; // URL goes here
var token = Preferences.Get("AccessToken", "");
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
var stream = new MemoryStream(image);
var content = new StreamContent(stream);
//Without a name we can't actually put the file in IFormFile. We need the equivalent
//"name" value to be "file" (used if you upload via an <input> tag). We could call it
//anything but file is simple
content.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
{
FileName = imageName,
Name = "file"
};
content.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg");
var multipartContent = new MultipartFormDataContent();
multipartContent.Add(content);
var result = await client.PostAsync(url, multipartContent);
}
}
You can test this using a console application as well and just send over a picture from your computer, instead of doing this through the app

How to post an Image file to a specific folder using ASP.NET WEB 2 Api controller

I can use GET and DELETE, but i'am struggeling with POST. I wanna post to a specific folder in the UWP app. Iam using image name is ID. I have this code for GET and DELETE.
//[Route("api/Image/{Name}")]
public HttpResponseMessage Get(string name)
{
string file =$"C:\\Users\\bjornma\\source\\repos\\HuntHelper_v3\\Images\\{name}";
var ext = new FileInfo(file).Extension;
ext = ext.Substring(1);
var img = File.ReadAllBytes(file);
var ms = new MemoryStream(img);
var respons = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StreamContent(ms)
};
respons.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue($"image/{ext}");
return respons;
}
public void Delete(string name)
{
string file = $"C:\\Users\\bjornma\\source\\repos\\HuntHelper_v3\\Images\\{name}";
File.Delete(file);
}

Google gdata pdf download and upload

My problem is only donwload. Tomorrow I have 2 problem they are download and upload. But I solve upload problem. I can share upload code this questions answer. But now only problem download
P.S =Client application
var authenticator = new ClientLoginAuthenticator("ApplicationName", ServiceNames.Documents, new GDataCredentials("Username", "Password"));
var service = new DocumentsService("ApplicationName");
var entry = new DocumentEntry();
entry.Title.Text = fileName;
entry.MediaSource = new MediaFileSource(filePath, "application/pdf");
var createUploadUrl = new Uri(String.Format(UploadPath, "uploadFileId"));
var link = new AtomLink(createUploadUrl.AbsoluteUri);
link.Rel = ResumableUploader.CreateMediaRelation;
entry.Links.Add(link);
entry.Service = service;
var uploader = new ResumableUploader();
var response = uploader.Insert(authenticator, entry);
return response.ResponseUri.AbsolutePath;
string UploadPath = "https://docs.google.com/feeds/upload/create-session/default/private/full/folder:{0}/contents";

Facebook C# SDK Post Image more than once

I am uploading or Posting Image to Facebook using Facebook C# SDK but I call this function one time but it uploads the same Image three times or more. It should only upload the Image one time but it does at least three times, I am using 5.4.1 SDK.
Code is:
public void AddCover(string accessToken, string imageName, string folder, string loggedinuserId)
{
FacebookClient facebookClient = new FacebookClient(accessToken);
var fbUpl = new Facebook.FacebookMediaObject
{
FileName = imageName,
ContentType = "image/jpg"
};
var bytes = System.IO.File.ReadAllBytes(#"F:\Websites\Covers\" + folder + "\\" + imageName);
fbUpl.SetValue(bytes);
var photoDetails = new Dictionary<string, object>();
photoDetails.Add("message", "Facebook Covers");
photoDetails.Add("image", fbUpl);
var response = facebookClient.Post(#"/" + loggedinuserId + "/photos", photoDetails);
var result = (IDictionary<string, object>)response;
var postedcoverId = result["id"];
}
Am I missing something here? Please see the code and tell me what I am doing wrong.
Thanks

How to post to user's wall from application's name?

I tried to post to users wall using application access token but it still posts from users name. Is it possible to post from application's name?
var fbc = new FacebookWebClient();
var wc = new WebClient();
var tokenStr = wc.DownloadString(string.Format("https://graph.facebook.com/oauth/access_token?client_id={0}&client_secret={1}&grant_type=client_credentials",
FacebookApplication.Current.AppId, FacebookApplication.Current.AppSecret));
fbc.AccessToken = tokenStr.Replace("access_token=", string.Empty);
dynamic parameters = new ExpandoObject();
parameters.message = "testMessage";
//parameters.link = pageLink;
parameters.name = "testCaption";
dynamic result = fbc.Post(string.Format("{0}/feed", userID), parameters);
var fbPost = new FacebookClient ( accessToken );
dynamic result = fbPost.Post ( "me/feed", new { message = "Welcome to C# SDK and me" } );

Categories

Resources