How do I obtain a Facebook access token for an app to post on a company wall?

感情迁移 提交于 2019-12-12 03:14:44

问题


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#.


回答1:


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.


来源:https://stackoverflow.com/questions/10147375/how-do-i-obtain-a-facebook-access-token-for-an-app-to-post-on-a-company-wall

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!