This plugin has no dependency: You don't need (and shouldn't use) the Facebook SDK with this library.
OpenFB allows you login to Facebook with the Cordova InAppBrowser and execute any Facebook Graph API request.
- Android
- iOS
This requires Phonegap / Cordova CLI 3.0 +
- Cordova CLI
cordova plugin add https://github.com/whebcraft/openfb
- Phonegap Build
<plugin spec="https://github.com/whebcraft/openfb.git" source="git" />
- Create html file
oauthcallback.html
in your appwww
folder.
<html>
<body>
<script>
window.opener.openfb.oauthCallback(window.location.href);
window.close();
</script>
</body>
</html>
- Create a Facebook app here, in the app sidebar under >
PRODUCTS
click onFACEBOOK LOGIN
.
- on FACEBOOK LOGIN page scroll down enter
https://www.facebook.com/connect/login_success.html
andhttp://localhost:8888/cordova-open-fb/www/oauthcallback.html
as Valid OAuth redirect URIs - [Required]
To Test Facebook login sample on your own system for example:
if during development you access your application from http://localhost/YOUR-APP/www/index.html
(If testing from localhost) you must also declare http://localhost/YOUR-APP/www/index.html
as a valid redirect URI.
// Int
var fbAppId, loginCallback, runningInCordova = true; // add this to your deviceready function..
openfb.init({appId: 'YOUR_FACEBOOK_APP_ID'});
// Facebook Login
openfb.login(
function(res) {
if (res.status === 'connected') {
// Login Successfull...
getUsersData();
} else {
// Login cancelled by the user.
alert('Login With Facebook Cancelled');
}
}, {
scope: 'email' // for more permissions see https://developers.facebook.com/docs/facebook-login/permissions
});
// Get user data after user is logged in
function getUsersData(){
openfb.api({
path: '/me',
success: function(f) {
// f is an object
{
id: "123456789", // Facebook User Id
birthday: "06/10/1394",
email: "useremail@gmail.com", // Facebook User Email
first_name: "Mark", // Firstname
gender: "male", // Gender
last_name: "openfb", // Last name
link: "https://www.facebook.com/app_scoped_user_id/123456789/",
locale: "en_US",
name: "Mark openfb",
timezone: 1,
updated_time: "2014-04-15T60:48:21+0000",
verified: true, // If user is verified
website: "openfb.com"
}
console.log(f.name) // Mark openfb
},
error: function(e) {
console.log(e);
}
});
}
Facebook access token is saved to localstorage.
localStorage.getItem('fbAccessToken'); // returns user token
var id = f.id;
document.getElementById("myphoto").innerHTML = '<img src="https://avatars.io/facebook/'+id+'"/>';
<div id="myphoto"></div>
See https://avatars.io for image sizes.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.