Android中FaceBook第三方登录集成
1、首先是要先下载 FaceBook SDK Android 版;
2、导入FaceBookSDK 作为Library 引入到自己的项目中
3、在FaceBook 注冊一个 測试应用
4、配置AndroidManifest.xml:
<activity android:name="com.facebook.LoginActivity"/> <meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/app_id"/>
在String 中加入:
<string name="app_id">1548440642074664</string>5、加入 Facebook 登录button :
<com.facebook.widget.LoginButton android:id="@+id/login_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginBottom="30dp" android:layout_marginTop="30dp" />
6、在activity中加入代码:
public class MainActivity extends Activity { private static final String TAG = "MainActivity"; private UiLifecycleHelper uiHelper; private Session.StatusCallback callback = new Session.StatusCallback() { @Override public void call(Session session, SessionState state, Exception exception) { onSessionStateChange(session, state, exception); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); uiHelper = new UiLifecycleHelper(MainActivity.this, callback); uiHelper.onCreate(savedInstanceState); setContentView(R.layout.activity_main); LoginButton authButton = (LoginButton) findViewById(R.id.login_button); authButton.setReadPermissions(Arrays .asList("email","user_likes", "user_status")); } private void onSessionStateChange(Session session, SessionState state, Exception exception) { if (state.isOpened()) { Log.i(TAG, "Logged in..."); } else if (state.isClosed()) { Log.i(TAG, "Logged out..."); } } @Override public void onResume() { super.onResume(); // For scenarios where the main activity is launched and user // session is not null, the session state change notification // may not be triggered. Trigger it if it's open/closed. Session session = Session.getActiveSession(); if (session != null && (session.isOpened() || session.isClosed())) { onSessionStateChange(session, session.getState(), null); } uiHelper.onResume(); } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); uiHelper.onActivityResult(requestCode, resultCode, data); } @Override public void onPause() { super.onPause(); uiHelper.onPause(); } @Override public void onDestroy() { super.onDestroy(); uiHelper.onDestroy(); } @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); uiHelper.onSaveInstanceState(outState); } }
假设想得到用户的基本信息:
private void getUserInfo() { String fqlQuery = "SELECT uid,name,email FROM user WHERE uid = me()"; Bundle params = new Bundle(); params.putString("q", fqlQuery); Request request = new Request(Session.getActiveSession(), "/me", params, HttpMethod.GET, new Request.Callback(){ public void onCompleted(Response response) { String str = response.toString(); Log.i(TAG, str); } }); Request.executeBatchAsync(request); }