views:

41

answers:

1

I'm trying to link my website with Facebook.

If I call the method in the PHP SDK called getLoginUrl(), it returns the correct login URL. This URL contains a number of parameters, one of which is my API key and it is correct. This is the URL that my Connect button is supposed to take me to.

However, when I actually click the Connect button on my site, a popup opens and says "Invalid API key specified". With a bit of inspection, I see that the URL in this popup says "api_key=undefined". In fact, the URL is completely different to what the login URL should be.

Any idea why this is happening? Only conflicts I can imagine are codeigniter or jQuery.

It's hard to be more specific. I'm guessing this will require good knowledge of the Facebook SDK to answer.

Thanks a lot. :)

+1  A: 

PHP should handle only login callback if necessary, login should be done through javascript.

FB api initialization:

<div id="fb-root"></div>
<script src="http://connect.facebook.net/en_US/all.js"&gt;&lt;/script&gt;
<script>
    FB.init({
        appId  : appId,
        status : true,
        cookie : true,
        xfbml  : true
    });
</script>

Then you can trigger fb login dialog on some button:

FB.login(function(response) {
    //callback
}, {perms:'publish_stream'});

Also make sure you have correct settings in your fb app under "Connect" tab - "Connect URL" and "Base Domain" should be set. If they link to a wrong domain you won't be able to login.

serg
I'm running the website on localhost. What should I do about the Connect tab options? (not at my flat at the moment, so can't test things). Thanks. :)
sixfoottallrabbit
See this answer: http://stackoverflow.com/questions/3057089/dw-cs5-run-facebook-applications-on-local-server/3057559#3057559
serg
I have managed to set the Connect tab options properly, but it still won't work. I have used your API initalization code and a <fb:login-button> but when I click the login button I still get the same "Invalid API key specified" error and the URL says api_key=undefined. Where does the JS get the api_key from? I don't actually specify the API key anywhere. Just the App ID and the Secret Key.
sixfoottallrabbit
Assign your API key to "appId" inside FB.init(). It says appId but it actually needs API key there. I only now noticed when you pointed out that it actually called appId instead of apiKey lol. Facebook api is messed up.
serg
Wow, it's actually just the documentation that's wrong. It is really called apiKey, not appId. I just changed it to apiKey and put my API key as the value, and now it works. Thanks a lot! Got there eventually.
sixfoottallrabbit
Have you tried assigning api key to appId? It should work (that's what I do). If it does I would rather use appId var name, apiKey might be deprecated or something. Glad to help.
serg
Both appId and apiKey are [supported and mean the same thing](http://github.com/facebook/connect-js/blob/master/src/core/init.js#L132). We used to expect developers to pass the apiKey, but recently switched to appId for two reasons -- first, it's slightly shorter, and second, it's faster on our side as internally we key off the appId. Now we've made it so that anywhere an apiKey used to be expected, you can pass appId instead and it will also be acceptable.
daaku