Is it possible to authenticate as an application instead of a user with the Facebook C# SDK?

别等时光非礼了梦想. 提交于 2019-12-08 05:33:36

问题


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

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