Fuzzy
Fuzzy

Reputation: 11

Create an instance of FacebookApp programically using AppID, Secret and Key

How can I instantiate the FacebookApp with custom AppID/Key/Secret without using the web.config?

I need to do this due to multilingual Facebook applications with several tab-pages within one Visual Studio solution. Since Facebook can only have one Tab-page for each application I'm forced to figure this out.

Upvotes: 1

Views: 1224

Answers (3)

prabir
prabir

Reputation: 7794

If you are using the new v5.x of the Facebook C# SDK we have made it a lot easier to set Facebook settings without using config files.

There is a new interface called IFacebookApplication. Create a custom class and implement the IFacebookApplication interface.

Then you can change the default application settings by

FacebookContext.SetApplication( iFacebookApplication );

To retrieve the current application setting you would:

var fbAppSettigns = FacebookContext.Current;

Upvotes: 0

DevTheo
DevTheo

Reputation: 921

Fuzzy,

Carlos is correct.

Are you doing MVC or Classic WebForms? This is exactly what you would do if you were doing WebForms (you would probably want to store this in some common base page, but beyond that his solution is absolutely correct.

With MVC it is just a matter of getting things set up properly. You can do this within a Base Controller. You might want to study how things get wired up in the source code of the Facebook C# SDK to get a better picture (I'm thinking I need to build a sample for this in the SDK's samples)

Upvotes: 0

Carlos Muñoz
Carlos Muñoz

Reputation: 17844

You can use the right constructor for the job:

public FacebookApp(IFacebookSettings settings)

The settings object can be constructed this way:

var settings = new FacebookSettings
{
    ApiKey = //[Your key]
    ApiSecret = //[Your secret]
    AppId = //[Your appid]
}

Complete code:

var fbSettings = new FacebookSettings
{
    ApiKey = //[Your key]
    ApiSecret = //[Your secret]
    AppId = //[Your appid]
}
var fb = new FacebookApp(fbSettings);

Upvotes: 1

Related Questions