Parse SDK Android – Facebook Graph API v2.0

Introduction

This article will guide you through using Facebook Graph API v2.0 with Parse SDK for Android. Parse, a cloud platform, allows you to integrate Facebook features into your Android applications seamlessly. Graph API v2.0 is the latest version of Facebook’s API, offering enhanced capabilities and security features.

Prerequisites

Setting up Your Facebook App

1. Create a Facebook App

  • Log in to your Facebook developer account.
  • Go to “My Apps” and click “Create App”.
  • Choose “Consumer” as the platform and provide a relevant app name and description.
  • Create the app.

2. Add a Platform

  • On the app dashboard, click “Settings” -> “Basic”.
  • In the “Platforms” section, click “Add Platform”.
  • Select “Android” and provide your app’s package name (found in your Android project’s `AndroidManifest.xml` file).
  • Generate a key hash (more on this later) and paste it into the provided field.
  • Save the changes.

3. Obtaining Your App ID and App Secret

  • On your app dashboard, copy your App ID and App Secret. You’ll need these values for the Parse integration.

Setting up Your Parse App

1. Add Parse SDK to Your Project

  • Open your Android project in Android Studio.
  • Go to “File” -> “Project Structure”.
  • In the “Modules” section, select your app module.
  • Click “Dependencies” -> “+” -> “Library Dependency”.
  • Search for “Parse” and select the latest version.
  • Click “OK”.

2. Initialize Parse

  • In your `Application` class, initialize Parse with your app’s information:
  import com.parse.Parse;
  import com.parse.ParseObject;

  public class MyApplication extends Application {

  @Override
  public void onCreate() {
  super.onCreate();
  Parse.initialize(this, "YOUR_APP_ID", "YOUR_CLIENT_KEY");
  ParseObject.registerSubclass(YourCustomObject.class); // For custom objects
  }
  }

3. Enable Facebook Login

  • In your `build.gradle` file (Module: app), add the following dependencies:
 dependencies {
  // ... other dependencies
  implementation "com.facebook.android:facebook-android-sdk:5.16.2" // Ensure compatibility
  implementation "com.parse.bolts:bolts-android:1.4.0" // For async tasks
  }

Integrating Facebook Graph API v2.0

1. Key Hash Generation

  • Open a terminal or command prompt.
  • Navigate to your project directory.
  • Run the following command (replace `your_package_name` with your app’s package name):
keytool -exportcert -alias androiddebugkey -keystore ~/.android/debug.keystore | openssl sha1 -binary | openssl base64
  • Copy the generated key hash and paste it into the Facebook app settings (where you added the Android platform).

2. Facebook Login Implementation

  • In your activity, include the Facebook Login button:
  
  • Create a `LoginButton` object and set its permissions:
  import com.facebook.login.LoginManager;
  import com.facebook.login.LoginResult;
  import com.facebook.CallbackManager;

  LoginButton loginButton = findViewById(R.id.login_button);
  CallbackManager callbackManager = CallbackManager.Factory.create();
  loginButton.setReadPermissions("email", "public_profile"); // Request necessary permissions
  loginButton.registerCallback(callbackManager, new FacebookCallback() {
  @Override
  public void onSuccess(LoginResult loginResult) {
  // User logged in successfully
  }

  @Override
  public void onCancel() {
  // User cancelled the login attempt
  }

  @Override
  public void onError(FacebookException error) {
  // Handle login error
  }
  });

3. Making Graph API Calls

  • Retrieve the user’s access token after successful login:
 AccessToken accessToken = AccessToken.getCurrentAccessToken();
  if (accessToken != null) {
  // Access token available, make Graph API calls
  }
  • Use Parse’s `ParseFacebookUtils` to make Graph API calls:
  import com.parse.ParseFacebookUtils;
  import com.parse.ParseUser;

  ParseFacebookUtils.logInWithReadPermissionsInBackground(
  new String[]{"email", "public_profile"},
  new LogInCallback() {
  @Override
  public void done(ParseUser user, ParseException e) {
  if (user == null) {
  // User cancelled the login or log in failed
  } else {
  // User logged in through Facebook
  }
  }
  }
  );
  • You can use the access token to retrieve user data from Facebook Graph API v2.0:
  import com.facebook.GraphRequest;
  import com.facebook.GraphResponse;
  import org.json.JSONObject;

  GraphRequest request = GraphRequest.newMeRequest(accessToken,
  new GraphRequest.GraphJSONObjectCallback() {
  @Override
  public void onCompleted(JSONObject object, GraphResponse response) {
  if (response.getError() != null) {
  // Handle error
  } else {
  // User data from Graph API
  String name = object.getString("name");
  String email = object.getString("email");
  // ... more data
  }
  }
  });
  Bundle parameters = new Bundle();
  parameters.putString("fields", "name,email");
  request.setParameters(parameters);
  request.executeAsync();

Conclusion

By following these steps, you can successfully integrate Facebook Graph API v2.0 with Parse SDK in your Android applications. This integration allows you to leverage the power of Facebook’s social features and data to enhance user experiences and build robust functionalities within your apps.


Leave a Reply

Your email address will not be published. Required fields are marked *