對於Facebook登錄工作你有幾件事情要做。
您需要定義您想要獲取的數據類型。最好的方法是創建一個的UserData接口,並在你的類實現它:
public interface UserData {
public String getId();
public String getEmail();
public String getFirstName();
public String getLastName();
public String getImage();
public void fetchData(String token, Runnable callback);
}
然後實現它是這樣的:
class FacebookData implements UserData {
String id;
String email;
String first_name;
String last_name;
String image;
@Override
public String getId() {
return id;
}
@Override
public String getEmail() {
return email;
}
@Override
public String getFirstName() {
return first_name;
}
@Override
public String getLastName() {
return last_name;
}
@Override
public String getImage() {
return image;
}
@Override
public void fetchData(String token, Runnable callback) {
ConnectionRequest req = new ConnectionRequest() {
@Override
protected void readResponse(InputStream input) throws IOException {
try {
JSONParser parser = new JSONParser();
Map<String, Object> parsed = parser.parseJSON(new InputStreamReader(input, "UTF-8"));
id = (String) parsed.get("id");
email = (String) parsed.get("email");
first_name = (String) parsed.get("first_name");
last_name = (String) parsed.get("last_name");
image = (String) ((Map) ((Map) parsed.get("picture")).get("data")).get("url").toString();
} catch (Exception ex) {
}
}
@Override
protected void postResponse() {
callback.run();
}
@Override
protected void handleErrorResponseCode(int code, String message) {
if (code >= 400 && code <= 410) {
doLogin(FacebookConnect.getInstance(), FacebookData.this, true);
return;
}
super.handleErrorResponseCode(code, message);
}
};
req.setPost(false);
req.setUrl("https://graph.facebook.com/v2.10/me");
req.addArgumentNoEncoding("access_token", token);
req.addArgumentNoEncoding("fields", "id,email,first_name,last_name,picture.width(512).height(512)");
NetworkManager.getInstance().addToQueue(req);
}
}
讓我們創建一個doLogin()
方法,其中包括setCallback()
void doLogin(Login lg, UserData data, boolean forceLogin) {
if (!forceLogin) {
if (lg.isUserLoggedIn()) {
//process Facebook login with "data" here
return;
}
String token = Preferences.get("token", (String) null);
if (getToolbar() != null && token != null) {
long tokenExpires = Preferences.get("tokenExpires", (long) -1);
if (tokenExpires < 0 || tokenExpires > System.currentTimeMillis()) {
data.fetchData(token,() -> {
//process Facebook login with "data" here
});
return;
}
}
}
lg.setCallback(new LoginCallback() {
@Override
public void loginFailed(String errorMessage) {
Dialog.show("Error Logging In", "There was an error logging in with Facebook: " + errorMessage, "Ok", null);
}
@Override
public void loginSuccessful() {
data.fetchData(lg.getAccessToken().getToken(),() -> {
Preferences.set("token", lg.getAccessToken().getToken());
Preferences.set("tokenExpires", tokenExpirationInMillis(lg.getAccessToken()));
//process Facebook login with "data" here
});
}
});
lg.doLogin();
}
long tokenExpirationInMillis(AccessToken token) {
String expires = token.getExpires();
if (expires != null && expires.length() > 0) {
try {
long l = (long) (Float.parseFloat(expires) * 1000);
return System.currentTimeMillis() + l;
} catch (NumberFormatException ex) {
}
}
return -1;
}
最後,撥打doLogin()
後fb.setClientSecret()
String clientId = "1171134366245722";
String redirectURI = "http://www.codenameone.com/";
String clientSecret = "XXXXXXXXXXXXXXXXXXXXXXXXXX";
Login fb = FacebookConnect.getInstance();
fb.setClientId(clientId);
fb.setRedirectURI(redirectURI);
fb.setClientSecret(clientSecret);
doLogin(fb, new FacebookData(), false);