private void loginToFacebook() {
// TODO Auto-generated method stub
mPrefs = getPreferences(MODE_PRIVATE);
String access_token = mPrefs.getString("access_token", null);
long expires = mPrefs.getLong("access_expires", 0);
if (access_token != null) {
facebook.setAccessToken(access_token);
}
if (expires != 0) {
facebook.setAccessExpires(expires);
}
if (!facebook.isSessionValid()) {
facebook.authorize(this,
new String[] { "email", "publish_stream" },
new DialogListener() {
@Override
public void onCancel() {
// Function to handle cancel event
}
@Override
public void onComplete(Bundle values) {
// Function to handle complete event
// Edit Preferences and update facebook acess_token
SharedPreferences.Editor editor = mPrefs.edit();
editor.putString("access_token",
facebook.getAccessToken());
editor.putLong("access_expires",
facebook.getAccessExpires());
try {
// PROFILE DETAILS AND FRIENDS COUNTER
String queryUserInfo = "SELECT uid FROM user WHERE uid = me()";
Bundle paramUserInfo = new Bundle();
paramUserInfo.putString("method", "fql.query");
paramUserInfo.putString("query", queryUserInfo);
String responseUserInfo = Util.facebook
.request(paramUserInfo);
JSONArray JAUserData = new JSONArray(
responseUserInfo);
for (int i = 0; i < JAUserData.length(); i++) {
JSONObject JOUserData = JAUserData
.getJSONObject(i);
if (JOUserData.has("uid")) {
String getUserID = JOUserData
.getString("uid");
}
}
} catch (Exception e) {
e.printStackTrace();
}
/*
* try { editor.putString("access_expires",
* facebook.request("me"));
*
* try { JSONObject json =
* Util.parseJson(facebook.request("me", values));
* String userId = json.getString("id");
*
* } catch (FacebookError e) { // TODO
* Auto-generated catch block e.printStackTrace(); }
* catch (JSONException e) { // TODO Auto-generated
* catch block e.printStackTrace(); }
*
* Log.i(TAG, facebook.request("me")); } catch
* (MalformedURLException e) { // TODO
* Auto-generated catch block e.printStackTrace(); }
* catch (IOException e) { // TODO Auto-generated
* catch block e.printStackTrace(); }
*/
editor.commit();
}
@Override
public void onError(DialogError error) {
// Function to handle error
Log.wtf(TAG, error);
}
@Override
public void onFacebookError(FacebookError fberror) {
// Function to handle Facebook errors
Log.wtf(TAG, fberror);
}
});
}
}
私のアプリのログイン画面に「Facebookでログイン」ボタンがあり、ユーザーがクリックすると、ユーザーの詳細を入力した後にアプリにFacebookのログイン画面が表示されます。ここでは、IDを取得する方法を説明します。