14

I'd like to be able to authenticate myself (my profile, not just my app) on my own web application using the Facebook C# SDK. Using the Graph API, I can get an access token, but that token does not seem to work properly with the Facebook C# as it seems to be stateless.

The error that is thrown is:

(OAuthException) An active access token must be used to query information about the current user.

I've poked around the Facebook C# SDK and documentation and most of the info I'm seeing is to redirect users to a login page which is not what I'm looking for.

Does anyone have a good sample of auto-logging in myself so I can pull up my own information?

TIA

Jason N. Gaylord
  • 7,910
  • 15
  • 56
  • 95
  • Could you provide some information on what you are trying to do? Are you trying to post to your wall, get your status, or what? That will help us out a lot. Depending on what you are doing you will need an app that has been given the necessary permissions which will result in an access token. What kind of app are we talking about here? Web App? Windows Forms? WPF? Silverlight? Phone? All of this will help us get you an answer.. – DevTheo Mar 03 '11 at 13:24
  • I'm using a web app and I'm trying to grab all of the latest photos for my account. I've tried the Graph API as well as the Facebook C# SDK. The SDK appears to be just a wrapper. – Jason N. Gaylord Mar 03 '11 at 14:12
  • I am having problems with grabbing the images as well. [link](http://stackoverflow.com/questions/5178238/obtaining-photos-and-albums-from-facebook-using-c) – Jason N. Gaylord Mar 03 '11 at 14:12

3 Answers3

9

When you say "yourself" do you mean the app or your actual facebook user?

If it's just your app, you can get an access token by POSTing to this URL:

https://graph.facebook.com/oauth/access_token?grant_type=client_credentials&client_id=APP_ID_HERE&client_secret=APP_SECRET_HERE

You can use this access token to perform actions on behalf of your users if they have authorized your app to do so.

Max
  • 6,901
  • 7
  • 46
  • 61
  • Just updated my question. I know I can call out to the Graph API, but each time I do that, it says that I don't have the proper credentials. I'll try specifying the grant_type as I have not up to this point. I am looking for my profile and not just my app. – Jason N. Gaylord Mar 02 '11 at 21:16
  • 1
    Actually, I did try adding in that grant_type. – Jason N. Gaylord Mar 02 '11 at 21:18
  • I don't know what that error is being caused by. The above POST should work for your app (at least to get the access token). Make sure you are using contenttype = "application/x-www-form-urlencoded" I suggest doing the API stuff by hand. I've not found a good C# facebook library that has kept up with the API changes. – Max Mar 02 '11 at 21:29
  • I've updated the question again with the specific OAuth error. I get an auth token back, but it doesn't seem to work with subsequent requests. – Jason N. Gaylord Mar 02 '11 at 21:35
  • So I got it working to connect using https://graph.facebook.com/oauth/access_token?client_id={app_id}&client_secret={app_secret}&grant_type=client_credentials&scope=user_photos,offline_access"; – Jason N. Gaylord Mar 03 '11 at 00:31
  • @Jason N. Gaylord Did you really get access to your profile using the url in the answer or even the url above? – jimmystormig Mar 03 '11 at 20:49
  • @Jason N. Gaylord You should only be able to get access to app-specific api:s, such as Insight etc, using grant_type=client_credentials. To get to profile/user-specific api:s/data you need to perform a user login using the oAuth Dialog. This is my understanding from reading the Facebook documentation - http://developers.facebook.com/docs/authentication/ – jimmystormig Mar 04 '11 at 09:49
  • So there is no way to get my own information without prompting with the oAuth Dialog? – Jason N. Gaylord Mar 08 '11 at 03:48
2

I had the same problem where the access token didn't include the session part. Please check out my answer to this similar question - exchange code for token facebook-c#-sdk.

Here's a sample from my Home controller

[CanvasAuthorize]
public ActionResult Index()
{
    var app = new FacebookClient(new Authorizer().Session.AccessToken);

    dynamic me = app.Get("me");
    ViewBag.Firstname = me.first_name;
    ViewBag.Lastname = me.last_name;

    return View();
}

Edit

Now I understand the question better. What about this?

var auth = new Authorizer(FacebookContext.Current.AppId, FacebookContext.Current.AppSecret, HttpContext);

auth.Authorize();

From the documentation:

public bool Authorize() Member of Facebook.Web.Authorizer

Summary: Authorizes the user if the user is not logged in or the application does not have all the sepcified permissions.

Returns: Return true if the user is authenticated and the application has all the specified permissions.

Community
  • 1
  • 1
jimmystormig
  • 10,672
  • 1
  • 29
  • 28
  • Please add comment when down-voting. – jimmystormig Mar 02 '11 at 21:18
  • Doesn't that still require that I login? I want to login from code. – Jason N. Gaylord Mar 02 '11 at 21:19
  • So if I try that, I get an exception thrown: – Jason N. Gaylord Mar 02 '11 at 21:42
  • Locating source for 'd:\Projects\facebooksdk\Source\Facebook.Web\Authorizer.cs'. (No checksum.) The file 'd:\Projects\facebooksdk\Source\Facebook.Web\Authorizer.cs' does not exist. Looking in script documents for 'd:\Projects\facebooksdk\Source\Facebook.Web\Authorizer.cs'... Looking in the projects for 'd:\Projects\facebooksdk\Source\Facebook.Web\Authorizer.cs'. The file was not found in a project. Looking in directory 'c:\Program Files\Microsoft Visual Studio 10.0\VC\crt\src\'... Looking in directory 'c:\Program Files\Microsoft Visual Studio 10.0\VC\atlmfc\src\mfc\'... – Jason N. Gaylord Mar 02 '11 at 21:43
  • It looks like the SDK is not properly installed/built. I added mine through NuGet with the command "install-package FacebookWebMvc" – jimmystormig Mar 02 '11 at 21:59
  • I'm going to uninstall and reinstall it. I'll let you know what I can find. – Jason N. Gaylord Mar 02 '11 at 22:32
  • So I've reinstalled and now it's telling me that the parameter url cannot be null and I'm not quite sure why. This whole thing seems like a trival task thats way too difficult. – Jason N. Gaylord Mar 02 '11 at 22:47
  • Yes, I can reproduce your problem now. The code is working once you host your application inside the Facebook iFrame/canvas. Once run on it's own it needs a CancelUrl which and when set tells you that you're not authorized. It should be possible to authenticate outside though since it's also built for WPF etc. – jimmystormig Mar 03 '11 at 08:37
  • Not really. I'm sorry! I'll let you know if I found something. – jimmystormig Mar 03 '11 at 14:51
  • So I was able to connect using the URL. However, I cannot pull any albums as I continually receive a 400. Here's the issue on StackOverflow. - http://stackoverflow.com/questions/5178238/obtaining-photos-and-albums-from-facebook-using-c – Jason N. Gaylord Mar 03 '11 at 17:54
  • And here's my answer to the new question - http://stackoverflow.com/questions/5178238/obtaining-photos-and-albums-from-facebook-using-c/5209278#5209278 – jimmystormig Mar 06 '11 at 07:31
1

I dont know if this will work for you:

using Facebook.Web;
using Facebook;
using System.Dynamic;

var auth = new CanvasAuthorizer { Permissions = new[] { "user_about_me"} };

if (auth.Authorize())
{
}

and include this in the aspx:

<input type="hidden" name="signed_request" value="<%: Request.Params["signed_request"]%>"/>

good luck !!