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!
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!
Thanks a lot man.
ReplyDeleteyour welcome Anas..
Deletestringoutofboundexception is occuring and also webserviceinfo is getting null
ReplyDeletethat is because your are not getting anything in response. your response must be null. Check your request, and check you have sent proper header
Deletehow 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
ReplyDeletewhen you follow first step of fitbit developer post, you will get all thing.
Deleteyou should follow this link. https://dev.fitbit.com/apps/oauthtutorialpage
DeleteJust thing you need to do all this steps via code.
unable to generate oauth_token and oauth_signature
ReplyDeletedo u have google analytics code working
ReplyDeleteNo dear, i am sorry..
Deleteyour code works only for desktop ,not working for browser,
ReplyDeleteDear 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.
DeleteThanks.
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
ReplyDeleteMr 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.
ReplyDeleteYes of-course, but thing is that check first fitbit APIs whats they provide.
DeleteThank you! I'm going to try it out
DeleteQuick question: what are the credentials that I would need to change to make this code work for me?
ReplyDeleteYou need to do so many changes in manish's code in order to work it. Fitbit have done many changes after this example.
DeleteHow did you come up with the oauth_token & oauth_signature?
ReplyDeleteRead this https://dev.fitbit.com/apps/oauthtutorialpage. try to achieve this programatically.
DeleteHi, 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?
ReplyDeleteRegards
Yes, you can put any may be your company URL or client company URL. it show only for 2 second.
Deleteright, 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?
DeleteIts give an error on step 1: No authorization header has been provided in the request
DeleteYou 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
DeleteThis comment has been removed by the author.
ReplyDeleteit throws error "No authorization header has been provided in the request". with user_id= null .plz have a look ..
ReplyDeleteYou 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.
DeleteThis comment has been removed by the author.
ReplyDeletehi,
ReplyDeletei 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
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.
Deletehow to give header in this HTTP GET request.please reply
ReplyDeletehi
ReplyDeleteHi can you provide source code for retrieving fibit activity to android eclipse because i cant find any tutorial online
ReplyDeletePlease refer to fitbit doc.
DeleteHi
ReplyDeleteThis 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!
Diana, it is old code. May be Fitbit make changes in API. Please follow Fitbit developer for updated result.
Deletenice
ReplyDelete