Trying to post photos to Facebook using Facebook C# SDK returns exception "Facebook.FacebookOAuthException" - c#

I am trying to upload photo to a page I have created. Unfortunately it returns me the following exception - A first chance exception of type 'Facebook.FacebookOAuthException' occurred in Facebook.dll.
You'd think that I have missed some permission in the "Graph API Explorer", but I have actually checked every single one of them and I am using that Access Token. Obviously the problem is connected to the Access Token, but I am not sure how.
Here is what I have tested and it works fine:
Posting messages on my personal wall
Posting photos in my personal album
Posting messages on my page
And yet when I try to post photos to my page album or to my page wall it returns me that exception.
I use the code from here:
FacebookClient facebookClient = new FacebookClient(accessToken);
dynamic parameters = new ExpandoObject();
parameters.access_token = accessToken;
parameters.source = new FacebookMediaObject
{
FileName = "My_Picture.jpg",
ContentType = "image/jpeg"
}.SetValue(File.ReadAllBytes(#"F:\\My_Picture.jpg"));
//"my" album ID is 12345
//page album id is 67890
facebookClient.Post("/67890/photos", parameters);
I have tried changing /photos to /feed, but it doesn't work either. I have also tried changing the 67890 (album ID) with my page name and page id.
I use VS 2013, C# with WPF, newest version of Facebook SDK C#.

So I reset the permissions and now everything works properly*.
*What I was looking for was posting the photo on the page as the page. Right now the code above posts a photo on the page as a normal user.

Related

Post to Facebook group via graph api c#

I am trying to post to a facebook group via the graph api in c#.
https://developers.facebook.com/docs/graph-api/reference/v2.2/group/feed
According to the api I can post a message as well as a link to a url, here is my code to try and do this:
Uri result;
bool X = Uri.TryCreate(url, UriKind.Absolute, out result)
if(X){
// POST to group FB
dynamic fbInfo = fb.Post("/v2.2/" + "groupID" + "/feed", new
{
message = websiteDesc,
link = url
});
var fbInfoJson = fbInfo.ToString();
}
First i check that the url is absolute and if so proceed to post to the facebook group.
so far this code does post to the group but only the message and not the link.
How can I get it to post the link?
Also the api says that I can include a photo to the post but it must be a string, can i assume this is the url of the image?
Thanks in advance :)
I am not entirely sure, but the docs say "Either link or message must be supplied" so maybe you can only post a message OR a link. Definitely worth to try. It must be an absolute URL, of course. Same goes for the picture.
Posting in a group is pretty hard nowadays anyway, since you would need user_groups and publish_actions for that - and you will not get user_groups approved so you canĀ“t use it for a public App. It will only work for users with a role in the App (Admin, Developer, Tester).

how to post on facebook wall for specific list of users using asp.net c#?

I am trying to post on specific group of users on facebook such as (close friends, or family, or college friend...) and I used the code bellow.
code that I used:
1
FacebookClient fpost1 = new FacebookClient(access_token);
fpost1.Post("/1234567890/feed", new { message = "test post"});
note: access_token is working correctly when I am doing some job before this exception.
I put my friendlist id instead of 1234567890, that you can get it from graph .../me?fields=friendlists
it did not work and gave me this error "(OAuthException - #2) An unexpected error has occurred. Please retry your request later."
2
FacebookClient fpost1 = new FacebookClient(access_token);
fpost1.Post("/me/feed", new { message = "it is very cold.", to="1234567890"});
this one work, but it post to "only me" as target.
thank you
It looks to me that what you are doing here...
FacebookClient fpost1 = new FacebookClient(access_token);
fpost1.Post("/1234567890/feed", new { message = "test post"});
is wrong. Because I believe that 1234567890 is a user-id, right? Not a friendslist-id. According to the documentation this edge/endpoint signature goes like....
/{user-id}/feed
where user-id is obviously a user id. The documentation states that...
Most nodes in the Graph API have edges that can be published to (such as Photos or Posts). All Graph API publishing is done simply with an HTTP POST request to the relevant endpoint with any necesssary parameters included. For example, if you wanted to publish a post on behalf of someone, you would make an HTTP POST request as below:
POST graph.facebook.com
/{user-id}/feed?
message={message}&
access_token={access-token}
Notice that it says "On Behalf of Someone". My understanding is that you are publishing on behalf of someone and to do that, this someone must have requested an access_token through your application. In other words, if this user hasn't logged in to your app and generated a valid access token you cannot publish on his/her wall
POST graph.facebook.com
me/feed?message="hello"&privacy={"value": "CUSTOM", "allow": "1234567890"}
where the 1234567890 is one of friendlists id

Facebook C# SDK - Post to wall

I'm developing an asp.net MVC 3 Facebook app and I am trying to post a message to my wall. Here is my code:
FacebookWebClient client = new FacebookWebClient();
// Post to user's wall
var postparameters = new Dictionary<string, object>();
postparameters["message"] = "Hello world!";
postparameters["name"] = "This is a name";
postparameters["link"] = "http://thisisalink.com;
postparameters["description"] = "This is a description";
var result = client.Post("/me/feed", postparameters);
I can get the access token using client.AccessToken, so I'm assuming I don't have to set it anywhere. This code produces no errors and for the result I get an ID. However, when I bring up my Facebook, I see nothing on my wall nor in my news feed. I'm not sure what I'm missing. I've looked at related questions here at StackOverflow, but I see no reports/questions similar to mine. I've also tried changing the code based on what I've seen in other posts, but to no avail. I also checked my Facebook account settings and I see my application listed with permission to post to my wall. I also tried posting a message to my wall via the Graph API explorer and I'm getting the same result. I get an ID in return, but when I check my Facebook account I see nothing. At been at this for a couple of days. Any help would be greatly appreciated.
Thanks in advance.
[EDIT]
I wonder if something is wrong with my app generated access_token. Using this access_token, as I mentioned in my post, I get the same result using the Graph API explorer. An ID is returned, but no message on my wall. However, if I give the Graph API explorer permission to post to my wall and use its own generated access_token, I can successfully post a message using the explorer. Here's the FB login button code:
<div>
<h1>Login using Facebook</h1>
<p><fb:login-button perms="user_location, publish_stream, email"></fb:login-button></p>
</div>
Basically you need to add an additional parameter into your post parameters.
args["access_token"] = account.access_token;
this is the token of the specific page.
I wont repeat the code, follow here for example: Post On Facebook Page As Page Not As Admin User Using Facebook C# SDK
(second answer)
Did you request right App permissions to Facebook in your action method?
Try: [CanvasAuthorize(Permissions = "user_location, publish_stream, email")]
Did you append the access_token to the URL? See example here. It's also documented here (see Using the Access Token).
/me/feed?access_token=<access_token>
with one small change, your code works fine for me. you have to pass the users auth token into the constructor like this...
var client = new FacebookClient(accessToken);
// Post to user's wall
var postparameters = new Dictionary<string, object>();
postparameters["message"] = "Hello world!";
postparameters["name"] = "This is a name";
postparameters["link"] = "http://thisisalink.com;
postparameters["description"] = "This is a description";
var result = client.Post("/me/feed", postparameters);
this method works for me, although i am not sure which facebook sdk you are using.
i'm using http://facebooksdk.net/ its quite good so if you're not using it i would recommend it

facebook c# sdk: deleting a request-id

I am using the latest facebook c# sdk (http://facebooksdk.codeplex.com/). After i have sent an apprequest, i want to delete the request id.
This is how i do it at the moment:
var app = new FacebookClient(appid, appsecret);
app.Delete(requestID);
But i am not sure if its get deleted or not. If i try to see if it still exist using the graph api i get:
{
"error": {
"type": "GraphMethodException",
"message": "Unsupported get request."
}
}
But the user still has the request in his notification area. So my question is> Is the request deleted, or did i miss something? Thanks
var url = "https://graph.facebook.com/{0}?access_token={1}";
fb.Delete((String.Format(url, fullRequestId, fb.AccessToken)));
First parameter is requestId and user id like -> fullRequestId = requestId + "_" + fbUser.id
Second parameter is Accesstoken
I'm just getting started on this myself, but I'm guessing that you need to instantiate the FacebookClient with the authorization code from the user, not with your application data. The way I understand it, the request is sent by the user not your application. Hence the need to use the users authorization code to get information about the requeset.
This is what's working for me (sorry it's VB.Net):
Dim fb As FacebookClient = New FacebookClient(Config.FacebookAppId,Config.FacebookAppSecret)
Dim result = fb.Delete(String.Format("{0}_{1}?access_token={2}", facebookRequestId, facebookUserId, fb.AccessToken))

