📜 ⬆️ ⬇️

Simple sharing with Facebook and Twitter

Now it is difficult to meet a site without the Share button (I’m not even talking about “Like”): this is a fast and convenient way to share interesting information with friends and foes through various social networks. The same “button” can be used in your Android application. It is not necessary that the application be a Facebook client: you can simply give the user the opportunity to send himself a simple message to the wall, like “I use Android Yorshik! Try you too! ” Well, this is hypothetical.
But in fact, in the last project I had to provide the user with the opportunity to poison messages (and pictures) on Facebook and Twitter about his progress, and of course, there were some pitfalls (where could they be without them). Before writing the article, I decided to isolate the part of the code responsible for sharing, in a separate component, to demonstrate working with the Facebook API and Twitter API. But then Ostap suffered, and the result was a reusable tool for simple sharing on Facebook and Twitter (and in the future, on other social networks). By “simple sharing” I mean the ability to only send a message or a picture. There is no simple “sharing” to “zafrenit” or “retweet”. Simple functionality - simple interface! I tried to hide all the difficulties of working with the API behind a beautiful facade. But more on that further ...

So, we have a task: send a message to your Facebook wall on your own behalf, update your status on Twitter. Both there and there authorization occurs using OAuth (the principles of OAuth authorization are well reflected in this scheme ). First of all I climb at the office. sites hoping to find something ready.

Facebook


Facebook was pleased with the presence of the official Android SDK, which you can take on GitHub , as well as detailed instructions for starting using this SDK in your application. I can’t say that everything in the SDK is mega-simple (some parameters cause questions), but it performs the authorization task perfectly: we just need to call
facebook.authorize(Activity activity, String[] permissions, DialogListener listener); 
and the already-ready Dialog itself will appear, will allow the user to enter their data into the web-form and eventually return us the authorization token necessary for further communication with the Facebook API. Well, then you need to have the ability to use the request (...) method in different variations. For example, in order to send a message to your wall you will have to pervert like this:
 Bundle params = new Bundle(); params.putString("message", "Android Yorshik is great!"); facebook.request("me/feed", params, "POST"); 

To understand what kind of “me / feed” is - you will have to get into the concept of Open Graph Facebook. But this is only if you want to use the full power of the available Facebook API.
The first underwater stone met here: if the Facebook client application is already installed on the device, the facebook.authorize (...) authorization request will not lead to a dialogue with WebView inside, but to launch this client application and open its login form. According to the plan, after authorization, you will return to your application as well as after the dialogue. But, if suddenly you have something slyly tied up in onResume () or onStop () you will have to take into account that they may be called once more. On one device, this form of authorization generally worked anyhow. So I would just like to be sure to always log in via Dialog. And there is such a possibility: an additional parameter Facebook.FORCE_DIALOG_AUTH will help with this. Call:
 facebook.authorize(context, permissions, Facebook.FORCE_DIALOG_AUTH, authListener); 
and authorization will consistently take place through dialogue.

Twitter


But Twitter did not endorse developers with the official SDK. But the informal abound, with different possibilities. The first thing that got into consideration:

After testing the libraries, it was decided to cross Twitter4J and the UI part of the Sugree project. By trial and error, two ways of authorization were discovered.
1) In the first case (at the moment it is used in the implemented tool) when registering your application on dev.twitter.com we leave the “Callback URL” field empty. Then after the user enters the authorization data and clicks "Authorize / Authorize" he will be transferred to the page with a PIN code, which is generally not clear where to enter. But we need it, because only by transferring it to the Twitter service we will be able to get authorization "token and secret". To torture the user once again by entering codes of this kind is a thankless task. Therefore, we take and parse the HTML page and rip out this pin-code from it. How to get the HTML code loaded in the WebView page? Not difficult at all:

That's all, removed the pin code - completed the authorization.
2) And now the correct way: in the properties of your application on dev.twitter.com in the field
“Callback URL” insert any valid URL (eg “http: //your.shik” ). And now, instead of parsing the page with the pin code, we need to catch the callback from Twitter, whose URL will contain the oauth_verifier parameter, which we will use instead of the pin code to get all the same “token and secret”. This principle has been successfully implemented in this project , and in the near future I plan to implement the same logic in myself.
')

Simple social sharing


A few words about the created project, which was called Simple Social Sharing . As already mentioned at the beginning, the main purpose of the project is to provide a simple API for simple sharing with Facebook and Twitter (in the long term and with other social networks). A simple API includes 4 (+1) methods:

Dialogues will look like this:



All that is needed for work is:

Perhaps the following FacebookFacade method will raise a question:
 publishMessage(String message, String link, String linkName, String linkDescription, String pictureUrl, Map<String, String> actions) 

The code and the illustration of the result, I think, will explain everything:
 String message = "Look at this great App!"; String link = "https://github.com/nostra13/Android-Simple-Social-Sharing"; String linkName = "Use Android Simple Social Sharing in your project!"; String linkDescription = "Also see other projects of nostra13 on GitHub!"; String pictureUrl = "http://cdn.androidcommunity.com/wp-content/uploads/2011/01/facebook-android-logo-1.jpg"; Map<String, String> actions = new HashMap<String, String>(); actions.put("Android Simple Social Sharing", "https://github.com/nostra13/Android-Simple-Social-Sharing"); facebook.publishMessage(message, link, linkName, linkDescription, pictureUrl, actions); 



I think that's all. In the nearest plans of the project - to redo the authorization to use callbacks, to completely get rid of the use of heavy Twitter4J. Well, we'll see.

As always - source on github . I hope the tool is useful to you. Applications for adding support to some other social network are also being accepted (Google+, unfortunately, has not yet provided a coherent SDK).

UPD: At the request of workers. If you think that once a client does not have a specific social network installed, then he doesn’t want to fumble into this network, then you can use the simplest method of sharing (no libraries need to be screwed):
 final Intent intent = new Intent(Intent.ACTION_SEND); intent.setType(«text/plain»); intent.putExtra(Intent.EXTRA_TEXT, "Try Android Yorshik!"); startActivity(Intent.createChooser(intent, getString(R.string.app_name))); 

Thanks NikitaG for an example of the simplest sharing.

Source: https://habr.com/ru/post/135342/


All Articles