Facebook C# SDK OAuth Exception “ClientID required”

末鹿安然 提交于 2019-12-12 04:44:30

问题


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

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:

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

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

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...

Thanks in advance for any help!

Edit: 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.

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...

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.


回答1:


One more solution is add facebook settings to you web or app congfig

<facebookSettings appId="appid" appSecret="secret" />

after that create Auth class

var oauth = new FacebookOAuthClient(FacebookApplication.Current);

And it wil work as well




回答2:


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

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.

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


来源:https://stackoverflow.com/questions/7381370/facebook-c-sharp-sdk-oauth-exception-clientid-required

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