05-19-2021 05:45
05-19-2021 05:45
I have been trying to get fibit user data but I got stuck in the middle.
I will explain my problem in detail.
First I got my details OAuth 2.0 Client ID,
Client Secret,
Redirect URI:http://localhost:8043/login
when my application redirected to fitbit url, user could able to successful get authorized,authenticated and redirected to my application redirect uri i.e http://localhost:8043/login.
my url after successful authorization looks like below:
http://localhost:8080/login#access_token={user_access_token}&
user_id{user_id}&scope=heartrate+nutrition+location+profile+social+sleep+weight+activity+settings&token_type=Bearer&expires_in=604481
using the above url, how can i get user information?
here is my code
@GetMapping(value="/login")
public String data1(HttpServletRequest request,@RequestParam(value="access_token", required=false) List<String> access_token,@RequestParam(value="user_id", required=false) List<String> user_id,
@RequestParam(value="scope", required=false) List<String> scope,@RequestParam(value="token_type", required=false) List<String> token_type,
@RequestParam(value="expires_in", required=false) List<String> expires_in
) throws IOException {
//System.out.println(usr);
String url="https://api.fitbit.com/1/user/-/profile.json";
String body= "#access_token="+access_token+"&"+
"user_id="+user_id+"&scope=+heartrate+nutrition+location+profile+social+sleep+weight+activity+settings&token_type=Bearer&expires_in=604481";
OkHttpClient client = new OkHttpClient();
MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded");
RequestBody body1 = RequestBody.create(mediaType, body);
Request request1 = new Request.Builder()
.url(url)
.post(body1)
.addHeader("content-type", "application/x-www-form-urlencoded")
.addHeader("cache-control", "no-cache")
.build();
Response response = client.newCall(request1).execute();
return "got the data";
}
The above code is the way I have tried, but I couldnt succed because url has # parameter hence I could access the values in url.
is there anotherway to solve my problem using springboot?
if possible pls provided sample code using springboot.
Thank you
05-19-2021 09:19
05-19-2021 09:19
It looks like your application is receiving the access token in the redirect URL. You'll need to extract the access token and use to when calling the other endpoints to receive the user data. For example
GET https://api.fitbit.com/1/user/[user-id]/activities/date/[date].json
Authorization: Bearer <access_token>
05-19-2021 10:36
05-19-2021 10:36
thank you, Can I get code for extracting access token and how to call other endpoints using springboot
05-19-2021 14:15
05-19-2021 14:15
We don't have code samples for springboot. I'd recommend some internet searches or maybe one of the community developers has an example to share.