Show photos from my facebook page on my web site - c#

I have some doubts:
I want to show the photos from my company facebook page on my company web site using C# SDK.
How can I do that with an access token which never expires? Or why my app id and app secret is not enough?
var fb = new FacebookWebClient("app_id", "app_secret");
If I only use this it says:
An active access token must be used to query information about the current user.
Basically I see non-sense to ask for a valid access token because it's not about to interact with any of my visitors' page but just simply show my photos from my company facebook page to them.
UPDATE: To get my app token I am doing:
var oauthClient = new FacebookOAuthClient
{
AppId = "app_id",
AppSecret = "app_secret"
};
dynamic result = oauthClient.GetApplicationAccessToken();
accessToken = result.access_token;
But now, how I can show photos from my account?
If I do it like:
var fb = new FacebookWebClient(accessToken);
//Get the album data
dynamic albums = fb.Get("app_id/albums");
I receive an empty JSON but I am sure that my account on facebook has several albums.

Tokens can and do expire. You must have code in place to handle when it does.
Your appid and app secret are not enough because at least one of the page admins MUST grant access to the page to get photo data via the Graph API.
Remember, the API is not only Facebook's sandbox, but they also are the ones who built it, so you're going to have to play by their rules.

Related

Saving Tweetinvi credentials without re-authenticating every time

Basically what I'm trying to do is to get recent tweets from a user and do stuff with them. I'm using Tweetinvi with PIN-based authentication, as described on the website, like this:
// Create a new set of credentials for the application
var appCredentials = new TwitterCredentials("CONSUMER_KEY", "CONSUMER_SECRET");
// Go to the URL so that Twitter authenticates the user and gives him a PIN code
var url = CredentialsCreator.GetAuthorizationURL(appCredentials);
// This line is an example, on how to make the user go on the URL
Process.Start(url);
// Ask the user to enter the pin code given by Twitter
var pinCode = Console.ReadLine();
// With this pin code it is now possible to get the credentials back from Twitter
var userCredentials = CredentialsCreator.GetCredentialsFromVerifierCode(pinCode, appCredentials);
// Use the user credentials in your application
Auth.SetCredentials(userCredentials);
Now the problem is that I have to sign in and connect to Twitter every time I launch my application via browser, which is mildly annoying. I've tried to save my authentication details in a text file (Consumer Key, Consumer Secret, Access Token, Access Token Secret), and then just insert the info into appCredentials and userCredentials, but with no results, as I keep getting TwitterNullCredentialsException. So how do I save my credentials so that I don't have to reconnect on every launch?
I am the main developer of Tweetinvi.
If you store the 4 credentials information you can then reuse them with 2 different solutions :
Auth.SetUserCredentials("CONSUMER_KEY", "CONSUMER_SECRET", "ACCESS_TOKEN", "ACCESS_TOKEN_SECRET");
// OR
var creds = new TwitterCredentials("CONSUMER_KEY", "CONSUMER_SECRET", "ACCESS_TOKEN", "ACCESS_TOKEN_SECRET");
Auth.SetCredentials(creds);
The documentation might help you set up your application : https://github.com/linvi/tweetinvi/wiki/Introduction

Facebook SDK for .NET: Uploading pics to a page from backend

What I am looking to achieve.
I have a WPF application where in, I will be getting access to some pics generated on run time and I wanna post these pics to an album on FB Page(Think server to server interaction ). This process is supposed to be completely self reliant without any input from users. So I did some research and figured you gotta create an app to be able to do it. On further research the process seems to be as follows:
1) Using app secret and app id , get the app access token
2) Get the user access token
3) using this user access token, call {userId}/accounts and from the resulting JSON parse the page id and get the Page Access token.
Now I have successfully retrieved the App Access token and now I am not able to figure out how to get UserAccessToken without which I cant proceed to step 3.
I am using C# SDK for facebook and here is some code
Step 1.
var fbAccess = new FacebookClient();
dynamic resultAccess = fbAccess.Get("oauth/access_token", new
{
client_id = "XXXXXXXXX",
client_secret = "XXXXXXXXXXX",
grant_type = "client_credentials",
redirect_uri = "",
perms = "manage_pages, photo_upload, publish_stream"
});
String app_access_token = resultAccess["access_token"];
Step 2:
FacebookClient fbClient = new FacebookClient(app_access_token );
dynamic resultAccess = fbClient.Get("/{UserID}/accounts", new
{
client_id = "XXXXXXX",
client_secret = "XXXXXXXXXXXXXXXXX",
perms = "manage_pages, photo_upload, publish_stream"
});
The error I am getting is "A user access token is required to request this resource." I have already given the required permission to the app using Graph API Explorer.
Thanks in Advance

