05-23-2016 23:00 - edited 05-30-2016 05:23
05-23-2016 23:00 - edited 05-30-2016 05:23
hurl success but java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication14;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
/**
*
* @author hwan
*/
public class javaapplication14 {
public static void main(String[] args) {
// TODO code application logic here
final String AUTH_HOST = "https://www.fitbit.com/oauth2/authorize";
final String tokenRequestUrl = "https://api.fitbit.com/oauth2/token";
final String CLIENT_ID = "227LKJ";
final String REDIECRT_URI = "http://127.0.0.1";
final String code = "d4a36beff00a18697e61129906ca649176a27818";
final int expires_in_one_day = 86400;
final int expires_in_one_week = 604800;
final int expires_in_one_month = 2592000;
final int expires_in_one_year = 31536000;
HttpsURLConnection conn = null;
OutputStreamWriter writer = null;
BufferedReader reader = null;
InputStreamReader isr = null;
try {
final String params = String.format("grant_type=authorization_code&redirect_uri=%s&client_id=%s&code=%s"
,REDIECRT_URI,CLIENT_ID,code);
final URL url = new URL(tokenRequestUrl);
conn = (HttpsURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type:", "application/x-www-form-urlencoded");
conn.setRequestProperty("Authorization", "Basic MjI3TEtKOmU5ZTM0NmY3ZDJiMDg4YWRmMzZlZWEwMjgzYjVmNzlh");
writer = new OutputStreamWriter(conn.getOutputStream());
writer.write(params);
writer.flush();
final int responseCode = conn.getResponseCode();
System.out.println("\nSending 'POST' request to URL : " + AUTH_HOST);
System.out.println("Post parameters : " + params);
System.out.println("Response Code : " + responseCode);
isr = new InputStreamReader(conn.getInputStream());
reader = new BufferedReader(isr);
final StringBuffer buffer = new StringBuffer();
String line;
while((line= reader.readLine()) != null) {
buffer.append(line);
}
System.out.println(buffer.toString());
} catch (IOException e) {
System.out.println(e);
} finally {
if (writer != null) {
try {
writer.close();
} catch (Exception Ignore) {
}
}
if (isr != null) {
try {
isr.close();
} catch (Exception Ignore) {
}
}
}
}
}
I have tried to make a http request for getting the acess token
but always java say me this :
i need help!!