ÔõÑùʹÓÃFacebook SDK¾ÙÐеÚÈý·½µÇ¼²Ù×÷£¿
ÔÚ»¥ÁªÍøµÄʱ´úÖУ¬É罻ýÌåÓ¦ÓñäµÃÔ½À´Ô½ÆÕ¼°£¬¶øfacebookÊÇÆäÖÐ×îÊܽӴýµÄÒ»¸ö¡£Í¨¹ýʹÓÃfacebook£¬Äú¿ÉÒÔ½¨ÉèÒ»¸öÕË»§²¢Óë¼ÒÈË¡¢ÅóÙ»ò¿Í»§¼á³ÖÁªÏµ£¬Ò²¿ÉÒÔʹÓÃfacebookµÇ¼»á¼ûÆäËûµÚÈý·½Ó¦ÓûòÍøÕ¾¡£ÕâÖֵǼ·½·¨¹ØÓÚÓû§À´ËµºÜÊÇÀû±ã£¬¶ø¹ØÓÚ¿ª·¢Ö°Ô±À´ËµÒ²¿ÉÒÔ¼ò»¯ÕË»§ÖÎÀíºÍ¿ª·¢Àú³Ì¡£ÔÚ±¾ÎÄÖУ¬ÎÒÃǽ«ÏÈÈÝÔõÑùʹÓÃfacebook sdk¾ÙÐеÚÈý·½µÇ¼²Ù×÷¡£
ʲôÊÇFacebook SDK£¿
Facebook SDKÊÇFacebookÌṩµÄ¿ª·¢¹¤¾ß°ü£¬ÓÃÓÚ¿ª·¢Ö°Ô±Í¨¹ýFacebook APIÓëFacebook¾ÙÐн»»¥¡£¸Ã¹¤¾ß°üΪFacebookµÇ¼¡¢·ÖÏíºÍ¹ã¸æÌṩÁËAPIºÍ¹¤¾ß¡£
Facebook SDKÌṩÁËÒ»ÖÖÇáËɵķ½·¨À´ÊµÏÖFacebookµÇ¼¡£±ðµÄ£¬Ëü»¹Ö§³ÖFacebook Graph API£¨¿ª·¢Õß¿ÉÒÔÅÌÎÊÓйØFacebookÓû§µÄÏêϸÐÅÏ¢£©¡¢Ó¦ÓÃÄÚ¹ºÖá¢À©Õ¹Á¿¶È¡¢ÍÆËÍ֪ͨµÈ¹¦Ð§¡£
ʹÓÃFacebook SDK¾ÙÐеÚÈý·½µÇ¼
Ê×ÏÈ£¬ÔÚʹÓÃFacebook SDK֮ǰ£¬ÎÒÃDZØÐèÔÚFacebook¿ª·¢ÕßÒ³ÃæÉϽ¨ÉèÒ»¸öÓ¦Óá£ÔÚ½¨ÉèÓ¦ÓÃÖ®ºó£¬ÎÒÃÇ¿ÉÒÔ´ÓFacebook¿ª·¢ÕßÒ³ÃæÉÏ»ñÈ¡Ó¦ÓÃIDºÍÃÜÔ¿£¬ÕâЩÐÅÏ¢½«ÓÃÓÚÔÚ×ðÁú¿Ê±Ó¦ÓóÌÐòÖÐÉèÖÃFacebook SDK¡£
°ì·¨1£ºÏòÏîÄ¿Ìí¼ÓFacebook SDK
ÔÚAndroid StudioÖУ¬ÎÒÃÇ¿ÉÒÔͨ¹ýÌí¼ÓÒÔÏÂÒÀÀµÀ´¼¯³ÉFacebook SDK£º
implementation ‘com.facebook.android:facebook-android-sdk:[4,5)’
°ì·¨2£ºÉèÖÃÓ¦ÓÃIDºÍÃÜÔ¿
ҪʹÓÃFacebook SDK¾ÙÐеÚÈý·½µÇ¼²Ù×÷£¬ÎÒÃÇÐèÒªÔÚAndroidManifest.xmlÎļþÖÐÌí¼ÓÒÔÏ´úÂëÐÐÀ´Ìí¼ÓFacebookÓ¦ÓÃID£º
ÔÚÕâÀÎÒÃDZØÐ轨ÉèÒ»¸öÃûΪ¡°facebook_app_id¡±µÄ×Ö·û´®¡£ÎÒÃÇ»¹ÐèÒª½«Ó¦ÓÃIDÌí¼Óµ½×ðÁú¿Ê±strings.xmlÎļþÖС£
YOUR_APP_ID
ÎÒÃÇ»¹¿ÉÒÔʹÓôúÂëͨ¹ýFacebookSdk.initialize()ÒªÁìÀ´ÉèÖÃÓ¦ÓÃIDºÍÃÜÔ¿¡£
°ì·¨3£º½¨ÉèFacebookµÇ¼°´Å¥
Facebook SDKΪÎÒÃÇÌṩÁËÒ»¸öÒ×ÓÚʹÓò¢¿ÉÒÔÇáËɶ¨ÖƵÄFacebookµÇ¼°´Å¥¡£ÎÒÃÇ¿ÉÒÔʹÓô˰´Å¥ÔÚÓ¦ÓÃÖÐʵÏֵǼ£¬²¢ÇÒ¿ÉÒÔÖ§³Ö²î±ðµÄ°´Å¥Æø¸Å¡£
ÎÒÃÇ¿ÉÒÔÔÚXMLÎļþÖÐÌí¼ÓÒÔÏ´úÂëÐÐÀ´½¨ÉèFacebookµÇ¼°´Å¥£º
°ì·¨4£ºÎªµÇ¼°´Å¥ÉèÖÃȨÏÞ
ÔÚʹÓÃFacebook SDK¾ÙÐеǼ֮ǰ£¬ÎÒÃÇÐèÒªÉèÖÃÐèÒª»ñµÃµÄÓû§ÐÅÏ¢µÄȨÏÞÁÐ±í¡£Facebook SDKÌṩÁËÐí¶àȨÏÞÑ¡Ï°üÀ¨public_profile¡¢emailºÍuser_friendsµÈ¡£ÎÒÃÇ¿ÉÒÔͨ¹ýÌí¼ÓÒÔÏ´úÂëÐÐÀ´ÉèÖÃȨÏÞ£º
login_button.setReadPermissions(Arrays.asList(“public_profile”, “email”));
°ì·¨5£º´¦Öóͷ£FacebookµÇ¼Ч¹û
ÎÒÃÇ¿ÉÒÔÔڻص÷ÒªÁìÖд¦Öóͷ£µÇ¼Ч¹û¡£ÕâÐèҪʵÏÖFacebook CallbackManager¡£
Ê×ÏÈ£¬ÎÒÃÇÐèÒª½¨ÉèÒ»¸öCallbackManagerʵÀý£º
private CallbackManager callbackManager;
È»ºó£¬ÔÚonCreate()ÒªÁìÖУ¬ÎÒÃÇÐèÒª½«CallbackManagerÓë×ðÁú¿Ê±LoginButtonʵÀý¹ØÁªÆðÀ´£º
loginButton.registerCallback(callbackManager, new FacebookCallback () {
@Override public void onSuccess(LoginResult loginResult) { //´¦Öóͷ£µÇ¼ÀֳɺóµÄ²Ù×÷ } @Override public void onCancel() { //´¦Öóͷ£µÇ¼×÷·ÏºóµÄ²Ù×÷ } @Override public void onError(FacebookException error) { //´¦Öóͷ£µÇ¼ÍÉ»¯ÎéµÄ²Ù×÷ }
µÇ¼ºó¸´ÖÆ
});
×îºó£¬ÎÒÃÇ¿ÉÒÔÖØдonActivityResult()ÒªÁì²¢½«Ð§¹ûת´ïµ½CallbackManagerÖУº
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data); callbackManager.onActivityResult(requestCode, resultCode, data);
µÇ¼ºó¸´ÖÆ
}
ͨ¹ýÕâЩ°ì·¨£¬ÎÒÃǾͿÉÒÔʹÓÃFacebook SDKÇáËɵØʵÏÖµÚÈý·½µÇ¼²Ù×÷¡£Facebook SDKÌṩÁËÒ×ÓÚʹÓúÍÎÞаµÄAPI£¬²¢ÇÒ¿ÉÒÔÔöÇ¿Ó¦ÓóÌÐòµÄ¹¦Ð§ºÍÓû§ÌåÑé¡£
ÒÔÉϾÍÊÇÔõÑùʹÓÃFacebook SDK¾ÙÐеÚÈý·½µÇ¼²Ù×÷£¿µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