Cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Android oauth 2 httpUrlConnection

ANSWERED
I can not figure out this problem.
I have tried to make a http request for getting the acess token
but always fitbit say me this :
{"errors":[{"errorType":"invalid_request","message":"Authorization header required. Visit https://dev.fitbit.com/docs/oauth2 for more information on the Fitbit Web API authorization process."}],"success":false}
I have used the following code to make the http request : 
URL url = new URL("https://api.fitbit.com/oauth2/token");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Authorization", " Basic REDACTED");
conn.setRequestProperty( "Content-type", "application/x-www-form-urlencoded");
conn.setRequestProperty( "Accept", "*/*" );
OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());

writer.write("client_id=my_client_id&scope=activity&code=my_code_recived");
writer.flush();
String line;
BufferedReader reader = new BufferedReader(new
InputStreamReader(conn.getInputStream()));
while ((line = reader.readLine()) != null) {
Log.i("operationOut",line);
}
writer.close();
reader.close();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;


Can someone help me to resolve this problem?
I am sorry if my english is no perfect
PS:
it is for an android app


Moderator edit: I removed the value of your Authorization header, as you publicly posted your client secret. Please go to https://dev.fitbit.com/apps/ and reset your client secret by clicking the "Reset Consumer Key/Secret" button on your app's settings.

Best Answer
0 Votes
1 BEST ANSWER

Accepted Solutions

Dear JeremiahFitbit,
I have finally found the solution at my problem.
The problem was about the class HttpURLConnection of java that did not add properly the property "Authentication" so I changed it using the class HttpClient and it work successfully in my case with Android Lollipop (5.0.2).
Thank you very much for your attention !

View best answer in original post

Best Answer
7 REPLIES 7

I'm not familiar with Android development. Can you provide a capture of the actual HTTP request? You might use a tool like Runscope Traffic Inspector. Be sure to not publicly post your actual client credentials.

Best Answer
0 Votes

Dear JeremiahFitbit,
I have finally found the solution at my problem.
The problem was about the class HttpURLConnection of java that did not add properly the property "Authentication" so I changed it using the class HttpClient and it work successfully in my case with Android Lollipop (5.0.2).
Thank you very much for your attention !

Best Answer

Hi Liam001,

 

Could you share android code?

And It will help many android-devs

Best Answer

Yeah for sure

Can you tell me exactly what you need?

 

PS: 

Sorry for my bad English

Best Answer
0 Votes

Hey Liam001, could you please share the android java code? I need it for my school project.

Best Answer
0 Votes

Hi,

here are the steps I followed to get user data by using the Fitbit web API:

1) Use Chrome Customs Tabs: https://developer.chrome.com/multidevice/android/customtabs 

2) It's very important to use a intent-filter so after using google custom tab you can redirect to your app with the token

3) Use a generic http request for android as : http://hc.apache.org/httpclient-3.x/tutorial.html

 

There is no a specific code but an entire workflow in order to make it work.

Best Answer
0 Votes

Hi Liam,

 

I am also looking at OAuth2 with FitBit on Android. So far, I have tried with Spring Authentication and Scribe Library. I see you have just used HTTP Requests. 

 

Have you implemented the Authorization Code Grant Flow? 

 

If correct, this takes 4 HTTP requests:

- Authorization Code

- Access Token 

- Refresh Token 

- Revoke Token 

 

Can you share your code for Chrome Custom Tabs please? 

This would be very helpful.. 

 

Thanks and good luck!

 

Niels

Best Answer
0 Votes