Learn how to integrate social network profile in your Android app: Step by Step guide

Learn how to integrate social network profile in your Android app using a simple and unique library that supports all the social networks

Social Network Profile integration is an important aspect when developing an Android app. Most of the time, we have to connect to several social networks to retrieve user profile information. To do it, it is necessary to use a specific SDK. With CloudRail API integration, it is possible to use only one Android 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 integrate the Social network profiles using a uniform 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.

 

The final result is shown below:

Social network profile integration into Android app

Set up CloudRail Social network 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:

[xml] dependencies {
compile ‘com.android.support:appcompat-v7:<latest version>’
compile ‘com.cloudrail:cloudrail-si-android:<latest version>’
compile ‘com.android.support:design:<latest version>’
}[/xml]

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.

How to integrate Social network profile in your Android app

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:

social network profile integration: 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:

[java]private Profile getFacebookProfile() {
Log.d(“Prf”, “Facebook Profile”);
Profile profile = new Facebook(this, “xxxxxx”, “yyy”);
return profile;
}[/java]

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

[java] private Profile getGoogleProfile() {
Log.d(“Prf”, “Google Profile”);
Profile profile = new GooglePlus(this, “153867151354-xx”,”yy”);
return profile;
}[/java]

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:

[sociallocker]

twitter app params oauth

[/sociallocker]

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

[java] private Profile getTwitterProfile() {
Log.d(“Prf”, “Twitter Profile”);
Profile profile = new Twitter(this, “xxx”, “yyy”);
return profile;
}[/java]

Github Social API integration

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

android github profile

and finally:

[java]private Profile getGithubProfile() {
Log.d(“Prf”, “Github Profile”);
Profile profile = new GitHub(this, “xxx”, “yyy”);
return profile;
}[/java]

LinkedIn Social Profile Integration

The last social network covered is Linkedin.

Android Linkedin profile

To get Linkedin Profile we use:

[java] private Profile getLinkedinProfile() {
Log.d(“Prf”, “Linkedin Profile”);
Profile profile = new LinkedIn(this, “xxxx”, “yyy”);
return profile;
}[/java]

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:

[java] 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);
}

}[/java]

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

[java]

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);
}
}[/java]

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:

[java]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);
}[/java]

 

 

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