【问题标题】:Is it possible to authenticate as an application instead of a user with the Facebook C# SDK?是否可以使用 Facebook C# SDK 作为应用程序而不是用户进行身份验证?
【发布时间】:2011-06-08 06:03:53
【问题描述】:

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

丰富

【问题讨论】:

    标签: c# facebook authentication oauth facebook-c#-sdk


    【解决方案1】:

    根据@jfar 的回答,SDK 似乎本身并不支持应用程序身份验证。我的解决方案是自己获取访问令牌,然后将其应用到 SDK。

    下面是我用于检索访问令牌的相当粗糙的控制台应用程序。

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

    如您所见,我已使用 HttpWebRequest 启动 OAuth 身份验证。响应返回应用程序访问令牌,然后将其直接传递到 FacbeookApp 构造函数之一,让我可以访问 Facebook C# SDK。

    丰富

    【讨论】:

      【解决方案2】:

      看起来 Facebook C# SDK 无法帮助您以用户或应用程序的身份获取访问令牌。基于对 Facebook C# SDK 的评论:

      此 SDK 不包含以下方法 从用户那里获取令牌,作为 最好的方法取决于什么类型 的应用程序正在使用它。

      您几乎可以靠自己。

      【讨论】:

      • +1 谢谢@jfar。我觉得我在我的第一个 Facebook 上做了很多假设,结果证明这是一个雷区。我目前的解决方案是自己获取访问令牌,然后将其传递给 SDK。
      猜你喜欢
      • 2011-07-31
      • 2019-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-20
      • 1970-01-01
      • 2010-10-20
      相关资源
      最近更新 更多