而不是与Facebook的C#SDK用户进行身份验证

而不是与Facebook的C#SDK用户进行身份验证

本文介绍了是否有可能作为一个应用程序,而不是与Facebook的C#SDK用户进行身份验证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Facebook的OAuth的实现允许你作为一个应用程序(而不是作为一个用户)进行身份验证。是否有可能与Facebook的C#SDK实现这一目标?

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?

推荐答案

根据@ jfar的答案,它看起来并不仿佛SDK本身支持应用程序验证。我的解决方案一直到源代码访问令牌自己,然后将其应用于SDK。

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();
    }

正如你可以看到我已经开始使用OAuth认证的HttpWebRequest 。响应返回应用程序访问令牌,然后直接传递到 FacbeookApp 构造方法,让我到Facebook的C#SDK应用程序的访问。

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.

这篇关于是否有可能作为一个应用程序,而不是与Facebook的C#SDK用户进行身份验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 00:39