100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > java实现模拟登录新浪微博

java实现模拟登录新浪微博

时间:2023-08-13 23:18:47

相关推荐

java实现模拟登录新浪微博

最近做新浪微博的数据挖掘,遇到不少的问题,搜索了不少的网上资源,现在我来总结一下,希望对大家有所帮助:

1.准备工作

只是登录无需申请新浪和腾迅的开发者账号,如果需要发送微博功能,需要申请一个新浪和腾迅的开发者账号,并添加一个测试应用。

过程请参考官方帮助文档,申请地址:新浪: 腾迅:http://dev./

我们需要的是App Key和App Secre及redirect_URI,源代码中已经包含了我申请的测试key,但由于限制直接用我的key你们的账号是无法登录成功的。

2.注意事项

1)需要注意的是应用的App Key和App Secre及redirect_URI,对应项目根目录下的config.properties配置文件中的

client_ID=1745656892client_SERCRET=66056719c1d8ca7bcaf36f411217cefaredirect_URI=

redirect_URI由于只是测试用并没有直接的回调页面,所以这里随便填写一个地址就行了,但要注意与应用-高级设置里的“回调页面”一致。

2)代码中的测试账号需要要自己添加测试账号,新浪的在“应用信息-测试账号”;腾迅的在“权限控制-创建白名单”中。当然直接用

开发者账号也可以。

3)发送微博引用了新浪的weibo4j-oauth2-beta2.1.1.zip,

3.代码