Facebook Posting Photo using App Access Token

Scenario:
User has completed the OAuth process and has given our app publish_actions, publish_stream, user_friends, and user_photos permissions.
While we have the user token from the user logging in, we have retrieved and persisted the user's Facebook id
Also while we have the user token, we have created a photo album to later post photos to and persisted the Facebook album id
NOTE: We have not persisted the user token for legacy reasons
At a later time, we attempt to post a photo to the album previously created using the following code:
FacebookClient client = null;
Dictionary<string, object> parameters = null;
string path;
FacebookMediaObject media = null;
client = new FacebookClient();
client.AppId = "{our app id}";
client.AppSecret = "{our app secret}";
media = new FacebookMediaObject();
media.ContentType = "image/jpg";
media.FileName = "AnyAllContactTypesProblem.jpg";
media.SetValue(File.ReadAllBytes(Server.MapPath(".") + "\\" + media.FileName));
parameters = new Dictionary<string,object>();
parameters.Add("name",
"Test Image 1");
parameters.Add("source",
media);
path = "/" + "{album id}" + "/photos";
dynamic result = client.Post(path,
parameters);
The POST operation results in the following exception:
(OAuthException - #102) A user access token is required to request this resource.
The Facebook documentation indicates that an "app token" which is derived from the app id and app secret can be used as the access token for posting a photo on someone's behalf as long as publish_stream permission has been provided to the app which it has in our case.
Are we missing something needed in the code above or has Facebook changed the required permissions for posting photos?
Thanks in advance.
I don't know from where did you read this-
an "app token" which is derived from the app id and app secret can be used as the access token for posting a photo on someone's behalf as long as publish_stream permission has been provided to the app which it has in our case.
But this is not true. Once you got the publish permission, you can post the stories/feeds on the wall and get the basic info using the app access token. Posting the photos is not allowed! Here's what the documentation says-
App access tokens can also be used to publish content to Facebook on behalf of a person who has granted an open graph publishing permission to your application. Reference
One thing that you can do-
Save the extended token to your server (that expires in 60-days, after that user must go through the login again to get the new token), to get the extended token-
GET /oauth/access_token?
grant_type=fb_exchange_token&
client_id={app-id}&
client_secret={app-secret}&
fb_exchange_token={short-lived-token}

How to get the facebook user token with a facebook application

I have a facebook application with has to post updates on specific facebook page. The application has rights to post updates on the facebook wall of the page's admin. But how can i get the (admin's) users access token, which i need to get the access token of the page. Do i have to save the access tokenthe first time the user allows the app to post (offline access)?
Thanks for your reply!
I use the Facebook C# SDK as well. You'll need the following permissions:
manage_pages
read_stream
publish_stream
HTTP Get to "me/accounts" and get the .data object off of the returned value. This will be a list of the following:
var list = ApiGet("me/accounts").data;
foreach (dynamic acc in list)
{
var name = (string)acc.name;
var accessToken = (string)acc.access_token;
var category = (string)acc.category;
var pageId = (string)acc.id;
}
As you can see, each one of the pages have their own unique access token.

Auto Facebook OAuth from ASP.NET C#

I need help on oAuth authentication to facebook. I am able to do the steps that facebook API mentioned and successfully authenticated, get my profile information as well as post to my news feed.
I want my application to login automatically rather than routing to Facebook login page on behalf of a user(i.e. get credentials from db and do auto oauth) . So that the user can type a message on application and click on button should submit a post to facebook page.
Thanks
What you need to get is an access token. This is a key which allows you continued access to the account until your the token expires or the user cancels it.
When you first authenticate if you have set the required permissions you get an authtoken which you can store and use to initiale further calls.
var client = new FacebookClient("my_access_token");
dynamic result = client.Get("19292868552_118464504835613");
string id = result.id;
string fromName = result.from.name;
string fromCategory = result.from.category;
string message = result.message;
int likes = result.likes;
foreach (dynamic comment in result.comments.data) {
string commentId = comment.id;
string commentMessage = comment.message;
}
see this article for details about the process:
http://benbiddington.wordpress.com/2010/04/23/facebook-graph-api-getting-access-tokens/

Categories

Resources