1

I'm trying to write a windows service that will post to my Facebook Page with results when it runs.

I just downloaded Facebook C# SDK v6.0.10.0 and writing the windows application in .Net 4.0

I created a facebook application account and got the AppID and Secret code needed.

The end goal would be to have this windows service post on my facebook page wall as the page and not the application user.

I keep getting an error when I go to get the accounts for my facebook application.

string strAppID = "my app api id";
string strSecret = "my app secret code";
Facebook.FacebookClient fbClient = new Facebook.FacebookClient();
fbClient.AppId = strAppID;
fbClient.AppSecret = strSecret;
dynamic ac = fbClient.Get("oauth/access_token", new
{
    client_id = strAppID,
    client_secret = strSecret,
    grant_type = "client_credentials"
});

string strAccessToken = String.Empty;
strAccessToken = ac.access_token;
if (!String.IsNullOrEmpty(strAccessToken))
{

    fbClient = new Facebook.FacebookClient(strAccessToken);
    fbClient.AccessToken = strAccessToken;
    fbClient.AppId = strAppID;
    fbClient.AppSecret = strSecret;

    //Here is where it is bombing
    dynamic fbAccounts = fbClient.Get("/me/accounts");

    fbClient = new Facebook.FacebookClient(strAccessToken);
    fbClient.AccessToken = strAccessToken;
    fbClient.AppId = strAppID;
    fbClient.AppSecret = strSecret;

    dynamic me = fbClient.Get("**Name of the facebook page I am trying to post to**");

    string strPageID = String.Empty;
    strPageID = me.id;

    string strPageAccessToken = String.Empty;

    //Loop over the accounts looking for the ID that matches your destination ID (Fan Page ID)
    foreach (dynamic account in fbAccounts.data)
    {
        if (account.id == strPageID)
        {
            //When you find it, grab the associated access token and put it in the Dictionary to pass in the FB Post, then break out.
            strPageAccessToken = account.access_token;
            break;
        }
    }


    try
    {
        fbClient.AccessToken = strPageAccessToken;
        var args = new Dictionary<string, object>();
        args["message"] = "Testing 123";
        fbClient.Post("/" + strPageID + "/feed", args);

    }
    catch (Facebook.FacebookOAuthException ex)
    {
        // oauth exception occurred
    }
    catch (Facebook.FacebookApiLimitException ex)
    {
        // api limit exception occurred.
    }
    catch (Facebook.FacebookApiException ex)
    {
        // other general facebook api exception
    }
    catch (Exception ex)
    {
        // non-facebook exception such as no internet connection.
    }
}

The error I am getting is on the line:

dynamic fbAccounts = fbClient.Get("/me/accounts");

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

Shaun D
  • 33
  • 2
  • 7

2 Answers2

1

see here: (OAuthException - #2500) An active access token must be used to query information about the current user

you are getting access token for the APPLICATION, not for a user. Therefore, "me" does not make sense. You should supply ID there - either your user ID, or your app ID, or any other ID your app has permissions for.

Community
  • 1
  • 1
avs099
  • 10,937
  • 6
  • 60
  • 110
  • so would I then use the ID for the facebook page and get the accounts for that? Is that how I would get the access token that would allow me to post to the facebook page as the page? – Shaun D Apr 20 '12 at 14:27
  • how to post as the page - see here http://stackoverflow.com/questions/10147375/how-do-i-obtain-a-facebook-access-token-for-an-app-to-post-on-a-company-wall/10148257#10148257 – avs099 Apr 20 '12 at 15:24
  • how to get USER token (you need to display login popup to the user): http://stackoverflow.com/questions/10108361/accessing-facebook-for-posts-etc-with-graph-api-c-sharp-unity3d/10208715#10208715 another option can be to get user token from Graph API Explorer and hardcode it - that will work if you only want to test/play with the app. – avs099 Apr 20 '12 at 15:25
  • This will be for a windows service that will post results to my facebook page so a user login really wont work. All of the examples I found use an older version which don't seem to work in the latest version of the SDK – Shaun D Apr 20 '12 at 17:42
  • I was trying to follow this example: http://stackoverflow.com/questions/5231827/post-on-facebook-page-as-page-not-as-admin-user-using-facebook-c-sharp-sdk/5752447#5752447 – Shaun D Apr 20 '12 at 17:45
  • if I try to post to the page using the Auth token for my application I get the error "(OAuthException - #200) (#200) The user hasn't authorized the application to perform this action" even though I followed the instructions here http://stackoverflow.com/questions/6216289/facebook-c-sharp-sdk-the-user-hasnt-authorized-the-application-to-perform-thi – Shaun D Apr 20 '12 at 17:55
  • Ok, I followed the instructions listed here https://developers.facebook.com/docs/authentication/server-side/ and hard coded the access token and it did post to the facebook page however it posted it as my facebook user and not the page. Any help? – Shaun D Apr 20 '12 at 19:49
  • after you got access token for the USER, you need to get access token for your PAGE, as described here: http://stackoverflow.com/questions/10147375/how-do-i-obtain-a-facebook-access-token-for-an-app-to-post-on-a-company-wall/10148257#10148257 Then parse the returned array of the pages, get the one you need, get its access token - and use it to post instead of user's token you are using right now. – avs099 Apr 20 '12 at 21:37
  • Ok that makes sense however I'm having trouble getting the User's access token. I'm following the directions from the link you posted however when I do a TryParseOAuthCallbackUrl it always returns false. However if I copy and paste the LoginURL into the browser it returns the access token. Is this a bug with the new version of the SDK? – Shaun D Apr 23 '12 at 14:13
0
dynamic ac = fbClient.Get("oauth/access_token", new
{
    client_id = strAppID,
    client_secret = strSecret,
    grant_type = "client_credentials"
});

The above code may not work for version 6.0.

OAuth 2.0 - exchange code for access token

FacebookClient supports parsing only json responses. Due to this reason “oauth/access_token” token will not work when using FacebookClient.Get(“oauth/access_token”). Instead you will need to use a method in FacebookOAuthClient.

You can find more details here: http://blog.prabir.me/post/Facebook-CSharp-SDK-Making-Requests.aspx

Hope this helps.

cubski
  • 3,218
  • 1
  • 31
  • 33