Issues publishing image to Facebook Page via Graph API SDK .NET - c#

Attempting this on .NET Core 3.1
After several hours of trying to publish a photo to my public facebook page (as in business page) of which I am the admin and have a long-term token for...
I keep getting the following response to my request using the official Facebook SDK for .NET. However, the image itself is never loaded.
{{"id":"786692942226147","post_id":"113260773923227_786692942226147"}}
The request looks like
var imageMedia = new FacebookMediaObject { FileName = file.FileName, ContentType = file.ContentType };
var stream = file.OpenReadStream();
var bytes = await stream.ReadAsByteArrayAsync();
imageMedia.SetValue(bytes);
var fbClient = new FacebookClient(credential.Token)
{
AppId = _config.GetValue<string>("FacebookClientId"),
AppSecret = _config.GetValue<string>("FacebookSecret")
};
dynamic parameters = new ExpandoObject();
parameters.message = request.Message;
parameters.file = imageMedia;
var result = await fbClient.PostTaskAsync($"{credential.PageId}/photos", parameters, token);
I'm sure it has something to do with the parameters I'm passing in, like parameters.file... but the docs for this thing are VERY unclear... as in "literally does not exist"
Anyone with experience getting this working, please point me in the right direction?

The solution is to change parameters.file to parameters.image...

Related

Facebook .NET SDK Posting image to Facebook page

i am using facbook .net sdk in wpf application to post an image to facebook page.
string photoAlbumID = <page id>;
FacebookMediaObject facebookUploader = new FacebookMediaObject { FileName = "1.png", ContentType = "image/png" };
var bytes = File.ReadAllBytes(System.IO.Path.GetDirectoryName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName)+"\\" + facebookUploader.FileName);
facebookUploader.SetValue(bytes);
var postInfo = new Dictionary<string, object>();
postInfo.Add("message", "test photo");
postInfo.Add("image", facebookUploader);
postInfo.Add("access_token", <page tocken>);
var fbResult = client.Post("/" + photoAlbumID + "/photos", postInfo);
dynamic result = (IDictionary<string, object>)fbResult;
the problem is the image is being uploaded but it is only visible by the page admin knowing that the post is public, and when i post it directly from facebook via internet browser it is visible.
Solved, the problem was the application was not published yet .. it was on the developer mode that makes no one can see the posts except the application admins.

Post Image on Instagram with API using c#

I am building a Asp.net C# application i want to post the user uploaded picture directly to Instagram, but after a quick search i can not found any function in the API somebody help me how to post image on instagram wall by C# code
Below Code I can Get Access Token
private void Authentication()
{
string rest = string.Empty;
GlobusInstagramLib.Authentication.ConfigurationIns config = new GlobusInstagramLib.Authentication.ConfigurationIns("https://instagram.com/oauth/authorize/", ConfigurationManager.AppSettings["consumerKey"], ConfigurationManager.AppSettings["consumerSecret"], ConfigurationManager.AppSettings["callbackurl"], "https://api.instagram.com/oauth/access_token", "https://api.instagram.com/v1/", "");
oAuthInstagram _api = oAuthInstagram.GetInstance(config);
rest = _api.AuthGetUrl("likes+comments+basic+relationships");
Response.Redirect(rest);
}
// Call back Url
public ActionResult Instagram()
{
string code = (String)Request.QueryString["code"];
oAuthInstagram objInsta = new oAuthInstagram();
GlobusInstagramLib.Authentication.ConfigurationIns configi = new GlobusInstagramLib.Authentication.ConfigurationIns("https://api.instagram.com/oauth/authorize/", ConfigurationManager.AppSettings["consumerKey"], ConfigurationManager.AppSettings["consumerSecret"], ConfigurationManager.AppSettings["callbackurl"], "http://api.instagram.com/oauth/access_token", "https://api.instagram.com/v1/", "");
oAuthInstagram _api = new oAuthInstagram();
_api = oAuthInstagram.GetInstance(configi);
AccessToken access = new AccessToken();
access = _api.AuthGetAccessToken(code);
string accessToken = access.access_token;
string id =access.user.id;
ViewBag.accessToken = accessToken;`enter code here`
ViewBag.Uid = id;
return View();
}
Below Code i have get Access Token and Profile ID
How To Post Image ? some body tall me
You cannot upload photos to Instagram through the API. From Instagram's API documentation https://instagram.com/developer/endpoints/media/:
At this time, uploading via the API is not possible. We made a
conscious choice not to add this for the following reasons:
Instagram is about your life on the go – we hope to encourage photos from within the app.
We want to fight spam & low quality photos. Once we allow uploading from other sources, it's harder to control what comes into the
Instagram ecosystem. All this being said, we're working on ways to
ensure users have a consistent and high-quality experience on our
platform.

