Android Core

Social API integration in Android: Access social profile using CloudRail

Social API integration is an important aspect when developing an Android app. Most of the time, we have to connect to several social networks and it is necessary to use a specific SDK. With CloudRail Social API integration, it is possible to use only one API library and connect to several social networks. This is very interesting and useful and, moreover, this API is free.

First of all, there are several scenarios where it is helpful to retrieve a social user profile so that a user doesn’t have to insert again its personal information. Consequently, this post describes, step by step,  how to use Social API integration and how to retrieve user social profiles with a few lines of code. Without CloudRail Social API, we should use different social platform SDKs (i.e Twitter, Facebook and so on) and this would be very annoying.

In conclusion of this post, you will know how to create a Social Android App that connects to different social networks and retrieve all the information. To do it, we will develop a simple android app that uses Social API integration and you will be guided step by step.

The final result is shown below:

android_social_profile-576x1024

Set up CloudRail Social API integration library

The first step is creating an android project using Android studio. Once the project is ready, we add the dependency in gradle file:

dependencies {
 compile 'com.android.support:appcompat-v7:24.2.1'
 compile 'com.cloudrail:cloudrail-si-android:2.7.2'
 compile 'com.android.support:design:24.2.1'
}

Moreover, we can create a simple layout that shows a list of buttons that we use to select the social network we want to connect to. I will skip this step because is very simple.

We will implement the login phase using an Android Service, and in more details Android IntentService, so that we will not have ANR problems. The service sends back the information, after the login, using a local broadcast receiver.

Social API integration

Below, a set of configuration parameters to use with different social networks. As you will notice, the “hard” work is configuring the app so that it can access to the social profiles. Every social network has its configuration steps to follow. Once the app is configured the social APIs are very easy to use. These steps are covered below.

Facebook Social API integration

Before using Facebook integration in Android is necessary to authorize our app. This implies a few steps:

facebook_create_app

The next step is adding Facebook login feature:

facebook_app_setup

Finally, at the end of this configuration phase, we have the authentication keys:

facebook_app_key

That’s all. We are ready to use Facebook login and retrieve user profile:

private Profile getFacebookProfile() { 
  Log.d("Prf", "Facebook Profile");
  Profile profile = new Facebook(this, "xxxxxx", "yyy");
  return profile;
}

Google plus Social API integration

To enable our app to get Google plus user profile, we have to do a few configuration steps using google console:

  1. Configure the project

android_google_api_project

2. Create credentials

cloudrail_oauth

3. Get the keys

android_google_plus_keys

Ok..that’s all!!

private Profile getGoogleProfile() {
 Log.d("Prf", "Google Profile");
 Profile profile = new GooglePlus(this, "153867151354-xx","yy");
 return profile;
}

Twitter Social API integration

In the same way, we can use Twitter social API, going to twitter developer console and creating a twitter app:

android_twitter_app_config-1024x511

and once the configuration is done, you will get

twitter_app_params_oauth

In the ‘Keys and Access Tokens‘, you have the tokens to use inside your app:

private Profile getTwitterProfile() {
  Log.d("Prf", "Twitter Profile");
  Profile profile = new Twitter(this, "xxx",   "yyy");
  return profile;
}

Github Social API integration

The same thing works with Github, going to github console:

android_profile_github_setup

and finally:

private Profile getGithubProfile() {
 Log.d("Prf", "Github Profile");
 Profile profile = new GitHub(this, "xxx", "yyy");
 return profile;
 }

LinkedIn Social Profile Integration

The last social network covered is Linkedin.

android_linkedin_profile

To get Linkedin Profile we use:

private Profile getLinkedinProfile() {
 Log.d("Prf", "Linkedin Profile");
 Profile profile = new LinkedIn(this, "xxxx", "yyy");
 return profile;
 }

At the end, you can notice that Social API uses a generic interface called Profile and we can use it to access to several social platform in an uniform way. Using this interface, the Android app can retrieve all social profile information. We will cover it below.

Building the android app

Now that we know how to access to social network profiles, it is time to create the app. Let us create a MainActivity.java that handles UI and starts LoginService to perform the login and get the user profile. The activity is shown below:

public class MainActivity extends AppCompatActivity {

private ResponseReceiver receiver;

@Override
protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  // use your key
  CloudRail.setAppKey("57f669f076b9451e6xxxxx");
  Button btnFbk = (Button) findViewById(R.id.btnFbk);
  Button btnGp = (Button) findViewById(R.id.btnGoo);
  Button btnTwi = (Button) findViewById(R.id.btnTwitter);
  Button btnLnk = (Button) findViewById(R.id.btnLink);
  Button btnLGit = (Button) findViewById(R.id.btnGithub);

  registerReciever(); // We register the receiver to get profile info

  btnFbk.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View view) {
    startLoginService(SocialProfile.FACEBOOK);
   }
  });

  btnGp.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View view) {
     startLoginService(SocialProfile.GOOGLE_PLUS);
   }
  });
  // more social buttons
 }

 // Start the service, passing the social network selected
 private void startLoginService(SocialProfile profile) {
  Intent i = new Intent(this, LoginService.class);
  i.putExtra(LoginService.PROFILE_INFO, profile );
  startService(i);
 }
...
}

The receiver, at the same time, is an inner class that handles incoming Intent request coming from the LoginService:

public class ResponseReceiver extends BroadcastReceiver {
  public static final String ACTION = "com.survivingwithandroidp.PROFILE";

  @Override
  public void onReceive(Context context, Intent intent) {
    Log.d("Prf", "Receive info");
    String fullName = intent.getStringExtra("fullName");
    String email = intent.getStringExtra("email");
    String url = intent.getStringExtra("url");
    createDialog(fullName, email, url);
  }
}

We pass the profile details setting intent values. You could use instead a profile class that holds all the profile info. This class have to implement Android Parcelable interface so you can pass it from the service to the activity.

And when the profile information is ready, the app opens a dialog showing the details.

android_social_profile_github-576x1024

This example shows the Github profile.

Finally, the LoginService.java source code:

public class LoginService extends IntentService {
    public static String PROFILE_INFO = "profile_info";

    public LoginService() {
        super("LoginService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        Log.d("Prf", "Login service");
        SocialProfile socialProfile = null;

        Profile profile = null;

        if (intent != null)
            socialProfile = (SocialProfile) intent.getSerializableExtra(LoginService.PROFILE_INFO);

        switch(socialProfile) {
            case FACEBOOK:
               profile = getFacebookProfile();
                break;
            case GOOGLE_PLUS:
                profile = getGoogleProfile();
                break;
            case TWITTER:
                profile = getTwitterProfile();
                break;
            case LINKEDIN:
                profile = getLinkedinProfile();
                break;
            case GITHUB:
                profile = getGithubProfile();
                break;
        }


        String fullName = profile.getFullName();
        String email = profile.getEmail();
        String url = profile.getPictureURL();

        sendBroatcast(fullName, email, url);
    }

At the end of this post, you, hopefully, gained the knowledge about using social API integration in Android. You can use it to access to social user profile and retrieve its information. It can be useful, for example, if you want to implement a signup feature.

Francesco Azzola

He's a senior software engineer with more than 15 yrs old experience in JEE architecture. He's SCEA certified (Sun Certified Enterprise Architect), SCWCD, SCJP. He is an android enthusiast and he has worked for long time in the mobile development field.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button