Facebook Posting Photo using App Access Token - c#

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}

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

dropnet api c# unauthorized user exception

I am using dropnet api to access dropbox using c#.i send my apikey and secret key,i get token in response.but when i try to get account info or delete a file it throws unauthorized use exceptions.can any one help me.my code is as follow.
DropNet.DropNetClient client = new DropNet.DropNetClient("xxxxxxxxx", "xxxxxxxxx");
DropNet.Models.UserLogin login = client.GetToken();
client.UserLogin = login;
string st=client.BuildAuthorizeUrl(client.UserLogin,null);
client.Delete("/Public/testsk");
var accountInfo = client.AccountInfo();
Regards,
Shah Khalid.
Dropbox API uses standard http error codes,
in your case, 401 usually means that user access token expired or revoked and it should recreated,
All you need to do is re-authenticate end user dropbox account by doing next steps in this order:
your application basic authentication:
client = new DropNetClient(Settings.Default.DropboxApiKey, Settings.Default.DropboxApiSecret);
client.GetToken();
Let user login Dropbox with their account and approve your Dropbox app:
string url = Client.BuildAuthorizeUrl();
Retrieve and save authenticated user token:
UserLogin accessToken = Client.GetAccessToken();
Account.Token = accessToken.Token;
Account.Secret = accessToken.Secret;
Account.Status = AccountStatus.Active;
last - save account data in your app - Account.Save()

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.

Show photos from my facebook page on my web site

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.

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