Facebook C#SDK OAuth异常“需要ClientID” [英] Facebook C# SDK OAuth Exception "ClientID required"

查看:332
本文介绍了Facebook C#SDK OAuth异常“需要ClientID”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为这个问题类似于我以前的一个
在.NET 4上使用最新的C#Facebook SDK,我得到一个异常,并显示最后一行带有以下代码的消息ClientID required:

This question is, I think, similar to my previous one. Using the latest C# Facebook SDK on .NET 4 I get an Exception with the message "ClientID required" with the following code on the last line:

    var app = new DefaultFacebookApplication();
    app.AppId = "appId";
    app.AppSecret = "secret";
    var fb = new FacebookWebContext(app);
    fb.IsAuthenticated();

应用程序ID和密码正确设置。异常的堆栈跟踪如下:
发生System.Exception

App ID and secret are properly set. The stack trace of the exception is the following: System.Exception occurred

  Message=ClientID required.   Source=Facebook   StackTrace:
       at Facebook.FacebookOAuthClient.BuildExchangeCodeForAccessTokenParameters(IDictionary`2 parameters, String& name, String& path)
       at Facebook.FacebookOAuthClient.ExchangeCodeForAccessToken(String code, IDictionary`2 parameters)
       at Facebook.FacebookSession.get_AccessToken()
       at Facebook.FacebookSession.get_Expires()
       at Facebook.Web.FacebookWebContext.IsAuthenticated()
       at Piedone.FacebookTest.Authorize()   InnerException:

在客户端我正在使用JS SDK,初始化如下:

On the client side I'm using the JS SDK, initialized as following:

            FB.init({
                appId: appId,
                status: true, // check login status
                cookie: true, // enable cookies to allow the server to access the session
                xfbml: true, // parse XFBML
                oauth: true // enable OAuth 2.0
            });

用户使用JS login()方法正确登录,作为以下内容中的警报的代码运行:

The users gets properly logged in with the JS login() method, as the alert in the following piece of code runs:

        FB.login(function (response) {
            if (response.authResponse) {
                alert("logged in");
            } else {
                alert('User cancelled login or did not fully authorize.');
            }
        }, { scope: scope });

在Facebook的应用设置中,强制使用OAuth呼叫和验证登录密码。登录和加密访问令牌打开。据我所知,这一切都应该能够使用OAuth 2身份验证。
任何人有一个想法我做错了什么?在这几行代码中确实没有任何错误...

In the app settings on Facebook both the "Forces use of login secret for OAuth call and for auth.login" and "Encrypted Access Token" are turned on. As far as I know all this should enable the use of the OAuth 2 authentication. Anybody has an idea what am I doing wrong? There really can't be any error in these few lines of code...

提前感谢任何帮助!

编辑:
FacebookWebContext的AccessToken属性引发相同的错误,HttpContext.CurrentNotification:

The AccessToken property of FacebookWebContext throws the same error and HttpContext.CurrentNotification does:

CurrentNotification '(_facebookWebContextCache.HttpContext).CurrentNotification' threw an exception of type 'System.PlatformNotSupportedException'  System.Web.RequestNotification {System.PlatformNotSupportedException}
This operation requires IIS integrated pipeline mode.

由于我必须使用其开发服务器从Visual Studio运行该程序(正如我正在开发的应用程序)对于后一个例外,我想没有办法做任何事情。其实我也尝试过Webmatrix的IIS express,但问题依然存在。
这也很有趣,在FacebookWebContext中,设置(应用程序ID,秘密)也正确设置,用户ID和签名的请求也在那里...

Since I must run the program from Visual Studio with its Development Server (as I'm currently developing the application) there is no way anything can be done about the latter exception, I suppose. Actually I also tried with Webmatrix's IIS express, but the problem persists. It's also interesting, that in the FacebookWebContext the settings (app id, secret) are correctly set as well, the user Id and the signed request is also there...

编辑2:
使用SDK源时,我也会收到相同的错误。它看起来AccessToken和Session中Expires属性抛出异常。我不知道这是否连接到上面的httpcontext问题。

Edit 2: I also get the same error when using the SDK source. It looks that AccessToken and in the Session the Expires property throw the exception. I don't know if this is connected to the httpcontext issue above.

推荐答案

最后我设法解决问题,但大多数可能这是SDK中的一个错误。

Finally I managed to solve the problem, but most likely this is a bug in the SDK.

原因
问题是FacebookApplication.Current是空的,因为它没有填充在FacebookWebContext ctor中设置的数据。这导致访问令牌的问题:在第119行的FacebookSession.AccessToken中,FacebookOAuthClient被实例化为FacebookApplication.Current,当然实际上是空的。所以FacebookOAuthClient抛出异常,因为它没有获得应用程序设置。

The cause The problem is that the FacebookApplication.Current is empty, as it does not get populated with data set in the FacebookWebContext ctor. This leads to the problem of the access token: in FacebookSession.AccessToken on line 119 FacebookOAuthClient is instantiated with FacebookApplication.Current, that of course is practically empty. So FacebookOAuthClient is throwing the exception as it doesn't get the application settings.

解决方案
解决方法是简单明确地设置当前的FacebookApplication与FacebookWebContext的实例化:

The solution The workaround is to simply explicitly set the current FacebookApplication together with the instantiation of FacebookWebContext:

var app = new DefaultFacebookApplication();
app.AppId = "appId";
app.AppSecret = "secret";
var fb = new FacebookWebContext(app);
FacebookApplication.SetApplication(app); // Note this is the new line

这篇关于Facebook C#SDK OAuth异常“需要ClientID”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