2015-10-27 32 views
0

我如何使用java張貼此cURL?cURL在Java爲instagram API

curl -F 'client_id=CLIENT_ID' \ 
    -F 'client_secret=CLIENT_SECRET' \ 
    -F 'grant_type=authorization_code' \ 
    -F 'redirect_uri=AUTHORIZATION_REDIRECT_URI' \ 
    -F 'code=CODE' \ 
    https://api.instagram.com/oauth/access_token 

回答

0

這裏是你在Java中如何POST(從here

public static String httpPost(String urlStr, String[] paramName, 
String[] paramVal) throws Exception { 
    URL url = new URL(urlStr); 
    HttpURLConnection conn = 
     (HttpURLConnection) url.openConnection(); 
    conn.setRequestMethod("POST"); 
    conn.setDoOutput(true); 
    conn.setDoInput(true); 
    conn.setUseCaches(false); 
    conn.setAllowUserInteraction(false); 
    conn.setRequestProperty("Content-Type", 
     "application/x-www-form-urlencoded"); 

    // Create the form content 
    OutputStream out = conn.getOutputStream(); 
    Writer writer = new OutputStreamWriter(out, "UTF-8"); 
    for (int i = 0; i < paramName.length; i++) { 
    writer.write(paramName[i]); 
    writer.write("="); 
    writer.write(URLEncoder.encode(paramVal[i], "UTF-8")); 
    writer.write("&"); 
    } 
    writer.close(); 
    out.close(); 

    if (conn.getResponseCode() != 200) { 
    throw new IOException(conn.getResponseMessage()); 
    } 

    // Buffer the result into a string 
    BufferedReader rd = new BufferedReader(
     new InputStreamReader(conn.getInputStream())); 
    StringBuilder sb = new StringBuilder(); 
    String line; 
    while ((line = rd.readLine()) != null) { 
    sb.append(line); 
    } 
    rd.close(); 

    conn.disconnect(); 
    return sb.toString(); 
} 

一種更好的方式來使用Java中的Instagram的API是使用別人編寫的名爲JInstagram一個Java庫。