简体   繁体   English

使用Graph API在Android应用程序中登录Facebook

[英]Facebook Login in Android application using Graph api

Anybody has working sample code for Facebook login in Android using the code snippet mentioned in Facebook developer site ??I couldn't understand it properly.I want to get the name and profile picture of logged in user.I want my app to display the name and profile picture as long as the session is active and need to modify the details if user changed any. 任何人都有使用Facebook开发人员网站中提到的代码段在Android中登录Facebook的有效示例代码?我无法正确理解。我想获取已登录用户的名称和个人资料图片。我希望我的应用显示名称和个人资料图片,只要会话处于活动状态,并且如果用户更改了任何内容,则需要修改详细信息。 Currently what i do is,saving access token and name in shared preferences on first login and saving the image in sd card and checking access token value during each app launch.If access token value is not null,then i display the name from shared preferences and profile picture from sd card.I know that this is not the right way to do this.Somebody please help me with this. 目前我要做的是,在首次登录时将访问令牌和名称保存在共享首选项中,并将图像保存在sd卡中,并在每次启动应用程序时检查访问令牌值。如果访问令牌值不为null,那么我将显示共享首选项中的名称和sd卡中的个人资料照片。我知道这样做不是正确的方法。请有人帮助我。

You can this https://github.com/sromku/android-simple-facebook library it is pretty well defined and can get details of methods by searching simle facebook android on google. 您可以在https://github.com/sromku/android-simple-facebook这个库中定义得很好,并可以通过在google上搜索simle facebook android来获取方法的详细信息。 Do whatever you want to do with facebook with this library....happy coding 使用此库,对Facebook做任何您想做的事情。

Try this code 试试这个代码

public class LoginActivity extends Activity {

private Button butLogin, butMaps, butJackpot, butAdministrator, buttonMenu;
public static String APP_ID = " paste your app_id";
public static Facebook facebook;
private AsyncFacebookRunner mAsyncRunner;
public static SharedPreferences mPrefs;
private static final String TAG = "Activity";


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    loginToFacebook();
}           
// Method to call the Facebook login
protected void loginToFacebook() {
    facebook = new Facebook(APP_ID);
    mAsyncRunner = new AsyncFacebookRunner(facebook);
    mPrefs = getSharedPreferences("faceBook", 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", "public_profile",
                "publish_stream" }, Facebook.FORCE_DIALOG_AUTH,
                new DialogListener() {
                    @Override
                    public void onCancel() {
                    }

                    @Override
                    public void onComplete(Bundle values) {
                        getProfileInformation();
                    }

                    @Override
                    public void onError(DialogError error) {
                    }

                    @Override
                    public void onFacebookError(FacebookError fberror) {
                    }
                });
    } else {
        getProfileInformation();
    }
}

// FaceBook getting profile information
public void getProfileInformation() {
    showLoadingImage();
    Helper.setFacebookLogin(getApplicationContext(), true);
    mAsyncRunner.request("me", new RequestListener() {
        @Override
        public void onComplete(String response, Object state) {
            String json = response;
            try {
                Log.i("JSOB", json);
                JSONObject profile = new JSONObject(json);

                try {
                Bitmap bmp = null;
                    URL image_value = new URL("http://graph.facebook.com/"
                            + profile.getString("id")
                            + "/picture?type=large");
                    bmp =  BitmapFactory.decodeStream(image_value.openConnection().getInputStream());
                    profile_pic.setImageBitmap(bmp);
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                }
                String first_name= profile.getString("first_name"));
                String last_name=profile.getString("last_name"));
                String email=profile.getString("email"));


            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onIOException(IOException e, Object state) {
        }

        @Override
        public void onFileNotFoundException(FileNotFoundException e,
                Object state) {
        }

        @Override
        public void onMalformedURLException(MalformedURLException e,
                Object state) {
        }

        @Override
        public void onFacebookError(FacebookError e, Object state) {
        }
    });
   }

    // faceBook login method end

  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM