Thursday, November 14, 2013

Fitbit Integration in Android | Login with Fitbit

Hello friends!

on many demand today i am sharing fitbit account integration in android application.

REF-

http://dev.fitbit.com/

https://github.com/manishsri01/FitbitIntegration

NOTE: please create your application on fitbit and replace with yours credential. Don't use mine in your application.



For this i created a MainActivity.java and put a button for login with fitbit account and open a web-view in next page. see below code hope it will help you-

MainActivity.java-


package com.manish.fitbitintegration;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

Button btnLogin;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

btnLogin = (Button) findViewById(R.id.button1);

btnLogin.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this,
FitBitActivity.class);
startActivity(intent);
}
});
}

}

And on FitBitActivity.java class-


private void login() {

try {

HttpResponse response = null;
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, 20000);
HttpConnectionParams.setSoTimeout(httpParameters, 20000);
HttpClient client = new DefaultHttpClient(httpParameters);

HttpGet request = new HttpGet(
"http://api.fitbit.com/oauth/request_token?oauth_consumer_key=7af733f021f649bcac32f6f7a4fe2e9a&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1358921319&oauth_nonce=456236281&oauth_callback=http://androidhub4you.com/&oauth_version=1.0&oauth_token=5cefb18d2a80073520211f03f8d75321&oauth_signature=QdVUzMvT6tveGyoPu%2BEevzvo07s%3D");
response = client.execute(request);

BufferedReader rd = new BufferedReader(new InputStreamReader(
response.getEntity().getContent()));

String webServiceInfo = "";
while ((webServiceInfo = rd.readLine()) != null) {
Log.e("****Step 1***", "Webservice: " + webServiceInfo);
String result[]=webServiceInfo.split("=");
String result2 = result[1];
String result3[]=result2.split("&");
authToken = result3[0];
Log.e("Auth token:", "Webservice: " + authToken);

}

} catch (Exception e) {http://google.com/
// TODO: handle exception
e.printStackTrace();
}

}


DOWNLOAD FULL CODE HERE

Thanks!

20 comments :

  1. stringoutofboundexception is occuring and also webserviceinfo is getting null

    ReplyDelete
  2. how did you create the base string,i am only getting consumer key after creating app on dev.fitbit.com what about oauth_token and oauth_signature

    ReplyDelete
    Replies
    1. when you follow first step of fitbit developer post, you will get all thing.

      Delete
  3. unable to generate oauth_token and oauth_signature

    ReplyDelete
  4. do u have google analytics code working

    ReplyDelete
  5. your code works only for desktop ,not working for browser,

    ReplyDelete
    Replies
    1. Dear i am android mobile developer and all of my post on android only. So where here desktop and browser? It is working code . Please see screen shot.
      Thanks.

      Delete
  6. onclick to fitbit_login button its moving to fitbitactivity class where fitbitlogin page is displaying but after i enter my details and press allow its moving to the main activity page i.e the first page,however its fetching everything,oauth_token ,signature etc,no error is coming but not moving to the fitbit profile

    ReplyDelete
  7. Mr Manish, first of all thanks for sharing your code. Secondly, I have a quick question: with this code, would I be able to access any information from my Fitbit account? I'm interested in particular information to use in my Android app.

    ReplyDelete
    Replies
    1. Yes of-course, but thing is that check first fitbit APIs whats they provide.

      Delete
    2. Thank you! I'm going to try it out

      Delete
  8. Quick question: what are the credentials that I would need to change to make this code work for me?

    ReplyDelete
  9. How did you come up with the oauth_token & oauth_signature?

    ReplyDelete
  10. Hi, one question. In the first fitbit steps to register an app it requires a callback url, so what is that? How can i use if i have to use it?
    Regards

    ReplyDelete
    Replies
    1. Yes, you can put any may be your company URL or client company URL. it show only for 2 second.

      Delete
    2. right, i'm trying to run your example with my credentials, but where i have to put them? in a url on login method... for callback url what i have to build to have the back responses then?

      Delete
    3. Its give an error on step 1: No authorization header has been provided in the request

      Delete