Giter VIP home page Giter VIP logo

rxsocialconnect-android's Introduction

Android Arsenal

OAuth RxJava extension for Android. iOS version is located at this repository.

RxSocialConnect

RxSocialConnect simplifies the process of retrieving authorizations tokens from multiple social networks to a minimalist observable call, from any Fragment or Activity.

OAuth20Service facebookService = //...

RxSocialConnect.with(fragmentOrActivity, facebookService)
                    .subscribe(response -> response.targetUI().showResponse(response.token()));

Features:

  • Webview implementation to handle the sequent steps of oauth process.
  • Storage tokens locally.
  • Automatic refreshing tokens taking care of expiration date.
  • I/O operations performed on secondary threads and automatic sync with user interface on the main thread, thanks to RxAndroid
  • Mayor social network supported, more than 16 providers; including Facebook, Twitter, GooglePlus, LinkedIn and so on. Indeed, it supports as many providers as ScribeJava does, because RxSocialConnect is a reactive-android wrapper around it.
  • Honors the observable chain. RxOnActivityResult allows RxSocialConnect to transform every oauth process into an observable for a wonderful chaining process.

Setup

Add the JitPack repository in your build.gradle (top level module):

allprojects {
    repositories {
        jcenter()
        maven { url "https://jitpack.io" }
    }
}

And add next dependencies in the build.gradle of android app module:

dependencies {
    compile "com.github.FuckBoilerplate:RxSocialConnect-Android:0.0.5"
    compile "io.reactivex:rxjava:1.1.5"
}

Usage

Because RxSocialConnect uses RxActivityResult to deal with intent calls, all its requirements and features are inherited too.

Before attempting to use RxSocialConnect, you need to call RxSocialConnect.register in your Android Application class, supplying as parameter the current instance.

public class SampleApp extends Application {

    @Override public void onCreate() {
        super.onCreate();
        RxSocialConnect.register(this);
    }
}

Every feature RxSocialConnect exposes can be accessed from both, an activity or a fragment instance.

Limitation:: Your fragments need to extend from android.support.v4.app.Fragment instead of android.app.Fragment, otherwise they won't be notified.

The generic type of the observable returned by RxSocialConnect when subscribing to any of its providers is always an instance of Response class.

This instance holds a reference to the current Activity/Fragment, accessible calling targetUI() method. Because the original one may be recreated it would be unsafe calling it. Instead, you must call any method/variable of your Activity/Fragment from this instance encapsulated in the response instance.

Also, this instance holds a reference to the token.

Retrieving tokens using OAuth1.

On social networks which use OAuth1 protocol to authenticate users (such us Twitter), you need to build a OAuth10aService instance and pass it to RxSocialConnect.

OAuth10aService twitterService = new ServiceBuilder()
                .apiKey(consumerKey)
                .apiSecret(consumerSecret)
                .callback(callbackUrl)
                .build(TwitterApi.instance());
                
RxSocialConnect.with(fragmentOrActivity, twitterService)
                    .subscribe(response -> {
                        OAuth1AccessToken token = response.token();
                        response.targetUI().showToken(token.getToken());
                        response.targetUI().showToken(token.getTokenSecret());
                    });

Once the OAuth1 process has been successfully completed, you can retrieve the cached token calling RxSocialConnect.getTokenOAuth1(defaultApi10aClass) -where defaultApi10aClass is the provider class used on the oauth1 process.

        RxSocialConnect.getTokenOAuth1(TwitterApi.class)
                .subscribe(token -> showResponse(token),
                        error -> showError(error)); 

Retrieving tokens using OAuth2.

On social networks which use OAuth2 protocol to authenticate users (such us Facebook, Google+ or LinkedIn), you need to build a OAuth20Service instance and pass it to RxSocialConnect.

OAuth20Service facebookService = new ServiceBuilder()
                .apiKey(appId)
                .apiSecret(appSecret)
                .callback(callbackUrl)
                .scope("public_profile")
                .build(FacebookApi.instance());
                
RxSocialConnect.with(fragmentOrActivity, facebookService)
                    .subscribe(response -> {
                        OAuth2AccessToken token = response.token();
                        response.targetUI().showToken(token.getAccessToken());
                    });

Once the OAuth2 process has been successfully completed, you can retrieve the cached token calling RxSocialConnect.getTokenOAuth2(defaultApi20Class) -where defaultApi20Class is the provider class used on the oauth2 process.

        RxSocialConnect.getTokenOAuth2(FacebookApi.class)
                .subscribe(token -> showResponse(token),
                        error -> showError(error)); 

Token lifetime.

After retrieving the token, RxSocialConnect will save it on disk to return it on future calls without doing again the oauth process. This token only will be evicted from cache if it is a OAuth2AccessToken instance and its expiration time has been fulfilled.

But, if you need to close an specific connection (or delete the token from the disk for that matters), you can call RxSocialConnect.closeConnection(baseApiClass) at any time to evict the cached token -where baseApiClass is the provider class used on the oauth process.

//Facebook
RxSocialConnect.closeConnection(FacebookApi.class)
                .subscribe(_I ->  showToast("Facebook disconnected"));

//Twitter
RxSocialConnect.closeConnection(TwitterApi.class)
                .subscribe(_I ->  showToast("Twitter disconnected"));

You can also close all the connections at once, calling RxSocialConnect.closeConnections()

RxSocialConnect.closeConnections()
                .subscribe(_I ->  showToast("All disconnected"));

Examples

There are several examples of social networks connections in the android app module.

Credits

Author

Víctor Albertos

Another author's libraries using RxJava:

  • RxCache: Reactive caching library for Android and Java.
  • RxGcm: RxJava extension for Gcm which acts as an architectural approach to easily satisfy the requirements of an android app when dealing with push notifications.
  • RxPaparazzo: RxJava extension for Android to take images using camera and gallery.
  • RxActivityResult: A reactive-tiny-badass-vindictive library to break with the OnActivityResult implementation as it breaks the observables chain.

rxsocialconnect-android's People

Contributors

victoralbertos avatar miguelbcr avatar

Watchers

James Cloos avatar Arjun avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.