0

So here is what I am trying to do. I have created my Facebook application and I have been able to use the Graph API explorer to specify the application in the top right.

Some more detailed information, I am using C# to code in the functionality to our in-house app to get this to work.

After that I go to the url section and type in the following address.

https://graph.facebook.com/MyCompanyPage

Then hit submit. It will navigate and display the information for that page below in the explorer. Well then to get my access_token for my app I go up and hit Get Access Token->Specify my Extended Permissions->Then hit Get Access Token again. All that is well and good and I can post to Facebook using our Facebook app successfully.

The problem is that the access_token is expiring and I am using a manual method of retrieval to get the access token.

Here is what I have tried doing using one of the examples from GIT.

FacebookClient client = new FacebookClient();
dynamic parameters = new ExpandoObject();
parameters.client_id = "MYAPPID"; // Or does this need to be my CompanyPage ID??
parameters.redirect_uri = "https://www.facebook.com/connect/login_success.html";
// The requested response: an access token (token), an authorization code (code), or both (code token).
parameters.response_type = "token";
// list of additional display modes can be found at http://developers.facebook.com/docs/reference/dialogs/#display
parameters.display = "popup";
// add the 'scope' parameter only if we have extendedPermissions.
parameters.scope = "publish_stream,manage_pages,status_update";
// when the Form is loaded navigate to the login url.
Uri LoginURL = client.GetLoginUrl(parameters);

// whenever the browser navigates to a new url, try parsing the url.
// the url may be the result of OAuth 2.0 authentication.
FacebookOAuthResult oauthResult;
if (client.TryParseOAuthCallbackUrl(LoginURL, out oauthResult)) {
     // The url is the result of OAuth 2.0 authentication
} else {
     // The url is NOT the result of OAuth 2.0 authentication.
}

Now this works for me when I am trying to obtain an access token for the app for myself. However, I need the behavior to operate in the same way that it does for the Graph API Explorer. The reason I need it to work like the Explorer is because I wanted to make posts using the Facebook application under the actual Company Page, so it appears that the Company Page is actually making the Facebook post and not a user. Like I have said above I am able to successfully do this through the Graph API Explorer but haven't been successful doing this in C#.

Steven Combs
  • 1,890
  • 6
  • 29
  • 54

1 Answers1

2

in order to use Graph API on behalf of a Page, you need to get Page access token - see here for more details: https://developers.facebook.com/docs/authentication/pages/

  1. Authenticate the user and request the manage_pages permission
  2. Get the list of pages the user manages
  3. Parse the list and get the token - and use it to post to the feed.
avs099
  • 10,937
  • 6
  • 60
  • 110
  • Ok so after reading your post. I just realized what I was trying to accomplish in my mind is essentially impossible. I need to be able to automate the whole process. However, I have to authenticate the user first and get that access token. Which I can't do unless I redirect them to the login page. So the user is going to have to interact to at least login to Facebook and then the posts can be made on behalf of the user. Is it possible to lengthen the amount of time for an access_token? These are only lasting a few hours. – Steven Combs Apr 13 '12 at 21:09
  • 1
    re: extending the token, see question and my answer here: http://stackoverflow.com/questions/10082531/what-is-the-code-for-getting-a-refreshed-facebook-token-in-an-android-app/10148412#10148412 you will get new token which will be valid for 60 days – avs099 Apr 13 '12 at 21:16
  • You are awesome! I had to extend the app access_token first and then I was able to get a special what I assume will be lengthened access token for posting to the page. Learning slowly but surely. – Steven Combs Apr 13 '12 at 21:32