Post message from Website to Facebook wall

I have a website made with ASP.NET webform .NET 4.5 C#. This site contains a forum(homemade by me), parts of this forum needs to be posted to the specific facebook wall(made for this webpage). What I need :
Post just created thread(message) from specific part of the forum to the corsponding facebook wall.
Optional : Sync the forum thread on webpage with message/comments on the specific facebook page
I have looked at these guides :
http://www.codeproject.com/Articles/569920/Publish-a-post-on-Facebook-wall-using-Graph-API
http://www.c-sharpcorner.com/UploadFile/raj1979/post-on-facebook-users-wall-using-Asp-Net-C-Sharp/
But im not sure that this is really the solution for me? I have tried to follow the guide but it does not look the same.
Edit :
dynamic result;
//https://developers.facebook.com/tools/explorer/
//https://developers.facebook.com/tools/access_token/
FacebookClient client = new FacebookClient(ConfigurationManager.AppSettings["FacebookAppToken"]);
//result = client.Get("debug_token", new
//{
// input_token = ConfigurationManager.AppSettings["FacebookAppToken"],
// access_token = ConfigurationManager.AppSettings["FacebookAppToken"]
//});
//result = client.Get("oauth/access_token", new
// {
// client_id = ConfigurationManager.AppSettings["FacebookAppId"],
// client_secret = ConfigurationManager.AppSettings["FacebookAppSecret"],
// grant_type = "client_credentials",
// //redirect_uri = "http://www.MyDomain.net/",
// //code = ""
// });
result = client.Get("oauth/access_token", new
{
client_id = ConfigurationManager.AppSettings["FacebookAppId"],
client_secret = ConfigurationManager.AppSettings["FacebookAppSecret"],
grant_type = "client_credentials"
});
client.AccessToken = result.access_token;
result = client.Post("[IdOfFacebookPage]/feed", new { message = "Test Message from app" });
//result.id;
result = client.Get("[IdOfFacebookPage]");
return false;
Approach to post to a facebook wall:
You need to register in facebook developer tools.
Create a app (fill a form).
Download FGT 5.0.480 (Facebook graph toolkit)
Reference FGT dlls in your web application.
FGT has methods to post to facebook wall but needs appId.
Use the app id of your app created in step 2.
To post to an user's wall through facebook, it is only possible through an app.
Try this asp .net app, which will allow you to post in your wall:
https://apps.facebook.com/aspdotnetsample/?fb_source=search&ref=br_tf
This will allow you to envision what you need.
Your app gets an appId with which you need to generate auth token.
Limitation: The user's wall where you want to post should have added the app created at step 2, this is compulsory and there is no work around.
When the user accesses the app for the first time, it automatically asks for permission to post to wall--> the user needs to accept it.
Hope this gives you an idea on how to go about doing this.
You can then post into the user's wall.
For Banshee's below request using Facebook SDK for .NET:
userId - facebook user id, wall to post the message
This user should have added the app created by you in step 1 else it will say unauthorized access.
dynamic messagePost = new ExpandoObject();
messagePost.picture = "http://www.stackoverflow.com/test.png";
messagePost.link = "http://www.stackoverflow.com";
messagePost.name = "This is a test name";
messagePost.caption = "CAPTION 1";
messagePost.description = "Test desc";
messagePost.message = "message"
var fb = new FacebookClient();
dynamic result = fb.Get("oauth/access_token", new {
client_id = "app_id",
client_secret = "app_secret",
grant_type = "client_credentials"
});
fb.AccessToken = result.access_token;
try
{
var postId = fb.Post(userId + "/feed", messagePost);
}
catch (FacebookOAuthException ex)
{
//handle oauth exception
}
catch (FacebookApiException ex)
{
//handle facebook exception
}
You will need an extended token to publish to a wall.. Steps to get extended token is explained well by Banshee..
Edit: How to get extended token by Banshee:
Please follow this post
By creating a extended page token and use it to make the post everything works just fine. See this : How to get Page Access Token by code?
Im surprised that this simple task was so hard to get running and that there was vary little help to get.

