问题
Facebook's OAuth implementation allows you to authenticate as an application (rather than as a user). Is it possible to achieve this with the Facebook C# SDK?
Rich
回答1:
Looks like the Facebook C# SDK doesn't help you with obtaining an access token as a user or an application. Based on this comment on the Facebook C# SDK:
This SDK does not include a method of getting a token from a user, as the best method will depend on what type of application is using it.
Your pretty much on your own.
回答2:
As per @jfar's answer, it doesn't look as though the SDK natively supports application authentication. My solution has been to source the access token myself and then apply it to SDK.
Below is my rather crude Console application for retrieving the access token.
static void Main(string[] args) {
var webRequest = (HttpWebRequest)HttpWebRequest.Create("https://graph.facebook.com/oauth/access_token");
webRequest.Method = "POST";
webRequest.ContentType = "application/x-www-form-urlencoded";
var requestString = "grant_type=client_credentials&client_id=***client_secret=***";
byte[] bytedata = Encoding.UTF8.GetBytes(requestString);
webRequest.ContentLength = bytedata.Length;
var requestStream = webRequest.GetRequestStream();
requestStream.Write(bytedata, 0, bytedata.Length);
requestStream.Close();
var response = webRequest.GetResponse();
var responseStream = new StreamReader(response.GetResponseStream());
var responseString = responseStream.ReadToEnd();
responseStream.Close();
response.Close();
if (!String.IsNullOrWhiteSpace(responseString)) {
var accessToken = responseString.Replace("access_token=", "");
var fbApp = new FacebookApp(accessToken);
long id = ****;
dynamic results = fbApp.Query("select name, email, website from user where uid=" + id.ToString());
dynamic user = results[0];
Console.Write(String.Concat(user.name, " ", user.email, " ", user.website));
}
else {
}
Console.ReadLine();
}
As you can see I have initiated OAuth authentication using HttpWebRequest
. The response returns the application access token which is then passed straight into one of the FacbeookApp
constructors, giving me application access to the Facebook C# SDK.
Rich
来源:https://stackoverflow.com/questions/4676201/is-it-possible-to-authenticate-as-an-application-instead-of-a-user-with-the-face