I've managed to get the login with facebook working using FacebookConnect. I then try to pull the user's name and email address. I manage to get their name however their email address is blank. I tried it using my Facebook account where I have set my email address to be public. I also added the permissions ios.facebook_permissons = "email", "public_profile", "user_birthday" and the same for android. Here is my code
The login
public void facebookLogin() {
final Login fb = FacebookConnect.getInstance();
fb.setClientId("964865683621261");
fb.setClientSecret("1fb729a93d96bb8700f2c879f520052b");
fb.setRedirectURI("http://kyven.co.za");
FaceBookAccess.setPermissions(new String[]{"user_birthday", "email", "public_profile"});
fb.setCallback(new LoginCallback(){
@Override
public void loginFailed(String errorMessage) {
Dialog dg = new Dialog();
dg.setTitle("Login failed");
dg.show();
}
@Override
public void loginSuccessful() {
InfiniteProgress ip = new InfiniteProgress();
ip.setUIID("InfiniteProgress");
final Dialog d = ip.showInifiniteBlocking();
Preferences.set("token", fb.getAccessToken().getToken());
final FacebookData data = new FacebookData();
data.fetchData(Preferences.get("token", (String) null), new Runnable(){
public void run() {
String email = data.getEmail();
Dialog.show("hello " + data.getName(), data.getEmail(), "OK", null);
String fullName = data.getName();
String[] args = { email, fullName};
String[] keys = { "email", "password"};
int id = postRequest(args, keys, "facebook_login.php");
if (id != 0) {
Preferences.set("userId", id);
setUpMainPage();
Hashtable meta = new Hashtable();
meta.put(com.codename1.push.Push.GOOGLE_PUSH_KEY,
1276);
Display.getInstance().registerPush(meta, true);
} else {
Dialog.show("Error", "There was an error logging you in"
+ " please try again later", "OK", null);
d.dispose();
}
}
});
}
});
fb.doLogin();
}
Fetching the data
public class FacebookData implements UserData {
String name, email;
public String getName() {
return name;
}
public String getEmail() {
return email;
}
public void fetchData(String token, final Runnable callback) {
ConnectionRequest req = new ConnectionRequest() {
@Override
protected void readResponse(InputStream input) throws IOException {
JSONParser parser = new JSONParser();
Map<String, Object> parsed = parser.parseJSON(new InputStreamReader(input, "UTF-8"));
name = (String) parsed.get("name");
email = (String) parsed.get("email");
}
@Override
protected void postResponse() {
callback.run();
}
@Override
protected void handleErrorResponseCode(int code, String message) {
}
};
req.setPost(false);
req.setUrl("https://graph.facebook.com/v2.4/me");
req.addArgumentNoEncoding("access_token", token);
NetworkManager.getInstance().addToQueue(req);
}
}