Fitbit.com will redirect to your server.
This is a code for the server.
I'll store access_token and refresh_token.
@RequestMapping(value="/restTest", method=RequestMethod.GET) public String restTest(@RequestParam(value="code", required=false) String code) { StringBuilder sb = new StringBuilder(); if(code!=null) { HttpClient httpclient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(REST_URL_TO_GET_TOKEN);
httpPost.setHeader("Authorization",AUTH_APPID_APPSECRET);
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
nvps.add(new BasicNameValuePair("client_id", CLIENT_ID));
nvps.add(new BasicNameValuePair("grant_type", "authorization_code"));
nvps.add(new BasicNameValuePair("redirect_uri", REDIRECT_URL));
nvps.add(new BasicNameValuePair("code", code)); StringBuilder contentSb = new StringBuilder();
try { httpPost.setEntity(new UrlEncodedFormEntity(nvps));
HttpResponse response = httpclient.execute(httpPost);
HttpEntity resEntity = response.getEntity(); if (resEntity != null) { BufferedReader rd = new BufferedReader(new InputStreamReader( response.getEntity().getContent()));
String line = null;
while ((line = rd.readLine()) != null) { contentSb.append(line);
} } httpPost.abort();
} catch (Exception e) { sb.append("Exception::" + e.getStackTrace().toString());
} sb.append(contentSb.toString()); } //if(code!=null)
return sb.toString();
}
Authorization Header Example
This example assumes that the
code
URI parameter value in the callback URI was 1234567890
.POST https://api.fitbit.com/oauth2/token
Authorization: Basic Y2xpZW50X2lkOmNsaWVudCBzZWNyZXQ=
Content-Type: application/x-www-form-urlencoded
client_id=22942C&grant_type=authorization_code&redirect_uri=http%3A%2F%2Fexample.com%2Fcallback&code=1234567890
Example Response:
{
"access_token": "eyJhbGciOiJIUzI1NiJ9.eyJleHAiOjE0MzAzNDM3MzUsInNjb3BlcyI6Indwcm8gd2xvYyB3bnV0IHdzbGUgd3NldCB3aHIgd3dlaSB3YWN0IHdzb2MiLCJzdWIiOiJBQkNERUYiLCJhdWQiOiJJSktMTU4iLCJpc3MiOiJGaXRiaXQiLCJ0eXAiOiJhY2Nlc3NfdG9rZW4iLCJpYXQiOjE0MzAzNDAxMzV9.z0VHrIEzjsBnjiNMBey6wtu26yHTnSWz_qlqoEpUlpc",
"expires_in": 3600,
"refresh_token": "c643a63c072f0f05478e9d18b991db80ef6061e4f8e6c822d83fed53e5fafdd7",
"token_type": "Bearer",
"user_id": "26FWFL"
}
댓글 없음:
댓글 쓰기