API integration in Android: Access social profile using CloudRail

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 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.

There are several scenarios where it is helpful to retrieve a social user profile. For example, on the sign-up page so that a user doesn’t have to insert again its personal information. Thus, this post describes, step by step,  how to use Social API integration.  Moreover, it describes 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). All 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. The app will 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.

Connect to several social platforms using the same APIClick To Tweet


The final result is shown below:

android api integration

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:<latest version>'
 compile 'com.cloudrail:cloudrail-si-android:<latest version>'
 compile 'com.android.support:design:<latest version>'

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.

social api integration

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 the social profiles. Every social network has its configuration steps to follow. Once the app is configured, we can use the API to integrate the social platform with our app and everything is 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:

configure facebook 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 keys

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

2. Create credentials

android google oauth

3. Get the keys

android google api 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:

twitter app config api with android

and once the configuration is done, you will get:

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 github profile

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;

protected void onCreate(Bundle savedInstanceState) {
  // use your key
  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() {
   public void onClick(View view) {

  btnGp.setOnClickListener(new View.OnClickListener() {
   public void onClick(View view) {
  // 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 );

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";

  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. Instead, you could use a class holding  all the profile info. This class has to implement Android Parcelable interface. In this way, 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

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() {

    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();
            case GOOGLE_PLUS:
                profile = getGoogleProfile();
            case TWITTER:
                profile = getTwitterProfile();
            case LINKEDIN:
                profile = getLinkedinProfile();
            case GITHUB:
                profile = getGithubProfile();

        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.

More posts...

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.