Skip to content

A library that simplifies Fitbit authorization

Notifications You must be signed in to change notification settings

TemMax/FitbitAuth

Repository files navigation

FitbitAuth

Download Hits-of-Code API

FitbitAuth library that simplifies Fitbit authorization

Setup

Add to your module's build.gradle dependencies

// Update top level build.gradle
allprojects {
  repositories {
    jcenter()
  }
}
// Add this to app level build.gradle (or module)
implementation 'com.duglasher.fitbit:auth:1.0.2'

Setup fitbit credentials

1. Register a Fitbit Dev Account

Go to https://dev.fitbit.com/apps and create and account. You'll need to use a valid email address and confirm it before continuing.

2. Create a Fitbit App

  1. Open https://dev.fitbit.com/apps/new
  2. Fill out the fields about your app. The first five fields (app name, description, website, organization, and organization website) will be specific to your organization and will be what users see in the app when giving permissions.

Important: For OAuth 2.0 Application Type select Client For Callback URL choose a non-url word starting with https://. We recommend https://finished

  1. Agree and click Register. You should get a screen with your Client Id and Client Secret. Copy these and the callback url—we will need these later.

3. Add credentials to project

In your local.properties file in project write lines like below:

fitbit.client_id=YOUR_CLIENT_ID
fitbit.client_secret=YOUR_CLIENT_SECRET
fitbit.redirect_url=YOUR_CALLBACK_URL

Using library

Create instance of FitbitAuthManager in your Application class in onCreate():
You can specify required scopes and optional scopes with addRequiredScopes and addOptionalScopes in AuthorizationConfiguration

class App : Application() {

	override fun onCreate() {
		super.onCreate()
		fitbitAuth = FitbitAuthManager(
			this,
			AuthorizationConfiguration.Builder()
				.setCredentials(
					BuildConfig.CLIENT_ID,
					BuildConfig.CLIENT_SECRET,
					BuildConfig.REDIRECT_URL
				)
				.addRequiredScopes(Scope.nutrition, Scope.profile)
				.setExpiresIn(AuthorizationConfiguration.ExpiresIn.WEEK)
				.build()
		)
	}

	companion object {
		lateinit var fitbitAuth: FitbitAuthManager
			private set
	}

}

Implement FitbitAuthHandler interface in your activity :

class MainActivity : AppCompatActivity(), FitbitAuthHandler {
     // other code
    
     override fun onAuthResult(authResult: FitbitAuthResult) {
        when (authResult) {
            FitbitAuthResult.Success      -> {
                Log.d(
                    "FitbitAuth",
                    "Login success! Scopes: ${fitBitAuth.getAccessToken().scopes.joinToString()}"
                )
            }
            is FitbitAuthResult.Error     -> {
                Log.d(
                    "FitbitAuth",
                    "Login error!\nErrors: ${authResult.errors.map(ErrorFitbitResponse.Error::errorType).joinToString()}"
                )
            }
            is FitbitAuthResult.Exception -> {
                Log.d("FitbitAuth", "Login exception!")
                authResult.exception.printStackTrace()
            }
        }
        
    }
}

Next override onNewIntent() function in your activity, where you will use authentication and pass intent to the FitbitAuthManager#handleIntent:

Note, that FitbitAuthManager#handleIntent parameter activity must implements LifecycleOwner and FitbitAuthHandler.

override fun onNewIntent(intent: Intent?) {
    super.onNewIntent(intent)
        if (intent != null) {
            App.fitBitAuth.handleIntent(this, intent)
        }
    }

And now let's authorize! Call FitbitAuthManager#login:

App.fitBitAuth.login(this)



Special thanks to Miha-x64 for review.

About

A library that simplifies Fitbit authorization

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages