注:本文代码已失效,请不要模仿之,其他方式请参考 使用scribe方式
最近研究了下新浪微博的开发api,尝试在某些客户端应用中也能够调用相应的api发送新浪微博,经过一番的尝试,最后成功。现将其中的几个关键点,以及相应的demo公示如下,供大家了解。
在本文中,主要演示了如何通过用户名/密码的方式,连接上新浪微博,然后读取最新的微博信息,最后演示如何发送一条简单的微博,其它的都可以通过相应的微博api进行处理,这里就没有仔细深入了。
首先得申请开发者授权,也就是你得在新浪上申请一个应用。直接通过地址为:http://open.weibo.com/,使用你的微博帐号登陆之后,创建一个应用即可。创建应用的目的在于获取相应的app Key和app Secret,在后面的主要api中,均会使用到app key,以便于新浪官方跟踪请求来源。具体流程,请google之。
申请好了之后,我们就开始我们的api之旅了,在本文中,没有使用官方提供的java sdk,因为主是简单的demo,如果是应用开发,还是可以下载相应的java sdk应用的,可以查看其中的example,了解如何进行调用。
本文使用了httpclient,使用maven的话,可以使用groupId为commons-httpclient,artifactId为commons-httpclient将其导入到工程中。其次,为了便于解析返回的数据,使用了JSON组件(fastjson),用于解析json字符串。
获取应用授权,即允许应用访问用户相应信息(除用户名,密码)
由于新浪使用了auth2.0,所以整个登陆过程分为了两个部分,一个部分即是登陆授权,即允许我们的应用访问指定用户的相应信息,这里的信息不包括用户名,密码。在类似网页的应用中,即通过弹出一个新浪的登陆框,用户通过输入完用户名,密码之后,并且授权应用使用,新浪验证通过之后,再返回到我们应用所在的主界面中来。第二个部分,即是获取到相应的token,即相当于用户session,以便在后面的操作中,使用此token进行数据访问。
应用授权的地址为:https://api.weibo.com/oauth2/authorize,整个代码如下所示:
PostMethod postMethod = new PostMethod("https://api.weibo.com/oauth2/authorize"); postMethod.addParameter("client_id", props.getProperty("client_ID")); //appkey postMethod.addParameter("redirect_uri", props.getProperty("redirect_URI")); //oauth2 回调地址 postMethod.addParameter("response_type", "code");//请求code信息 postMethod.addParameter("action", "submit");//表示授权访问 postMethod.addParameter("userId", "xxxx"); //微博帐号 postMethod.addParameter("passwd", "xxxx"); //帐号密码 client.executeMethod(postMethod); String url = postMethod.getResponseHeader("location").getValue();//取得重定向的地址信息 /** 以下为获取新浪返回的code信息 */ String params = url.substring(url.lastIndexOf("?") + 1); Map<String, String[]> p = parseQueryString(params); String code = p.get("code")[0]; //取得code数据 System.out.println("code->" + code);
获取access_token
api参考界面:http://open.weibo.com/wiki/OAuth2/access_token
整个代码如下所示:
PostMethod tokenMethod = new PostMethod("https://api.weibo.com/oauth2/access_token"); tokenMethod.addParameter("client_id", props.getProperty("client_ID")); //appkey tokenMethod.addParameter("client_secret", props.getProperty("client_SERCRET")); //appsecret tokenMethod.addParameter("grant_type", "authorization_code"); tokenMethod.addParameter("code", code); //上一步骤拿到的code tokenMethod.addParameter("redirect_uri", props.getProperty("redirect_URI")); //回调地址 client.executeMethod(tokenMethod); String result = tokenMethod.getResponseBodyAsString(); JSONObject jsonObject = JSON.parseObject(result); String access_token = jsonObject.getString("access_token");//获取到的access_token System.out.println("access_token-->" + access_token);
获取最新的微博信息列表
api参考界面:http://open.weibo.com/wiki/2/statuses/home_timeline
代码如下:
GetMethod lastNewMethod = new GetMethod("https://api.weibo.com/2/statuses/home_timeline.json"); List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>(); nameValuePairList.add(new NameValuePair("access_token", access_token));//access_token 必填 lastNewMethod.setQueryString(nameValuePairList.toArray(new NameValuePair[nameValuePairList.size()])); client.executeMethod(lastNewMethod); result = lastNewMethod.getResponseBodyAsString(); jsonObject = JSON.parseObject(result); System.out.println("lastnew->" + jsonObject);
发一条微博信息
api参考:http://open.weibo.com/wiki/2/statuses/update
代码如下:
PostMethod newMethod = new PostMethod("https://api.weibo.com/2/statuses/update.json"); newMethod.addParameter("access_token", access_token); newMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "utf-8");//解决乱码问题,避免传输中文时微博上显示乱码 newMethod.addParameter("status", "使用java发新浪weibo test234563"); client.executeMethod(newMethod); result = newMethod.getResponseBodyAsString(); jsonObject = JSON.parseObject(result); System.out.println("newM->" + jsonObject);
结论
其实,整个过程还是很简单的,最主要的还是最开始两步的登陆和获取授权,这两步通了,后面的都是进行json请求,然后传递参数,获取结果,再处理结果的问题了。
值得注意的是,在第二步的获取access_token中,官方文档中写可以通过grant_type=password,即输入用户名密码的方式获取到token信息。不过,这种方式需要特殊的申请,具体申请步骤复杂且必须通过官方的审核,非一般开发人员能够弄得好的。所以这里就没有使用这种(当然使用这种方式的话,可以将第一步和第二步,合二为一)。
转载请标明出处:i flym
本文地址:https://www.iflym.com/index.php/code/201112220001.html
恩,不错
你好,header中没有“location”项,getResponseHeader(“location”)内容为null应该如何解决呢?
经测试,新浪使用了ticket机制,这种方法已经失效,可以算是新浪的一个bug。你可以看下http://www.iflym.com/index.php/code/201201030001.html这个,这个可以工作。