Tools: Excerpted from Sina Api
///////////////////////////////////////////////////////// //Bare Bones Browser Launch // //Version 1.5 (December 10, 2005) // // By Dem Pilafian // //Supports: Mac OS X, GNU/Linux, Unix, Windows XP // //Example Usage: // // String url = "http://www.centerkey.com/"; // // BareBonesBrowserLaunch.openURL(url); // //Public Domain Software -- Free to Use as You Like // ///////////////////////////////////////////////////////// /** * @author Dem Pilafian * @author John Kristian */ import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import javax.swing.JOptionPane; public class BareBonesBrowserLaunch { public static void openURL(String url) { try { browse(url); } catch (Exception e) { JOptionPane.showMessageDialog(null, "Error attempting to launch web browser:\n" + e.getLocalizedMessage()); } } private static void browse(String url) throws ClassNotFoundException, IllegalAccessException, IllegalArgumentException, InterruptedException, InvocationTargetException, IOException, NoSuchMethodException { String osName = System.getProperty("os.name", ""); if (osName.startsWith("Mac OS")) { Class fileMgr = Class.forName("com.apple.eio.FileManager"); Method openURL = fileMgr.getDeclaredMethod("openURL", new Class[] { String.class }); openURL.invoke(null, new Object[] { url }); } else if (osName.startsWith("Windows")) { Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url); } else { // assume Unix or Linux String[] browsers = { "firefox", "opera", "konqueror", "epiphany", "mozilla", "netscape" }; String browser = null; for (int count = 0; count < browsers.lengt
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; import sohu.api.example.util.BareBonesBrowserLaunch; public class GetAccessTocken { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { getAuthorize("eAJUqlajMZQwaK5NTWL6","4nN6%0ZNc7C=WFIdSG7a=bzr1u(qWziFi%t3wcq4","http://www.baidu.com"); } public static void getAuthorize(String client_id,String client_secret,String redirect_uri) throws ClientProtocolException, IOException{ String openURL = "https://api.t.sohu.com/oauth2/authorize"; openURL += "?client_id="+client_id; openURL += "&scope=basic&response_type=code"+ "&redirect_uri="+redirect_uri; BareBonesBrowserLaunch.openURL(openURL ); System.out.print("Hit enter when it's done.[Enter]:"); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String code = br.readLine(); getAccessTocken( client_id, client_secret, code, redirect_uri ); } public static void getAccessTocken(String client_id,String client_secret,String code,String redirect_uri ) throws ClientProtocolException, IOException{ String url = "https://api.t.sohu.com/oauth2/access_token"; client_secret = (new sun.misc.BASE64Encoder()).encode( client_secret.getBytes() ); DefaultHttpClient httpclient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(url); List <NameValuePair> nvps = new ArrayList <NameValuePair>(); nvps.add(new BasicNameValuePair("client_id", client_id)); nvps.add(new BasicNameValuePair("client_secret", client_secret)); nvps.add(new BasicNameValuePair("code", code)); nvps.add(new BasicNameValuePair("redirect_uri", redirect_uri)); nvps.add(new BasicNameValuePair("grant_type", "authorization_code")); httpPost.setEntity(new UrlEncodedFormEntity(nvps)); HttpResponse response2 = httpclient.execute(httpPost); HttpEntity entity2 = response2.getEntity(); String entitys = EntityUtils.toString( entity2); System.out.println(entitys); EntityUtils.consume(entity2); } }
h && browser == null; count++) if (Runtime.getRuntime().exec(new String[] { "which", browsers[count] }).waitFor() == 0) browser = browsers[count]; if (browser == null) throw new NoSuchMethodException("Could not find web browser"); else Runtime.getRuntime().exec(new String[] { browser, url }); } } }
The actual running class: