Quick Solution for Adding and Changing Twitter Profile Picture From Android App
OVERVIEW
One of our clients sent us a request to deliver Facebook and Twitter integration for both iOS and Android platforms. Facebook and Twitter change their API very often and due to the latest changes, it was not obvious how to do that quickly on Android platform, and there was no well-structured documentation on Twitter API for adding and changing Twitter Profile picture from 3rd party Android app.
iOS has an easy option to add or change user profile icon via Twitter API under native SDK. For Android, here is what we have under Twitter API docs https://docs.fabric.io/android/twitter/access-rest-api.html#extensions and there is no clear code example with POST request for REST API.
Twitter changed REST API (previously it was written for Eclipse IDE) and currently under Android Studio IDE it is working with Fabric, but steps and code for integration Fabric for quick login were not described in full and it appeared that there was no easy and quick way to do that.
Here we are glad to describe Steps On How to Change User Avatar/on User Profile from mobile app (Android).
And we did it. Here I am happy to share more details on how we resolved that (in code).
SOLUTION
For proper Twitter integration we have to start from the following steps
1. Integrate Fabric into Android Studio
2. Register App under Fabric
3. Receive Consumer Key (API Key) and Consumer Secret (API Secret)
4. Under project you should add the following:
1 2 |
private static final String TWITTER_KEY = “Your_twitter_key”; private static final String TWITTER_SECRET = “Your_twitter_secret”; |
5. Create Config
1 2 |
TwitterAuthConfig authConfig = new TwitterAuthConfig(TWITTER_KEY, TWITTER_SECRET); Fabric.with(this, new Twitter(authConfig)); |
6. Add Twitter login button under XML file
1 2 3 4 |
<com.twitter.sdk.android.core.identity.TwitterLoginButton android:id="@+id/twitter_login_button" android:layout_width="wrap_content" android:layout_height="wrap_content" /> |
7. Find Twitter login button under class
1 |
loginButton = (TwitterLoginButton) view.findViewById(R.id.twitter_login_button); |
8. Add Callback to Twitter button
1 2 3 4 5 6 7 8 9 10 11 12 13 |
loginButton.setCallback(new Callback<TwitterSession>() { @Override public void success(Result<TwitterSession> result) { // The TwitterSession is also available through: // Twitter.getInstance().core.getSessionManager().getActiveSession() session = result.data; } @Override public void failure(TwitterException exception) { Log.d("TwitterKit", "Login with Twitter failure", exception); } }); |
9. Create class to upload User picture
Please check the following link for the reference: http://square.github.io/retrofit/.
Here the 1st parameter for sendPhoto method is User ID, the 2d parameter is picture itself, base64. Tips and Tricks here: we would highly recommend compressing image, thus allowing to minimize time that query might take. Also this step also helps us to avoid getting Twitter warning regarding image size.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
public class MyTwitterApiClient extends TwitterApiClient{ public MyTwitterApiClient(TwitterSession session){ super(session); } public CustomService getCustomService(){ return getService(CustomService.class); } } interface CustomService { @POST("/1.1/account/update_profile_image.json") void sendPhoto(@Query("user_id") long id, @Query("image") String image, Callback<Response> cb); } |
10. Class received under Step 8 (described above) we transfer into our new class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
MyTwitterApiClient apiClients = new MyTwitterApiClient(session); apiClients.getCustomService().sendPhoto(userId, photoBase64, new Callback<Response>() { @Override public void success(Result<Response> result) { if (result.data.getStatus() == 200) { Toast.makeText(activityLogin, R.string.changed_twitter_photo, Toast.LENGTH_SHORT).show(); } } @Override public void failure(TwitterException e) { Log.v(TAG, "TwitterException response -->" + e); } }); |
11. Ta-Daaah, you are welcome to run project and check results
SUCCESS! From now your app can add and change User Profile picture under Twitter successfully.
We will be happy to send you Android demo video under request.
Source code. Source code is available from here Github
If you have questions please contact us