Part I:
SDK Version:
M3 1. Step
First of all, download the official Facebook SDK from this site: https://github.com/facebook/fa
After that, create an application in this http://www.facebook.com/develo
2. Step
You need to create the key hash value of your signature and your android debugkeystore (for the develop stage), and than add them to your Facebook Application, in the application site. (Edit Settings -> Mobile and Devices section)
To do this, we need Openssl, download from: http://code.google.com/p/opens
To create this hash values, you need to navigate to your JAVA jdk folder, where the keytool.exe is. (In my case, in windows is: c:\Program Files(x86)\Java\jdk 1.6.0_24\bin)
Copy your debug.keystore to there from the (in my case) c:\Users\MyUserName\.android folder. In the jdk/bin folder, open a command prompt, and execute the following:
keytool -exportcert -alias androiddebugkey -keystore debug.keystore > c:\openssl\b in\debug.txt
Navigate to the openssl/bin folder, and we have a debug.txt here, which contains the keystore values, but not in the expected format! Open a command promt from there, and execute the following commands:
- openssl base64 -in debug_sha.txt > debug_base64.txt
3. Step
Finally we can start coding... In the next part!
Part II:
In this part, I show you an android application, which logging in to Facebook, then get the Facebook ID.
Please go through the first part of the tutorial, before reading this post.
1. Step
Create a new Android project in Eclipse, choose "create project from existing source", and set the location to: "Your Facebook SDK"\facebook. In my case it's D:\facebook android sdk\facebook.
Create a new empty Android project, and open project properties/android. Hit Add in the Library section, add the facebook project.
Change the main.xml like this:
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.c
om/apk/res/android" - android:orientation="vertical" android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <TextView android:layout_width="fill_parent" android:id="@+id/textFacebook"
- android:gravity="center_horizontal" android:layout_height="wrap_content"
- android:text="@string/welcome" android:layout_alignParentTop="true" />
- <Button android:text="@string/enter" android:id="@+id/buttonLogin"
- android:layout_below="@+id/textFacebook"
- android:layout_centerHorizontal="true" android:layout_width="wrap_content"
- android:layout_height="wrap_content" android:layout_marginTop="30dip"></Button>
- <ProgressBar android:id="@+id/progressLogin"
- android:layout_centerInParent="true" android:layout_width="wrap_content"
- android:visibility="gone" android:layout_height="wrap_content"></ProgressBar>
- </RelativeLayout>
- <uses-permission android:name="android.permission.INTER
NET"> - </uses-permission>
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="welcome">Welcome </string>
- <string name="app_name">FacebookTest</string>
- <string name="enter">Log in to Facebook</string>
- </resources>
Create an abstract class, for the Facebook connection. Don't forget to insert your app id!
- public abstract class FBConnectionActivity extends Activity {
- private Facebook mFacebook;
- private AsyncFacebookRunner mAsyncRunner;
- private SharedPreferences sharedPrefs;
- private TextView username;
- private ProgressBar pb;
- public void setConnection() {
- mContext = this;
- mFacebook = new Facebook(APP_ID);
- mAsyncRunner = new AsyncFacebookRunner(mFacebook);
- }
- public void getID(TextView txtUserName, ProgressBar progbar) {
- username = txtUserName;
- pb = progbar;
- if (isSession()) {
- Log.d(TAG, "sessionValid");
- mAsyncRunner.request("me", new IDRequestListener());
- } else {
- // no logged in, so relogin
- Log.d(TAG, "sessionNOTValid, relogin");
- mFacebook.authorize(this, PERMS, new LoginDialogListener());
- }
- }
- public boolean isSession() {
- sharedPrefs = PreferenceManager.getDefaultSharedPreferences(mContext);
- Log.d(TAG, access_token);
- if (access_token != null && expires != -1) {
- mFacebook.setAccessToken(access_token);
- mFacebook.setAccessExpires(expires);
- }
- return mFacebook.isSessionValid();
- }
- private class LoginDialogListener implements DialogListener {
- @Override
- public void onComplete(Bundle values) {
- Log.d(TAG, "LoginONComplete");
- long token_expires = mFacebook.getAccessExpires();
- Log.d(TAG, "AccessToken: " + token);
- Log.d(TAG, "AccessExpires: " + token_expires);
- sharedPrefs = PreferenceManager
- .getDefaultSharedPreferences(mContext);
- sharedPrefs.edit().putLong("access_expires", token_expires)
- .commit();
- sharedPrefs.edit().putString("access_token", token).commit();
- mAsyncRunner.request("me", new IDRequestListener());
- }
- @Override
- public void onFacebookError(FacebookError e) {
- Log.d(TAG, "FacebookError: " + e.getMessage());
- }
- @Override
- public void onError(DialogError e) {
- Log.d(TAG, "Error: " + e.getMessage());
- }
- @Override
- public void onCancel() {
- Log.d(TAG, "OnCancel");
- }
- }
- private class IDRequestListener implements RequestListener {
- @Override
- try {
- Log.d(TAG, "IDRequestONComplete"
;); - Log.d(TAG, "Response: " + response.toString());
- public void run() {
- username.setText("Welcome: " + name+"\n ID: "+id);
- pb.setVisibility(ProgressBar.GONE);
- }
- });
- } catch (JSONException e) {
- Log.d(TAG, "JSONException: " + e.getMessage());
- } catch (FacebookError e) {
- Log.d(TAG, "FacebookError: " + e.getMessage());
- }
- }
- @Override
- Log.d(TAG, "IOException: " + e.getMessage());
- }
- @Override
- Log.d(TAG, "FileNotFoundException: " + e.getMessage());
- }
- @Override
- Log.d(TAG, "MalformedURLException: " + e.getMessage());
- }
- @Override
- Log.d(TAG, "FacebookError: " + e.getMessage());
- }
- }
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- }
- }
Add the connection activity to the manifest:
- <activity android:name=".FBConnectionActivity&qu
ot; android:label="@string/app_name"></activity>
Change the Main activity like that:
- public class Main extends FBConnectionActivity {
- private TextView txtUserName;
- private ProgressBar pbLogin;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- txtUserName = (TextView) findViewById(R.id.textFacebook);
- pbLogin = (ProgressBar) findViewById(R.id.progressLogin);
- btnLogin.setOnClickListener(new OnClickListener() {
- @Override
- pbLogin.setVisibility(ProgressBar.VISIBLE);
- setConnection();
- getID(txtUserName, pbLogin);
- }
- });
- }
- }
(helloandroid)