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:
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:
The next step is adding Facebook login feature:
Finally, at the end of this configuration phase, we have the authentication 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:
- Configure the project
2. Create credentials
3. Get the 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:
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:
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.
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.
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.
Reference: | Social API integration in Android: Access social profile using CloudRail from our JCG partner Francesco Azzola at the Surviving w/ Android blog. |