Twitter is a real-time information network used to share and view photos, videos and conversations. A large chunk of its traffic comes from Twitter apps available on mobile devices such as smartphones and tablets. Mobile app developers know the immense potential and popularity of Twitter and therefore often integrate it with a wide variety of mobile applications. This blog post will walk you through the process of integrating Twitter with your Android application.
SocialAuth Android, an easy to use open source SDK, allows integration with Twitter and provides simple code to access its various functionalities, along with free support. It is one of the few SDKs available today which can help you integrate other social networks in your Android App with Twitter.
It lets you share status updates, get user profile, user contacts and user feeds. You can upload images with your friends using this API easily. One of the key features of this API is its ability to create Twitter albums by grouping images posted on your account.
Getting Started
First you need to register your application with Twitter, and get the API keys and secret keys. The following link will help you in this regard: https://code.google.com/p/socialauth-android/wiki/Twitter
Integrate SDK in App
Now we can start integrating the SDK, which can be downloaded from http://code.google.com/p/socialauth-android. This SDK contains the Java libraries that do the heavy lifting of OAuth as well as the REST calls for each social provider. Extract the contents and we are all set.
That is all you need to get started. The SDK .zip file also contains code samples which can help you go ahead with the coding.
Implementation
You just need to click the button and it will open a Twitter login dialog box. After user authorization is completed you can get options to various functionalities.
Initialization
Initialize the SocialAuth Adapter Object and create a Twitter Button. On clicking the button give the required authorization to the adapter.
public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); adapter = new SocialAuthAdapter(new ResponseListener()); twitter_button = (Button)findViewById(R.id.fb_btn); twitter_button.setBackgroundResource(R.drawable.twitter); twitter_button.setOnClickListener(new OnClickListener() { public void onClick(View v) { adapter.authorize(ProviderUI.this, Provider.TWITTER); } }); }
Receive Authentication response in ResponseListener and initiate calls to get various functionalities. See Examples below:
Update Status
Share your status by calling adapter.updateStatus() and verify response in MessageListener. You can share your status on your Twitter account in any language.
private final class ResponseListener implements DialogListener { public void onComplete(Bundle values) { adapter.updateStatus(edit.getText().toString(), new MessageListener()); } } // To get status of message after authentication private final class MessageListener implements SocialAuthListener { @Override public void onExecute(Integer t) { Integer status = t; if (status.intValue() == 200 || status.intValue() == 201 ||status.intValue() == 204) Toast.makeText(CustomUI.this, "Message posted",Toast.LENGTH_LONG).show(); else Toast.makeText(CustomUI.this, "Message not posted",Toast.LENGTH_LONG).show(); } }
Get Profile
Get User profile by getUserprofileAsync() method and receive response in profileDataListener. You can get
User profile ID, screen name, full name, language, location and profile image URL for Twitter.
private final class ResponseListener implements DialogListener { public void onComplete(Bundle values) { adapter.getUserProfileAsync(new ProfileDataListener()); } } // To receive the profile response after authentication private final class ProfileDataListener implements SocialAuthListener { @Override public void onExecute(Profile t) { Log.d("Custom-UI", "Receiving Data"); Profile profileMap = t; Log.d("Custom-UI", "Validate ID = " + profileMap.getValidatedId()); Log.d("Custom-UI", "First Name = " + profileMap.getFullName()); Log.d("Custom-UI", "Language = " + profileMap.getLanguage()); Log.d("Custom-UI", "Location = " + profileMap.getLocation()); Log.d("Custom-UI", "Profile Image URL = " + profileMap.getProfileImageURL()); }
Get Contacts
Get User contacts by getContactListAsync() method and receive response in contactDataListener. You can get
display name and profile URL for Twitter.
private final class ResponseListener implements DialogListener { public void onComplete(Bundle values) { adapter.getContactListAsync(new ContactDataListener()); } } // To receive the contacts response after authentication private final class ContactDataListener implements SocialAuthListener> { @Override public void onExecute(List t) { Log.d("Custom-UI", "Receiving Data"); List contactsList = t; if (contactsList != null && contactsList.size() > 0) { for (Contact c : contactsList) { Log.d("Custom-UI", "Contact ID = " + c.getId()); Log.d("Custom-UI", "Display Name = " + c.getDisplayName()); } }
private final class ResponseListener implements DialogListener { public void onComplete(Bundle values) { adapter.getFeedsAsync(new FeedDataListener()); } } // To receive the feed response after authentication private final class FeedDataListener implements SocialAuthListener> { @Override public void onExecute(List t) { Log.d("Share-Bar", "Receiving Data"); List feedList = t; if (feedList != null && feedList.size() > 0) { for (Feed f : feedList) { Log.d("Custom-UI ", "Feed ID = " + f.getId()); Log.d("Custom-UI", "Screen Name = " + f.getScreenName()); Log.d("Custom-UI", "Message = " + f.getMessage()); Log.d("Custom-UI ", "Get From = " + f.getFrom()); Log.d("Custom-UI ", "Created at = " + f.getCreatedAt()); } } }
Upload Images
Share your images by calling adapter.uploadimageAsync() and verify response in UploadImageListener.
private final class ResponseListener implements DialogListener { public void onComplete(Bundle values) { adapter.uploadImageAsync("Landscape Images", "icon.png", bitmap, 0,new UploadImageListener()); } } // To get status of image upload after authentication private final class UploadImageListener implements SocialAuthListener { @Override public void onExecute(Integer t) { Log.d("Custom-UI", "Uploading Data"); Integer status = t; Log.d("Custom-UI", String.valueOf(status)); Toast.makeText(CustomUI.this, "Image Uploaded", Toast.LENGTH_SHORT).show(); } }
Get Albums
As you may be aware, Twitter does not provide album functionality, which is a common feature across other social networking platforms. We have developed a custom functionality to show user albums by grouping all images posted by specific friends in one album. The friend name is what gets reflected as the album name. Get user albums by getAlbumsAsync() method and receive response in albumDataListener. You can get album name, cover photo, photos count, and array of images with their name and URLs.
private final class ResponseListener implements DialogListener { public void onComplete(Bundle values) { adapter.getAlbumsAsync(new AlbumDataListener()); } } // To receive the album response after authentication private final class AlbumDataListener implements SocialAuthListener> { @Override public void onExecute(List t) { Log.d("Custom-UI", "Receiving Data"); List albumList = t; if (albumList != null && albumList.size() > 0) { // Get Photos inside Album for (Album a : albumList) { Log.d("Custom-UI", "Album ID = " + a.getId()); Log.d("Custom-UI", "Album Name = " + a.getName()); Log.d("Custom-UI", "Cover Photo = " + a.getCoverPhoto()); Log.d("Custom-UI", "Photos Count = " + a.getPhotosCount()); photosList = a.getPhotos(); if (photosList != null && photosList.size() > 0) { for (Photo p : photosList) { Log.d("Custom-UI", "Photo ID = " + p.getId()); Log.d("Custom-UI", "Name = " + p.getTitle()); Log.d("Custom-UI", "Thumb Image = " + p.getThumbImage()); Log.d("Custom-UI", "Small Image = " + p.getSmallImage()); Log.d("Custom-UI", "Medium Image = " + p.getMediumImage()); Log.d("Custom-UI", "Large Image = " + p.getLargeImage()); } } } }
Conclusion
That’s the overview of how to download and install SocialAuth Twitter to easily integrate Twitter with your Android applications, and it concludes our three-part series on how to set up SocialAuth. To download the SocialAuth Twitter SDK, view further instructions and examples, or get support, please visit: http://code.google.com/p/socialauth-android.
Wow……great code…..gud one
Well. I tried this with ‘ProfileDataListener’ and it went smooth. Users get authenticated successfully and data are being retrieved. BUT… I m getting only ‘name’ and ‘id’ in JSON response from facebook api. And because of that i’m getting ‘JSONException: No value for locale’ and eventually it is leading to a ‘SocialAuthError: Profile Details not Received’
Can you please help me over this ?
profile.getEmail() is ‘null’
(P.S. I have request email id in my developer.twitter account and I’m getting email id properly in my website with same developer key and secret).
Any solution ?
Hi sir,
Using social Auth files.When we run on genemotion i get SSLHandshake Exception.Plus can we get mobile number from twitter or facebook using same function getProfileAsynch().
If you want to integrate the Twitter API in the android then it will be very much helpful for you. Reading the post has helped me a lot in understanding the integration process.