Thursday, November 14, 2013

Fitbit Integration in Android | Login with Fitbit

Hello friends!

on many demands 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=your_key&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1358921319&oauth_nonce=456236281&oauth_callback=http://androidhub4you.com/&oauth_version=1.0&oauth_token=your_token&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!

37 comments:

  1. stringoutofboundexception is occuring and also webserviceinfo is getting null

    ReplyDelete
    Replies
    1. that is because your are not getting anything in response. your response must be null. Check your request, and check you have sent proper header

      Delete
  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
    2. you should follow this link. https://dev.fitbit.com/apps/oauthtutorialpage
      Just thing you need to do all this steps via code.

      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
    Replies
    1. You need to do so many changes in manish's code in order to work it. Fitbit have done many changes after this example.

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

    ReplyDelete
    Replies
    1. Read this https://dev.fitbit.com/apps/oauthtutorialpage. try to achieve this programatically.

      Delete
  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
    4. You need to provide header information in request which contains details which you are providing in query string. Read this https://dev.fitbit.com/apps/oauthtutorialpage

      Delete
  11. This comment has been removed by the author.

    ReplyDelete
  12. it throws error "No authorization header has been provided in the request". with user_id= null .plz have a look ..

    ReplyDelete
    Replies
    1. You need to provide header information in request which contains details which you are providing in query string. Read this https://dev.fitbit.com/apps/oauthtutorialpage. You need to provide access token and other details in header in order to use user data.

      Delete
  13. This comment has been removed by the author.

    ReplyDelete
  14. hi,
    i am getting this error message . Please let me know how can i resolve this error.
    uh-oh...
    This page is no longer valid.it looks like you provided an invalid token or someone already used this token you provided.please return to the site or application which sent you to this page and try again.

    Thanks
    Pankaj

    ReplyDelete
    Replies
    1. I think you might be getting this error after approve, you are pressing back which is not a valid URL. or you have not provided call back URL. Well, I am not sure either.

      Delete
  15. how to give header in this HTTP GET request.please reply

    ReplyDelete
  16. Hi can you provide source code for retrieving fibit activity to android eclipse because i cant find any tutorial online

    ReplyDelete
  17. Hi
    This may sound like a stupid question but isn't the timestamp going to change? does it work if we hardcode it? To be honest I get the following error when running: "This page is no longer valid. It looks like you provided an invalid token..." but I'm pretty sure all of my other tokens are right
    Thank you!

    ReplyDelete
    Replies
    1. Diana, it is old code. May be Fitbit make changes in API. Please follow Fitbit developer for updated result.

      Delete