package org.utils;import java.io.IOException;import java.util.ArrayList;import java.util.List;import mons.httpclient.Header;import mons.httpclient.HttpClient;import mons.httpclient.methods.PostMethod;import mons.httpclient.params.HttpMethodParams;import org.apache.http.HttpException;import org.core.weibo.sina.Oauth;import org.core.weibo.sina.Timeline;import org.core.weibo.sina.http.AccessToken;import org.core.weibo.sina.model.WeiboException;import org.core.weibo.sina.weibo4j.util.WeiboConfig;/**** 模拟自动登录并发微博* @author zdw**/public class Sina {/**** 模拟登录并得到登录后的Token* @param username 用户名* @param password 密码* @return* @throws HttpException* @throws IOException*/public static AccessToken getToken(String username,String password) throws HttpException, IOException {String clientId = WeiboConfig.getValue("client_ID") ;String redirectURI = WeiboConfig.getValue("redirect_URI") ;String url = WeiboConfig.getValue("authorizeURL");PostMethod postMethod = new PostMethod(url);//应用的App Key postMethod.addParameter("client_id",clientId);//应用的重定向页面postMethod.addParameter("redirect_uri",redirectURI);//模拟登录参数//开发者或测试账号的用户名和密码postMethod.addParameter("userId", username);postMethod.addParameter("passwd", password);postMethod.addParameter("isLoginSina", "0");postMethod.addParameter("action", "submit");postMethod.addParameter("response_type","code");HttpMethodParams param = postMethod.getParams();param.setContentCharset("UTF-8");//添加头信息List<Header> headers = new ArrayList<Header>();headers.add(new Header("Referer", "/oauth2/authorize?client_id="+clientId+"&redirect_uri="+redirectURI+"&from=sina&response_type=code"));headers.add(new Header("Host", ""));headers.add(new Header("User-Agent","Mozilla/5.0 (Windows NT 6.1; rv:11.0) Gecko/0101 Firefox/11.0"));HttpClient client = new HttpClient();client.getHostConfiguration().getParams().setParameter("http.default-headers", headers);client.executeMethod(postMethod);int status = postMethod.getStatusCode();System.out.println(status);if (status != 302){System.out.println("token刷新失败");return null;}//解析TokenHeader 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) {Oauth oauth = new Oauth();try{AccessToken token = oauth.getAccessTokenByCode(code);return token;}catch(Exception e){e.printStackTrace();}}}}return null;}/*** 发微博* @param token 认证Token* @param content 微博内容* @return* @throws Exception*/public static boolean sinaSendWeibo(String token,String content) throws Exception {boolean flag = false ;Timeline timeline = new Timeline();timeline.client.setToken(token);try {timeline.UpdateStatus(content);flag = true ;} catch (WeiboException e) {flag = false ;System.out.println(e.getErrorCode());}return flag;}public static void main(String[] args) throws Exception{AccessToken at = getToken("xxxx","xxx");sinaSendWeibo(at.getAccessToken(),"测试呢");}}</span></span>

网址为:新浪微博模拟登录

4,获取应用授权,即允许应用访问用户相应信息(除用户名,密码)

由于新浪使用了auth2.0,所以整个登陆过程分为了两个部分,一个部分即是登陆授权,即允许我们的应用访问指定用户的相应信息,这里的信息不包括用户名,密码。在类似网页的应用中,即通过弹出一个新浪的登陆框,用户通过输入完用户名,密码之后,并且授权应用使用,新浪验证通过之后,再返回到我们应用所在的主界面中来。第二个部分,即是获取到相应的token,即相当于用户session,以便在后面的操作中,使用此token进行数据访问。

5,代码如下:

a,应用授权的地址为:/oauth2/authorize,整个代码如下所示:

<pre name="code" class="java"><span style="font-size: 14px;"><span style="background-color: rgb(249, 249, 249); color: rgb(51, 51, 51);"></span></span><pre name="code" class="html"><strong style="font-family: 'Microsoft Yahei', Arial, Georgia, Helvetica, sans-serif;"> </strong><span style="font-family:Courier New;"> PostMethod postMethod = new PostMethod("/oauth2/authorize");postMethod.addParameter("client_id", props.getProperty("client_ID")); //appkeypostMethod.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参考界面:/wiki/OAuth2/access_token

<span style="font-family:Courier New;font-size:14px;color:#333333;"><span style="font-size: 14px; line-height: 20px; text-align: justify; background-color: rgb(255, 255, 255);"></span></span><pre name="code" class="java"><span style="font-family:Courier New;">PostMethod tokenMethod = new PostMethod("/oauth2/access_token");tokenMethod.addParameter("client_id", props.getProperty("client_ID"));//appkeytokenMethod.addParameter("client_secret", props.getProperty("client_SERCRET")); //appsecrettokenMethod.addParameter("grant_type", "authorization_code");tokenMethod.addParameter("code", code); //上一步骤拿到的codetokenMethod.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_tokenSystem.out.println("access_token-->" + access_token);

c,获取最新的微博信息列表

api参考界面:/wiki/2/statuses/home_timeline

代码如下:

GetMethod lastNewMethod = new GetMethod("/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);

网址为:新浪微博模拟登录网址出处

小结:

1,在这两中资源的帮助下,我的java模拟登录新浪微博逐渐由了毛头,完成了大部分,下载出现了一个很多人都遇到的一个问题就是access Tocket无效,据查网说:access tocket的有效期为 一天,当你是测试用户时,你可以进入新浪微博的官方网查看自己所处的级别,不懂得可以去查网

<span style="font-family: Arial, Helvetica, sans-serif;">2,真希望对大家有所帮助,少走弯路!</span>

/link?url=tpqlVY1pSWjrokeD7XNjS8mRS1XR3oQC5pR0Z9m9V_0lOLdJDs_HzOlE4raybvy8UkI_j2KwMz8x-XERpZ3FK_wyn6ZUuCHOo3af9q1Roj3

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

package test;import java.io.BufferedReader;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import java.io.UnsupportedEncodingException;import .HttpURLConnection;import .URL;import mons.io.IOUtils;import org.apache.log4j.Logger;public class fHttpRequestProxy {private static Logger logger = Logger.getLogger(CopyOfHttpRequestProxy.class);/*** * @param reqUrl* HTTP 请求URL* @param recvEncoding* HTTP的字符编码* @return HTTP响应的字符串* @throws IOException* @throws UnsupportedEncodingException*/public static String doGet(String reqUrl)throws UnsupportedEncodingException, IOException {// 首先要和URL下的URLConnection对话。 URLConnection可以很容易的从URL得到URL url = new URL(reqUrl);HttpURLConnection url_con = (HttpURLConnection) url.openConnection();/*** 然后把连接设为输出模式。URLConnection通常作为输入来使用,比如下载一个Web页。* 通过把URLConnection设为输出,你可以把数据向你个Web页传送.*/url_con.setDoOutput(true);/*** 最后,为了得到OutputStream,简单起见,把它约束在Writer并且放入POST信息中,例如: ...*/OutputStreamWriter out = new OutputStreamWriter(url_con.getOutputStream(), "utf-8");// 其中的memberName和password也是阅读html代码得知的,即为表单中对应的参数名称out.write("memberName=myMemberName&password=myPassword");// post的关键所在!out.flush();out.close();// 取得cookie,相当于记录了身份,供下次访问时使用String cookieVal = url_con.getHeaderField("Set-Cookie");return cookieVal;}public static void a(String s, String cookieVal)throws FileNotFoundException, IOException {URL url = new URL(s);HttpURLConnection resumeConnection = (HttpURLConnection) url.openConnection();if (cookieVal != null) {// 发送cookie信息上去,以表明自己的身份,否则会被认为没有权限resumeConnection.setRequestProperty("Cookie", cookieVal);}resumeConnection.connect();InputStream urlStream = resumeConnection.getInputStream();BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlStream));String ss = null;String total = "";while ((ss = bufferedReader.readLine()) != null) {total += ss;}IOUtils.write(total, new FileOutputStream("d:/index.html"));bufferedReader.close();}public static void main(String[] args) throws UnsupportedEncodingException, IOException {String surl = "/index.php/action/UserLogin";String s = "/dispatcher.php/module/Resume/action/Preview";String result = CopyOfHttpRequestProxy.doGet(surl);System.out.println("result---------->" + result);CopyOfHttpRequestProxy.a(surl, result);}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。