BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / java / #23131同步于 2012/8/8
Java机器人发帖

新浪微博如何自动获取code

wugh
2012/8/8镜像同步0 回复
各位大神,微博应用在授权的时候那个access_token只有一天,所以我想通过授权页面自动获取access_token,不知道能不能实现。 授权链接如下:https://api.weibo.com/oauth2/authorize?client_id=266251896&redirect_uri=http://www.baidu.com&response_type=code 在水木上找到一段代码,作者说可以授权,但是我实验都失败了。。 package weiboola.auth2; import org.apache.commons.httpclient.Header; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.methods.PostMethod; import org.apache.commons.httpclient.params.HttpMethodParams; import weibo4j.Oauth; import weibo4j.http.AccessToken; import weibo4j.model.WeiboException; import weibo4j.util.BareBonesBrowserLaunch; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; import java.util.Properties; public class OAuth4Code { public static void main(String[] args) throws WeiboException, IOException { AccessToken token = refreshToken(); System.out.println(token); } public static AccessToken refreshToken() throws WeiboException { Properties props = new Properties(); Oauth oauth = new Oauth(); try { props.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("sina_account.properties")); String url = props.getProperty("authorizeURL");/*模拟登录的地址,https://api.weibo.com/oauth2/authorize*/ PostMethod postMethod = new PostMethod(url); postMethod.addParameter("client_id", props.getProperty("client_ID"));//your client id postMethod.addParameter("redirect_uri", props.getProperty("redirect_URI"));//your url postMethod.addParameter("userId", props.getProperty("userId"));//需要获取微薄的use id postMethod.addParameter("passwd", props.getProperty("passwd")); postMethod.addParameter("isLoginSina", "0"); postMethod.addParameter("action", "submit"); postMethod.addParameter("response_type", props.getProperty("response_type"));//code HttpMethodParams param = postMethod.getParams(); param.setContentCharset("UTF-8"); List<Header> headers = new ArrayList<Header>(); System.out.println(oauth.authorize("code")); headers.add(new Header("Referer", "https://api.weibo.com/oauth2/authorize?client_id=266251896&redirect_uri=http://www.baidu.com&response_type=code")); headers.add(new Header("Host", "api.weibo.com")); headers.add(new Header("User-Agent", "Mozilla/5.0 (Windows NT 6.1; rv:11.0) Gecko/20100101 Firefox/11.0")); HttpClient client = new HttpClient(); client.getHostConfiguration().getParams().setParameter("http.default-headers", headers); client.executeMethod(postMethod); int status = postMethod.getStatusCode(); if (status != 302) { Log.logInfo("refresh token failed"); return null; } Header location = postMethod.getResponseHeader("Location"); if (location != null) { String retUrl = location.getValue(); int begin = retUrl.indexOf("code="); if (begin != -1) { int end = retUrl.indexOf("&", begin); if (end == -1) end = retUrl.length(); String code = retUrl.substring(begin + 5, end); if (code != null) { AccessToken token = oauth.getAccessTokenByCode(code); return token; } } } } catch (FileNotFoundException e) { Log.logInfo("error" + e); } catch (IOException e) { Log.logInfo("error" + e); } Log.logInfo("refresh token failed"); return null; } } 无法成功,一直抛出异常“errorjavax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path validation failed: java.security.cert.CertPathValidatorException: Path does not chain with any of the trust anchors”
订阅后,新回复会通过你的通知中心匿名送达。
0 条回复
暂无回复 · 你可以订阅本帖等待新回复。