使用java代码获取新浪微博应用的access token代码实例
程序员文章站
2023-12-01 12:35:10
本文实例为大家分享了java代码获取新浪微博应用的access token的具体代码,供大家参考,具体内容如下
package test;
import...
本文实例为大家分享了java代码获取新浪微博应用的access token的具体代码,供大家参考,具体内容如下
package test; import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstream; import java.io.inputstreamreader; import java.net.uri; import java.net.urisyntaxexception; import org.apache.http.httpentity; import org.apache.http.httphost; import org.apache.http.httpresponse; import org.apache.http.client.httpclient; import org.apache.http.client.methods.httppost; import org.apache.http.conn.params.connroutepnames; import org.apache.http.impl.client.defaulthttpclient; public class weiboaccesstokenrequest { static public void main(string[] arg) { //system.setproperty("http.proxyhost", "proxy.wdf.diablo.corp"); //system.setproperty("http.proxyport", "8080"); httpclient httpclient = new defaulthttpclient(); httppost post = new httppost(); uri url; try { //url = new uri("https://api.weibo.com/oauth2/access_token"); string request = "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 = new uri(request); post.seturi(url); httphost proxy = new httphost("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");*/ httpresponse response = httpclient.execute(post); httpentity entity = response.getentity(); if (entity == null) { system.out.println("response is null!"); return; } inputstream instreams = entity.getcontent(); string str = convertstreamtostring(instreams); system.out.println("do something"); system.out.println(str); } catch (exception e) { e.printstacktrace(); } } public static string convertstreamtostring(inputstream is) { bufferedreader reader = new bufferedreader(new inputstreamreader(is)); stringbuilder sb = new stringbuilder(); string line = null; try { while ((line = reader.readline()) != null) { sb.append(line + "\n"); } } catch (ioexception e) { e.printstacktrace(); } finally { try { is.close(); } catch (ioexception e) { e.printstacktrace(); } } return sb.tostring(); } }
以上所述是小编给大家介绍的java代码获取新浪微博应用的access token实例详解整合,希望对大家有所帮助