问题
I am developing a WPF application that needs post on wall of a facebook's Page, and this without login window. Well, I want to get access token for my facebook page, and this is my code.
var fb = new FacebookClient();
string token = "";
dynamic accounts = fb.Get("/"<USER_ID>"/accounts");
foreach (dynamic account in accounts)
{
if (account.id == <PAGE_ID>)
{
token = account.access_token;
break;
}
}
But I receive a error #104. It is a simple error, that I need a access token to do this operation. Then I use other code to get the user access token
var fb = new FacebookClient();
dynamic result = fb.Get("oauth/access_token", new
{
client_id = <PAGE_ID>,
client_secret = <APP_SECRET>,
grant_type = "fb_exchange_token",
fb_exchange_token = <USER_TOKEN>
});
But I get error #101:
"Error validating application. Cannot get application info due to a system error."
Someone knows what I have to do?
Thanks!!!
回答1:
I'm not sure if you've been able to get a never expiring token for the page, so I'll explain you the steps:
Open Graph API Explorer
Select your app from the drop-down
Click "Get Access Token" button, and select the
manage_pages
permission.Copy the token and run this in the browser:
https://graph.facebook.com/oauth/access_token?grant_type=fb_exchange_token&client_id={app-id}&client_secret={app-secret}&fb_exchange_token={step-3-token}
Copy the token from step-4 and paste in to the access_token field and call:
/{page-id}?fields=access_token
The token you get now is a never-expiring token, you can validate the same in Debugger .Use this in your app.
But beware, its not recommended to use this token on client side if your app is public.
回答2:
If you use the fb_exchange_token call, it will give you a token that expires after 60 days. In order to make it work correctly, I had to go through the login web flow to guarantee I got an up-to-date page access token.
- Go to the Facebook App dashboard
- If you haven't already added the Facebook Login product, click "+ Add Product" and select Facebook Login
- Enable the "embedded browser control" option and enter
https://www.facebook.com/connect/login_success.html
as the allowed redirect URL. - Make a Window with a WebView control on it. The WebBrowser control no longer works; the browser engine powering it is too old.
Add code to listen for the navigation to the success URL:
this.webView.NavigationCompleted += (sender, args) => { if (args.Uri.AbsolutePath == "/connect/login_success.html") { if (args.Uri.Query.Contains("error")) { MessageBox.Show("Error logging in."); } else { string fragment = args.Uri.Fragment; var collection = HttpUtility.ParseQueryString(fragment.Substring(1)); string token = collection["access_token"]; // Save the token somewhere to give back to your code } this.Close(); } };
Add code to navigate to the facebook login URL:
string returnUrl = WebUtility.UrlEncode("https://www.facebook.com/connect/login_success.html"); this.webView.Source = new Uri($"https://www.facebook.com/dialog/oauth?client_id={appId}&redirect_uri={returnUrl}&response_type=token%2Cgranted_scopes&scope=manage_pages&display=popup");
Call window.ShowDialog() to pop up the login window, then grab the user access token.
Create some models to help you out:
public class AccountsResult { public List<Account> data { get; set; } } public class Account { public string access_token { get; set; } public string id { get; set; } }
Using the user access token, get the page access token:
FacebookClient userFacebookClient = new FacebookClient(userAccessToken); var accountsResult = await userFacebookClient.GetTaskAsync<AccountsResult>("/me/accounts"); string pageAccessToken = accountsResult.data.FirstOrDefault(account => account.id == PageId)?.access_token; if (pageAccessToken == null) { MessageBox.Show("Could not find page under user accounts."); } else { FacebookClient pageFacebookClient = new FacebookClient(pageAccessToken); // Use pageFacebookClient here }
来源:https://stackoverflow.com/questions/22976350/get-access-token-to-facebook-page-wpf