How to integrate Facebook with Phonegap/Cordova on Android

published Oct 14, 2013 02:15   by admin ( last modified Oct 17, 2013 01:13 )

It can be a boon to post on Facebook from your Android phonegap/cordova app. You can actually use the https scheme to post through the user's browser, but then he needs to login there. It is also possible to communicate with the Facebook app via the fb:// protocol, but you cannot post stories or any other kind of updates that way (anymore).

There is however a plugin for Phonegap to use Facebook, and it seems to work, and you can post from the app to Facebook. The instructions for it worked out A ok for me, just follow it step by step, but with the following caveats:

Don't use Phonegap 2.8.0 or 2.7 (2.8.1 works though)

In Phonegap 2.7 they removed some deprecated interfaces, that the Facebook plugin for Phonegap uses. They then put those deprecated interfaces back in 2.8.1. So if you - like me - were on 2.8.0 or if you are on 2.7, you are on the versions that do not work. (Disclaimer, I have not tested the 3.x series or anything pre 2.8). it is possible to use the new interfaces as outlined in the blog post linked above, but when it came down to lines in the plugin as:

this.fba.success(new PluginResult(PluginResult.Status.OK), this.fba.callbackId);

...it started to become a bit hairy, and not clear to me what that actually does, or how to replace it. With 2.8.1 you don't need to worry about that.

In the simple example, the name of the javascript sdk library is slightly wrong

It is "facebook-js-sdk.js", not "facebook_js_sdk.js" as it is written in the index.html file currently.

On Linux the key you need to make a hash out of, is in "~/.android/"

In the Facebook documentation, they only give examples for OSX and Windows. On Linux you need to write:

keytool -exportcert -alias androiddebugkey -keystore ~/.android/debug.keystore | openssl sha1 -binary | openssl base64

After these changes, I was able to post on my wall from an Android emulator. I have not tried with an exported apk yet, for that you need to make a hash as above but from the keystore you use to sign your apks with.