10-06-2015 05:54 - edited 10-06-2015 06:10
10-06-2015 05:54 - edited 10-06-2015 06:10
When I pass a invalid refresh token I'm not getting the message which I'm supposed to get which is :
{"errors":[{"errorType":"oauth","fieldName":"refresh_token","message":"Refresh token invalid: XXXXX"}],"success":false}
instead my program crasahes at the line where I make the request. Can someone look into my code and tell me What I'm doing wrong. When I pass a correct refresh token everything is working fine.
Thank you.
Here's my code:
public static String getAccessTokenFromRefreshToken(int accountId, Connection connection, int accountDeviceId, String refreshToken, String accessToken) throws Exception {
String newAccessToken = null;
OAuthJSONAccessTokenResponse oAuthResponse = null;
try {
OAuthClientRequest request = OAuthClientRequest
.tokenLocation(FITBIT_TOKEN_URI)
.setGrantType(GrantType.REFRESH_TOKEN)
.setRefreshToken(refreshToken).buildBodyMessage();
Map<String, String> headers = new HashMap<String, String>();
headers.put(OAuth.HeaderType.AUTHORIZATION, "Basic " + "~ ~");
headers.put(OAuth.HeaderType.CONTENT_TYPE, "application/x-www-form-urlencoded");
request.setHeaders(headers);
OAuthClient oAuthClient = new OAuthClient(new URLConnectionClient());
oAuthResponse = oAuthClient.accessToken(request); // program goes to catch block from here while I'm expecting oAuthResponse to contain the above error message (invalid refresh token message)
log.debug("response: " + oAuthResponse.getBody()); // not getting printed..
Map<String, Object> tokenMap = convertJsonToMap(oAuthResponse.getBody());
newAccessToken = tokenMap.get("access_token").toString();
refreshToken = tokenMap.get("refresh_token").toString();
String userId = tokenMap.get("user_id").toString();
String expiresIn = tokenMap.get("expires_in").toString();
updateOAuthCredentials(accountDeviceId, newAccessToken, refreshToken, connection);
} catch (Exception e) {
log.debug("inside catch");
}
return newAccessToken;
}