Trying to post message to facebook using windows client app

I am using visual studio 2013 .NET 4.5 and C# with a windows forms client application, I am trying to post a message to my own Facebook managed page. I have created a new Facebook application, but not yet published it, I am unsure of the settings, but I have the app_id, app_secret and from debugging I have the access_token.
I used a piece of code to open a browser window to login with permissions and this was successful/
I am getting the following error:
OAuthException - #200) (#200) The user hasn't authorized the application to perform this action
The code I am using is below:
var fb = new FacebookClient("AUTH_CODE")
dynamic parameters = new ExpandoObject();
parameters.message = "hello tweet tweet";
dynamic result = fb.Post("SITE_ID/feed", parameters);
var id = result.id;
for logging in I am using:
var client = new FacebookClient();
client.AppSecret = app_secret;
client.AppId = app_id;
client.AccessToken = "AUTH_CODE";
var fbLoginUri = client.GetLoginUrl(new
{
client_id = app_id,
redirect_uri =
"https://www.facebook.com/connect/login_success.html",
response_type = "code",
scope = "manage_pages,offline_access,publish_stream,publish_actions"
});
System.Diagnostics.Process.Start(url);
I am already logged in and the message on the browser shows SUCCESS...
The DLL that I have referenced is the latest v6.0 Facebook C# SDK
Do I need to setup the Facebook app so that it has permissions to post onto my managed wall and if so how do I do it.
I hope someone out there can help, I have been looking at this for days and I need a little help.
PS:
I have also tried the following code:
var fb = new FacebookClient();
dynamic result = fb.Get("oauth/access_token", new
{
client_id = app_id,
client_secret = app_secret,
grant_type = "client_credentials",
scope = "manage_pages,offline_access,publish_stream"
});
var app_token = result.access_token;
var client = new FacebookClient(app_token);
dynamic parameters = new ExpandoObject();
parameters.message = "Check out this funny article";
parameters.link = "http://www.natiska.com/article.html";
parameters.picture = "http://www.natiska.com/dav.png";
parameters.name = "Article Title";
parameters.caption = "Caption for the link";
//446533181408238 is my fan page
client.Post("/446533181408238/feed", parameters);
Paul.
It seems that the request code from your access token request is missing which probably causes this error, see this question for more details on this.

Can't send Notifications using C# Facebook SDK

I am trying to send a notification (not app requests) to registered users, server-side using the C# Facebook SDK. I actually get a success message, but no notification is sent. Our app is not in sandbox and has canvas enabled. The code below show attempts to send both notifications and app requests--the notifications do nothing, but the app request works:
var fb = new FacebookClient();
dynamic result = fb.Get("oauth/access_token", new
{
client_id = "MY_APP_KEY",
client_secret = "MY_SECRET",
grant_type = "client_credentials"
});
fb.AccessToken = result.access_token;
dynamic nparams = new ExpandoObject();
nparams.template = "Hello from my app!";
nparams.href = "Home";
dynamic requestParams = new ExpandoObject();
requestParams.message = "Hi there";
requestParams.title = "Please use this awesome app";
foreach (string facebookID in facebookIDs)
{
// This returns success:true, but doesn't actually do anything
var postResult = fb.Post(facebookID + "/notifications", nparams);
// This works and shows a request in the app center
var postResult2 = fb.Post(facebookID + "/apprequests", requestParams);
}
The fact that apprequests send here tells me the access token is ok, so why don't notifications work? We really need to do this server-side--has anyone been able to do that?
Got a response from facebook on this . . . basically, that this feature is in beta and it may just not work, despite giving Success responses
Good snippet Anthony. Appears to be working consistently now. +1 for your attempt.
PS: In case anyone stumbles upon this code, "facebookID" is a numeric Id and can be derived using the following link http://findmyfacebookid.com/

Categories

Resources