使用java代码获取新浪微博应用的access token代码实例
本文实例为大家分享了java代码获取新浪微博应用的accesstoken的具体代码,供大家参考,具体内容如下
packagetest; importjava.io.BufferedReader; importjava.io.IOException; importjava.io.InputStream; importjava.io.InputStreamReader; importjava.net.URI; importjava.net.URISyntaxException; importorg.apache.http.HttpEntity; importorg.apache.http.HttpHost; importorg.apache.http.HttpResponse; importorg.apache.http.client.HttpClient; importorg.apache.http.client.methods.HttpPost; importorg.apache.http.conn.params.ConnRoutePNames; importorg.apache.http.impl.client.DefaultHttpClient; publicclassWeiboAccessTokenRequest { staticpublicvoidmain(String[]arg) { //System.setProperty("http.proxyHost","proxy.wdf.diablo.corp"); //System.setProperty("http.proxyPort","8080"); HttpClienthttpclient=newDefaultHttpClient(); HttpPostpost=newHttpPost(); URIurl; try { //url=newURI("https://api.weibo.com/oauth2/access_token"); Stringrequest="https://api.weibo.com/oauth2/access_token?client_id=3921363495&client_secret=bac53e1f9c1e66514cf7410e39d581dd" +"&grant_type=authorization_code&code=7420036e360713bab82f62a5275aaba7&redirect_uri=https://api.weibo.com/oauth2/default.html"; url=newURI(request); post.setURI(url); HttpHostproxy=newHttpHost("proxy.wdf.sap.corp",8080); httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,proxy); /*post.addHeader("client_id","3921363495"); post.addHeader("client_secret","bac53e1f9c1e66514cf7410e39d581dd"); post.addHeader("grant_type","authorization_code"); post.addHeader("code","7420036e360713bab82f62a5275aaba7"); post.addHeader("redirect_uri","https://api.weibo.com/oauth2/default.html");*/ HttpResponseresponse=httpclient.execute(post); HttpEntityentity=response.getEntity(); if(entity==null) { System.out.println("responseisnull!"); return; } InputStreaminstreams=entity.getContent(); Stringstr=convertStreamToString(instreams); System.out.println("Dosomething"); System.out.println(str); } catch(Exceptione) { e.printStackTrace(); } } publicstaticStringconvertStreamToString(InputStreamis) { BufferedReaderreader=newBufferedReader(newInputStreamReader(is)); StringBuildersb=newStringBuilder(); Stringline=null; try{ while((line=reader.readLine())!=null){ sb.append(line+"\n"); } }catch(IOExceptione){ e.printStackTrace(); }finally{ try{ is.close(); }catch(IOExceptione){ e.printStackTrace(); } } returnsb.toString(); } }
以上所述是小编给大家介绍的java代码获取新浪微博应用的accesstoken实例详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!