Windows phone 7 and uploading photo to facebook wall

I am having a problem uploading a photo to a facebook wall. The post shows up on the wall, and the result returned from the apiasync call has no error, but the photo is not there :(
I am using the facebook sdk from http://facebooksdk.codeplex.com
Here is my code, any help would be appreciated!!!
var photo = new WriteableBitmap(0, 0).FromResource("Background200x200.jpg");
FacebookClient app = new FacebookClient();
IDictionary<string, object> parameters = new Dictionary<string, object>();
parameters["access_token"] = _facebookAccessToken; //set in another method where I authenticate...
parameters["name"] = "my picture";
parameters["message"] = "this is a picture uploaded from my the facebook sdk";
var mediaObject = new FacebookMediaObject {
FileName = "Background200x200.jpg",
ContentType = "image/jpeg",
};
mediaObject.SetValue(photo.ToByteArray());
parameters["source"] = mediaObject;
app.ApiAsync(
UploadComplete,
null,
"https://graph.facebook.com/me/feed",
parameters,
HttpMethod.Post);
Not sure if you're still having this problem, but I'll answer to help future users if you aren't.
The problem is the way you're doing the upload. You need to first upload the picture to a photo album using the /me/photos connection (me can be replaced with the User_ID as well). Once you get the resulting PHOTO ID from the upload you can then make a subsequent call to /me/feed and post the image to the user's wall using the object_attachment parameter.
Both would be done via HTTP POST
You can reference these two documentation pages:
How to upload image via the GraphAPI
Graph API POST reference
Note: As of right now, the object_attachment parameter is undocumented, but works as long as you post a POST and not a STATUS MESSAGE.

Categories