How to integrate Social API in Android
Social API integration is now possible in Android using CloudRail, but what how is it possible? Before describing you about Social API integration, let me tell you about CloudRail.
What is CloudRail?
CloudRail is an API integration solution that allows a one- to- many approaches in connecting to services. You can integrate multiple services in just one API. For example, you can integrate Facebook, Twitter, Google+, Instagram, LinkedIn, Yahoo, GitHub and Windows Live in just one App. Here is the description of interfaces that it supports:
- Cloud Storage
- Social Profiles
- Social Interaction
- Payment
- SMS
It offers some interesting features like you can easily download files from cloud storage, upload files to cloud storage, get information about files and folders that are already stored and retrieve user information in a simple way.
Social media API
When you are developing an Android App, the first thing to do is Social API integration. An API is a common path that allows two applications or programs, to communicate with each other, whereas Social API is a structure that allows Web browsers to integrate with social media services. The most popular Social API is as follows:
- Facebook: It is the most popular social media.
- Twitter: It is also a popular social micro-blogging service.
- Instagram: It is a photo sharing App and service.
- LinkedIn: It is recognized as the largest enterprise social network.
- Google+: It is an interesting social networking site, owned and operated by google.
- Pinterest: It is called the virtual pinboard.
When we have to connect these social networks and we use a definite SDK, then CloudRail Social API Integration can connect you to several networks using only one API library. This API is free, fascinating and very helpful.
Now let me explain to you how to use Social API integration and how using the code you can retrieve user social profiles. I will step by step develop an Android App that uses Social API integration.First, create an account on CloudRail if you already have an account then sign in…
1. Click on Create New App and enter your app name and hit create button.
2. Next step you will get a License Key and dependencies Guide to add in your android studio project.
Facebook Social media API integration
Each social network has its own configuration steps to follow. The usage of APIs becomes simple, once the App is configured. Authorization steps for Facebook are given below
first go to https://developers.facebook.com/apps and click on Add a New App.
2.Now you will provide your Display Name and email address and Chose Category .
3. adding Facebook login feature click on Facebook login and provide require detail and click save as shown in the pic .
4. At the end return to dashboard and get authentication keys
Facebook Login and Retrieval of User Profile
private Profile getFacebookProfile() { Log.d("Profile", "Fb Profile"); Profile profile = new Facebook(this, "xxxx", "yyy"); return profile; }
Google Social media API integration
- Configuration steps using Google Console,go to Google plus Console and Click on CREATE PROJECT button enter project name and hit CREATE .
2.As the project will be ready you will get redirected to your project console. in windows on the left side click on Credentials and then click on OAuth consent screen enter your Product name shown to users and hit Save button.
3.Go to search bar and type plus then select the Google+ API and then select ENABLE as shown in the pic below
4. Now click on Go to Credentials button and click on client ID and then select Web Application Enter Name and Authorized redirect URIs then hit create button as shown in pic
you will get client ID and client secret
private Profile getGoogleProfile() { Log.d("Prf", "Google Profile"); Profile profile = new GooglePlus(this, "xxxx","yy"); return profile; }
Twitter Social media API integration
We’ll follow the same steps as we performed earlier first we will go to Twitter Apps and Create a new App hitting Create New App button then fill require detail as shown in pic below and hit Create your Twitter application button
Now click on Keys and Access Tokens here you will find your Consumer Key (API Key) and Consumer Secret (API Secret)
private Profile getTwitterProfile() { Log.d("Prf", "Twitter Profile"); Profile profile = new Twitter(this, "xXXxx","yyy"); return profile; }
GitHub social API integration
Go to GitHub console and if you have already account sign in or signup to access GitHub console fill required information and hit Register application button. and you will get your Client ID and Client Secret.
private Profile getGithubProfile() { Log.d("Prf", "Github Profile"); Profile profile = new GitHub(this, "xxx", "yyy"); return profile; }
Hit Create Application and fill required detail
after hitting Submit button you will Authentication Keys
private Profile getLinkedinProfile() { Log.d("Prf", "Linkedin Profile"); Profile profile = new LinkedIn(this, "xxXXxx", "yyy"); return profile; }
Instagram social API integration
Go to instagram developer console click Register Your Application button.next step fill required filled with require information and hit signup button.
click Register Your Application button again and then click on Register a New Client fill required information and hit Register and on next screen, you will find your CLIENT ID.
Windows Live social sign in
Go to Windows Live social sign in console Click on Register your app if you are not sign please sign in Microsoft account then click on android logo/pic fill required detail and hit Create button
Next windows will provide you dependencies permissions and others code just follow the instruction add all codes in your app run app and click the button to launch a browser Microsoft will provide you token. later in browser fill required information and get access to a token.
Yahoo social network api
Login to your yahoo account Go to yahoo social network api click on Create an APP button on next screen fill all the field with required information and hit Create App Button on next screen you will find client secret or app secret key.
Social API uses a generic interface called Profile to access to the different social platform in a smooth way. Android App can retrieve all social profile information using this interface. Let’s see how it is done.
Building the Android App
dependencies
We will add following dependencies in build.gradle
compile 'com.cloudrail:cloudrail-si-android:2.9.0'
AndroidManifest
in manifest, we will add these permissions and service.
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <service android:name=".Cloud_Login_Service" />
MainActivity.java is used to handle UI and starts LoginService to log in and get the user profile, as shown below don’t forget to add your CloudRail app key.
package exceptionbound.come.socialtest; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import com.cloudrail.si.CloudRail; public class MainActivity extends AppCompatActivity { private Social_media_Profiles social_media_Profiles_profiles; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); CloudRail.setAppKey("58262141525gf78kl0po123f"); } public void FacebookLogin(View view) { social_media_Profiles_profiles = social_media_Profiles_profiles.FACEBOOK; performLogin(); } public void TwitterLogin(View view) { social_media_Profiles_profiles = social_media_Profiles_profiles.TWITTER; performLogin(); } public void GooglePlusLogin(View view) { social_media_Profiles_profiles = social_media_Profiles_profiles.GOOGLE_PLUS; performLogin(); } public void InstagramLogin(View view) { social_media_Profiles_profiles = social_media_Profiles_profiles.INSTAGRAM; performLogin(); } public void LinkedInLogin(View view) { social_media_Profiles_profiles = social_media_Profiles_profiles.LINKED_IN; performLogin(); } public void YahooLogin(View view) { social_media_Profiles_profiles = social_media_Profiles_profiles.YAHOO; performLogin(); } public void WinLiveLogin(View view) { social_media_Profiles_profiles = social_media_Profiles_profiles.WINDOWS_LIVE; performLogin(); } public void GitHubLogin(View view) { social_media_Profiles_profiles = social_media_Profiles_profiles.GITHUB; performLogin(); } public void performLogin() { Intent intent = new Intent(this, Cloud_Login_Service.class); intent.putExtra(Cloud_Login_Service.Profile_Extra, social_media_Profiles_profiles); startService(intent); } }
activity_main.xml
Here we have a main activity layout as you can see each button has an android:onClick attribute with value of method that will be called when button pressed
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="exceptionbound.come.socialtest.MainActivity"> <Button android:id="@+id/btn1" style="@style/SocialLoginButtonStyle" android:background="@color/facebook" android:onClick="FacebookLogin" android:text="@string/facebook" /> <Button android:id="@+id/btn2" style="@style/SocialLoginButtonStyle" android:layout_below="@id/btn1" android:background="@color/twitter" android:onClick="TwitterLogin" android:text="@string/twitter" /> <Button android:id="@+id/btn3" style="@style/SocialLoginButtonStyle" android:layout_below="@id/btn2" android:background="@color/google_plus" android:onClick="GooglePlusLogin" android:text="@string/google_plus" /> <Button android:id="@+id/btn4" style="@style/SocialLoginButtonStyle" android:layout_below="@id/btn3" android:background="@color/instagram" android:onClick="InstagramLogin" android:text="@string/instagram" /> <Button android:id="@+id/btn5" style="@style/SocialLoginButtonStyle" android:layout_below="@id/btn4" android:background="@color/linkedin" android:onClick="LinkedInLogin" android:text="@string/linkedin" /> <Button android:id="@+id/btn6" style="@style/SocialLoginButtonStyle" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_below="@+id/btn5" android:background="@color/github" android:onClick="GitHubLogin" android:text="@string/github" /> <Button android:id="@+id/btn7" style="@style/SocialLoginButtonStyle" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_below="@id/btn6" android:background="@color/yahoo" android:onClick="YahooLogin" android:text="@string/yahoo" /> <Button style="@style/SocialLoginButtonStyle" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_below="@id/btn7" android:background="@color/windowsLive" android:onClick="WinLiveLogin" android:text="@string/windowslive" /> </RelativeLayout>
Social_media_Profiles
Social_media_Profiles has bunch of constants in case of enum
package exceptionbound.come.socialtest; public enum Social_media_Profiles { FACEBOOK, GOOGLE_PLUS, INSTAGRAM, TWITTER, YAHOO, WINDOWS_LIVE, LINKED_IN, GITHUB }
Detail activity
package exceptionbound.come.socialtest; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; public class Details_Activity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_details); } }
layout detail
So layout detail will show Social profile detail .
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_details" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="exceptionbound.come.socialtest.Details_Activity"> <ImageView android:id="@+id/profile_picture" android:layout_width="match_parent" android:layout_height="192dp" android:layout_centerHorizontal="true" android:scaleType="center" android:src="@mipmap/ic_launcher" /> <TextView android:id="@+id/text_identifier" style="@style/UserOtherDetailsStyle" android:layout_below="@+id/profile_picture" /> <TextView android:id="@+id/text_full_name" style="@style/UserNameStyle" android:layout_below="@+id/text_identifier" /> <TextView android:id="@+id/text_email" style="@style/UserOtherDetailsStyle" android:layout_below="@id/text_full_name" /> <TextView android:id="@+id/text_dob" style="@style/UserOtherDetailsStyle" android:layout_below="@id/text_email" /> <TextView android:id="@+id/text_description" style="@style/UserOtherDetailsStyle" android:layout_below="@id/text_dob" /> <TextView android:id="@+id/text_gender" style="@style/UserOtherDetailsStyle" /> <TextView android:id="@+id/text_locale" style="@style/UserOtherDetailsStyle" android:layout_below="@id/text_gender" /> </RelativeLayout>
Social accounts
The IntentService will carry out the login and get the information but we want to show them inner an activity! And which means our IntentService goes to construct an object with all of the information and send this object to our Details_Activity.java file. This object goes to implement the Parcelable interface which lets you transfer it from the provider to the activity. it’s only a bunch of get-set methods with a few code for making it a Parcelable.
package exceptionbound.come.socialtest; import android.os.Parcel; import android.os.Parcelable; public class Social_User_Accounts implements Parcelable { private String identifier; private String fullName; private String email; private String description; private String gender; private long day; private long month; private long year; private String pictureURL; private String locale; public String getIdentifier() { return identifier; } public void setIdentifier(String identifier) { this.identifier = identifier; } public String getFullName() { return fullName; } public void setFullName(String fullName) { this.fullName = fullName; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public long getDay() { return day; } public void setDay(long day) { this.day = day; } public long getMonth() { return month; } public void setMonth(long month) { this.month = month; } public long getYear() { return year; } public void setYear(long year) { this.year = year; } public String getPictureURL() { return pictureURL; } public void setPictureURL(String pictureURL) { this.pictureURL = pictureURL; } public String getLocale() { return locale; } public void setLocale(String locale) { this.locale = locale; } public Social_User_Accounts() { } protected Social_User_Accounts(Parcel in) { identifier = in.readString(); fullName = in.readString(); email = in.readString(); description = in.readString(); gender = in.readString(); day = in.readLong(); month = in.readLong(); year = in.readLong(); pictureURL = in.readString(); locale = in.readString(); } @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeString(identifier); dest.writeString(fullName); dest.writeString(email); dest.writeString(description); dest.writeString(gender); dest.writeLong(day); dest.writeLong(month); dest.writeLong(year); dest.writeString(pictureURL); dest.writeString(locale); } @SuppressWarnings("unused") public static final Parcelable.Creator<Social_User_Accounts> CREATOR = new Parcelable.Creator<Social_User_Accounts>() { @Override public Social_User_Accounts createFromParcel(Parcel in) { return new Social_User_Accounts(in); } @Override public Social_User_Accounts[] newArray(int size) { return new Social_User_Accounts[size]; } }; }
Cloud login
in cloud log in class, we extend it with InternetService work in the background.
package exceptionbound.come.socialtest; import android.app.IntentService; import android.content.Intent; import android.os.Bundle; import android.util.Log; import com.cloudrail.si.exceptions.AuthenticationException; import com.cloudrail.si.interfaces.Profile; import com.cloudrail.si.services.Facebook; import com.cloudrail.si.services.GitHub; import com.cloudrail.si.services.GooglePlus; import com.cloudrail.si.services.Instagram; import com.cloudrail.si.services.LinkedIn; import com.cloudrail.si.services.MicrosoftLive; import com.cloudrail.si.services.Twitter; import com.cloudrail.si.services.Yahoo; import com.cloudrail.si.types.DateOfBirth; public class Cloud_Login_Service extends IntentService { public static final String Profile_Extra = "profile"; public static final String Social_Account_Extra = "account"; private static final String TAG = "Logcat"; public Cloud_Login_Service() { super("Social Login Service"); } private Profile init(Social_media_Profiles provider) throws AuthenticationException { Profile profile = null; switch (provider) { case FACEBOOK: profile = new Facebook(this, getString(R.string.facebook_key), getString(R.string.facebook_secret)); break; case TWITTER: profile = new Twitter(this, getString(R.string.twitter_key), getString(R.string.twitter_secret)); break; case GOOGLE_PLUS: profile = new GooglePlus(this, getString(R.string.google_plus_key), getString(R.string.google_plus_secret)); break; case INSTAGRAM: profile = new Instagram(this, getString(R.string.instagram_key), getString(R.string.instagram_secret)); break; case LINKED_IN: profile = new LinkedIn(this, getString(R.string.linkedin_key), getString(R.string.linkedin_secret)); break; case YAHOO: profile = new Yahoo(this, getString(R.string.yahoo_key), getString(R.string.yahoo_secret)); break; case WINDOWS_LIVE: profile = new MicrosoftLive(this, getString(R.string.windowslive_key), getString(R.string.windows_secret)); break; case GITHUB: profile = new GitHub(this, getString(R.string.github_key), getString(R.string.github_secret)); break; } return profile; } @Override protected void onHandleIntent(Intent intent) { if (intent != null) { try { Bundle extras = intent.getExtras(); Social_media_Profiles provider = (Social_media_Profiles) extras.getSerializable(Profile_Extra); Profile profile = init(provider); Social_User_Accounts account = init(profile); Intent detailsIntent = new Intent(getApplicationContext(), Details_Activity.class); detailsIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); detailsIntent.putExtra(Social_Account_Extra, account); startActivity(detailsIntent); } catch (AuthenticationException e) { Log.e(TAG, "onHandleIntent: You cancelled", e); } } } private Social_User_Accounts init(Profile profile) { Social_User_Accounts account = new Social_User_Accounts(); account.setIdentifier(profile.getIdentifier()); account.setFullName(profile.getFullName()); account.setEmail(profile.getEmail()); account.setDescription(profile.getDescription()); DateOfBirth dob = profile.getDateOfBirth(); if (dob != null) { Long day = dob.getDay(); Long month = dob.getMonth(); Long year = dob.getYear(); if (day != null) { account.setDay(day); } if (month != null) { account.setMonth(month); } if (year != null) { account.setYear(year); } } account.setGender(profile.getGender()); account.setPictureURL(profile.getPictureURL()); account.setLocale(profile.getLocale()); return account; } }
Strings
Here is string.xml we have the bunch of Strings important thing provide your social app key and secret keys in necessary strings
<resources> <string name="app_name">social test</string> <string name="facebook">Facebook</string> <string name="twitter">twitter</string> <string name="google_plus">google+</string> <string name="instagram">Instagram</string> <string name="linkedin">linkedin</string> <string name="yahoo">Yahoo</string> <string name="windowslive">winlive</string> <string name="github">Github</string> <string name="facebook_key">here your app key</string> <string name="facebook_secret">here your app secret key</string> <string name="twitter_key">here your app key</string> <string name="twitter_secret">here your app secret key</string> <string name="google_plus_key">here your app key</string> <string name="google_plus_secret">here your app secret key</string> <string name="instagram_key">here your app key</string> <string name="instagram_secret">here your app secret key</string> <string name="linkedin_key">here your app key</string> <string name="linkedin_secret">here your app secret key</string> <string name="yahoo_key">here your app key</string> <string name="yahoo_secret">here your app secret key</string> <string name="windowslive_key">here your app key</string> <string name="windows_secret">here your app secret key</string> <string name="github_key">here your app key</string> <string name="github_secret">here your app secret key</string> </resources>
Colors
So colors.xml look like this
<?xml version="1.0" encoding="utf-8"?> <resources> <color name="colorPrimary">#3F51B5</color> <color name="colorPrimaryDark">#303F9F</color> <color name="facebook">#3b5998</color> <color name="twitter">#1da1f2</color> <color name="google_plus">#db4437</color> <color name="instagram">#fb3958</color> <color name="linkedin">#287bbc</color> <color name="yahoo">#400090</color> <color name="windowsLive">#0078d7</color> <color name="colorAccent">#FF4081</color> <color name="github">#6e5494</color> </resources>
Style
<style name="SocialLoginButtonStyle"> <item name="android:layout_width">match_parent</item> <item name="android:layout_height">wrap_content</item> <item name="android:layout_weight">1</item> <item name="android:textColor">@android:color/white</item> <item name="android:textAppearance">@style/TextAppearance.AppCompat.Button</item> </style> <style name="UserOtherDetailsStyle" parent="TextAppearance.AppCompat.Body1"> <item name="android:layout_width">match_parent</item> <item name="android:layout_height">wrap_content</item> <item name="android:layout_below">@id/text_description</item> <item name="android:gravity">center</item> <item name="android:textColor">@android:color/white</item> <item name="android:layout_marginTop">4dp</item> <item name="android:layout_marginBottom">4dp</item> </style> <style name="UserNameStyle" parent="TextAppearance.AppCompat.Headline"> <item name="android:layout_width">match_parent</item> <item name="android:layout_height">wrap_content</item> <item name="android:layout_below">@id/text_description</item> <item name="android:gravity">center</item> <item name="android:textColor">@android:color/white</item> <item name="android:layout_marginTop">8dp</item> <item name="android:layout_marginBottom">8dp</item> </style>