Java จำลอง Sina และ Tencent เข้าสู่ระบบโดยอัตโนมัติและส่งฟังก์ชั่น Weibo ไปยังทุกคนสำหรับการอ้างอิงของคุณ เนื้อหาเฉพาะมีดังนี้
1. การเตรียม
เพียงเข้าสู่ระบบโดยไม่ต้องสมัครบัญชีนักพัฒนาของ Sina และ Tencent หากคุณต้องการส่งฟังก์ชั่น Weibo คุณต้องสมัครบัญชีนักพัฒนาของ Sina และ Tencent และเพิ่มแอปพลิเคชันทดสอบ
โปรดดูเอกสารช่วยเหลืออย่างเป็นทางการสำหรับกระบวนการ ที่อยู่แอปพลิเคชัน: Sina: http://open.weibo.com Tengxun: http://dev.t.qq.com/
สิ่งที่เราต้องการคือคีย์แอพแอพ Secre และ Redirect_uri ซอร์สโค้ดมีคีย์ทดสอบที่ฉันสมัครอยู่แล้ว แต่เนื่องจากข้อ จำกัด บัญชีของคุณไม่สามารถลงชื่อเข้าใช้ได้สำเร็จโดยใช้คีย์ของฉันโดยตรง
2. สิ่งที่ควรทราบ
1) ควรสังเกตว่าคีย์แอปของแอปพลิเคชันแอป Secre และ Redirect_uri อยู่ในไฟล์กำหนดค่าที่สอดคล้องกันในไฟล์การกำหนดค่าในไดเรกทอรีรูทของโครงการ
client_id = 1745656892
client_sercret = 66056719C1D8CA7BCAF36F411217CEFA
redirect_uri = www.baidu.com
เนื่องจาก Redirect_uri ใช้สำหรับการทดสอบเท่านั้นและไม่มีหน้าโทรกลับโดยตรงคุณสามารถกรอกที่อยู่ได้ที่นี่ แต่ระวังให้สอดคล้องกับ "หน้าโทรกลับ" ในการตั้งค่าแอปพลิเคชันขั้นสูง
2) บัญชีทดสอบในรหัสจำเป็นต้องเพิ่มบัญชีทดสอบด้วยตัวเอง "บัญชีทดสอบข้อมูลแอปพลิเคชัน" ของ Sina; "การอนุญาตการอนุญาตการอนุญาตการอนุญาตการอนุญาตการอนุญาต" ของ Tengxun แน่นอนว่ามันเป็นไปได้ที่จะใช้บัญชีนักพัฒนาโดยตรง
3) ส่ง Weibo เพื่ออ้างถึง Weibo4j-Ouuth2-beta2.1.1.zip ของ Sina และ Java_sdk_v1.2.1.7z ของ Tengxun และ Tengxun คลาสหลักอยู่ภายใต้แพ็คเกจ UTIL
3. รหัสคีย์
1) Sina
แพ็คเกจ org.utils; นำเข้า java.io.ioexception; นำเข้า java.util.arraylist; นำเข้า java.util.list; นำเข้า org.apache.commons.httpclient.header; นำเข้า org.apache.commons.httpclient.httpclient; org.apache.commons.httpclient.params.httpmethodparams นำเข้า org.apache.http.httpexception; นำเข้า org.core.weibo.sina.oauth; นำเข้า org.core.weibo.sina.timeline; org.core.weibo.sina.model.weiboexception; นำเข้า org.core.weibo.sina.weibo4j.util.weiboconfig;/**** จำลองการเข้าสู่ระบบอัตโนมัติ รหัสผ่านรหัสผ่าน * @return * @throws httpexception * @throws ioexception */ สาธารณะ accesstoken getToken (String username, String Password) พ่น httpexception, ioexception {string clientId = weiboconfig.getValue ("client_id"); String redirecturi = weiboconfig.getValue ("redirect_uri"); string url = weiboconfig.getValue ("AuthorizeUrl"); postmethod postmethod = postmethod ใหม่ (URL); // แอพคีย์ postmethod.addparameter ("client_id", clientId); // หน้าเปลี่ยนเส้นทางของแอพ postmethod.addparameter ("redirect_uri", redirecturi); // พารามิเตอร์การเข้าสู่ระบบจำลอง // นักพัฒนาหรือบัญชีทดสอบชื่อผู้ใช้และรหัสผ่าน postmethod.addparameter ("userId", ชื่อผู้ใช้); postmethod.addparameter ("passwd", รหัสผ่าน); postmethod.addparameter ("Isloginsina", "0"); postmethod.addparameter ("การกระทำ", "ส่ง"); postmethod.addparameter ("response_type", "รหัส"); httpmethodparams param = postmethod.getParams (); param.setContentCharset ("UTF-8"); // เพิ่มรายการข้อมูลส่วนหัว <ส่วนหัว> ส่วนหัว = new ArrayList <ส่วนหัว> (); Headers.Add (ส่วนหัวใหม่ ("ผู้อ้างอิง", "https://api.weibo.com/oauth2/authorize?client_id="+Clientid+"&edirect_uri="+edirecturi+"&from=sina&response_type headers.add (ส่วนหัวใหม่ ("โฮสต์", "api.weibo.com")); headers.add (ส่วนหัวใหม่ ("ผู้ใช้ตัวแทน", "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", ส่วนหัว); client.executemethod (postmethod); สถานะ int = postmethod.getStatusCode (); System.out.println (สถานะ); if (สถานะ! = 302) {system.out.println ("การรีเฟรชโทเค็นล้มเหลว"); คืนค่า null; } // แก้ไขตำแหน่งส่วนหัวโทเค็น = postmethod.getResponseHeader ("ตำแหน่ง"); if (location! = null) {string returl = location.getValue (); int เริ่มต้น = returl.indexof ("code ="); if (เริ่มต้น! = -1) {int end = returl.indexof ("&" เริ่มต้น); if (end == -1) end = returl.length (); String code = returl.substring (เริ่มต้น + 5, end); if (code! = null) {oauth oauth = new oauth (); ลอง {AccessToken token = oauth.getAccessTokenByCode (รหัส); โทเค็นกลับมา; } catch (exception e) {e.printstacktrace (); }}}} return null; } / ** * ส่ง weibo * @param โทเค็นโทเค็นที่ได้รับการรับรอง * เนื้อหา @param เนื้อหา Weibo เนื้อหา * @return * @throws Exception * / Public Static Boolean Sinasendweibo (โทเค็นสตริงเนื้อหาสตริง) ไทม์ไลน์ไทม์ไลน์ = timeline ใหม่ (); timeline.client.settoken (โทเค็น); ลอง {timeline.updatestatus (เนื้อหา); ธง = จริง; } catch (weiboexception e) {flag = false; System.out.println (e.geterRorCode ()); } return flag; } โมฆะคงที่สาธารณะหลัก (สตริง [] args) โยนข้อยกเว้น {accessToken at = getToken ("xxxx", "xxx"); Sinasendweibo (at.getaccessToken (), "ทดสอบ"); - 2) Tengxun
แพ็คเกจ org.utils; นำเข้า Java.io.ByTearrayOutputStream; นำเข้า Java.io.FileOutputStream; นำเข้า java.io.ioException; นำเข้า Java.io.OutputStream; นำเข้า java.io.Unsupportencodexception; net.sf.json.jsonobject; นำเข้า org.apache.http.httpentity; นำเข้า org.apache.http.httpresponse; นำเข้า org.apache.http.client.clientprotocolexception; นำเข้า org.apache.http.client.htt org.apache.http.client.methods.httpget; นำเข้า org.apache.http.impl.client.defaulthttpClient; นำเข้า org.apache.http.util.entityutils org.core.weibo.tencent.oauthv2.oauthv2; นำเข้า org.core.weibo.tencent.oauthv2.oauthv2client;/****Tengxun ลงชื่อเข้าใช้โดยอัตโนมัติและได้รับข้อมูลส่วนบุคคล สาธารณะคงที่ oauthv2 oauth = ใหม่ OAuthv2 (); ไคลเอนต์ httpClient ส่วนตัว = ใหม่ defaulthttpClient (); // ข้อมูลแอปพลิเคชัน OAUTH เริ่มต้นเป็นโมฆะคงที่สาธารณะเริ่มต้น (OAUTHV2 OAUTH) {OAUTH.SetClientId ("801216331"); oauth.setClientsecret ("EA71B26B0CBE5778CDD1C09AD17553A3"); oauth.setredirecturi ("http://www.tencent.com/zh-cn/index.shtml"); }/** * * @param qq * http://check.ptlogin2.qq.com/check?uin= {0}&appid=15000101&r= {1} * ค่าที่สามส่งคืน * @param รหัสผ่าน qq * @param VerifyCode สตริงคงที่ getPassword (สตริง QQ, รหัสผ่านสตริง, สตริง VerifyCode) พ่นข้อยกเว้น {สตริง p = hexchar2bin (MD5 (รหัสผ่าน)); String u = md5 (p + hexchar2bin (qq.replace ("// x", "") .touppercase ())); String V = MD5 (U + VerifyCode.touppercase ()); กลับ V; } สตริงคงที่สาธารณะ md5 (สตริงต้นฉบับข้อความ) พ่นข้อยกเว้น {byte buf [] = OriginalText.getBytes ("ISO-8859-1"); StringBuffer hexString = new StringBuffer (); สตริงผลลัพธ์ = ""; สตริงตัวเลข = ""; ลอง {อัลกอริธึม messageDigest = messageGeSt.getInstance ("MD5"); อัลกอริทึม. Reset (); อัลกอริทึม. UPDATE (BUF); ไบต์ [] digest = อัลกอริทึม. digest (); สำหรับ (int i = 0; i <digest.length; i ++) {digit = integer.tohexstring (0xff & digest [i]); if (digit.length () == 1) {digit = "0" + digit; } hexstring.append (ตัวเลข); } result = hexString.toString (); } catch (Exception Ex) {result = ""; } return result.touppercase (); } สตริงคงที่สาธารณะ hexchar2bin (String md5str) พ่น unsupportencodingexception {byteArrayOutputStream baos = ใหม่ byteArrayOutputStream (md5str.length () / 2); สำหรับ (int i = 0; i <md5str.length (); i = i + 2) {baos.write ((hexstring.indexof (md5str.charat (i)) << 4 | hexstring .indexof (md5str.charat (i + 1))); } ส่งคืนสตริงใหม่ (baos.tobytearray (), "ISO-8859-1"); }/**** การเข้าสู่ระบบการจำลอง* @param qq หมายเลข qq* @param รหัสผ่าน qq รหัสผ่าน* @throws ข้อยกเว้น*/การเข้าสู่ระบบแบบคงที่สาธารณะ (สตริง qq, รหัสผ่านสตริง) โยนข้อยกเว้น {httpget get = new httpget ( "& appid = 46000101 & ptlang = 2052 & js_type = 2 & js_ver = 10009 & r = 0.7948186025712065"); httPresponse response = client.execute (รับ); string entity = entityutils.toString (response.getEntity ()); String [] checkNum = entity.substring (entity.indexof ("(") + 1, entity.lastindexof (")")). แทนที่ ("", "") .split (","); String pass = ""; สตริง responsedata = ""; // รับรหัสการตรวจสอบ (หากมีเอาต์พุตรหัสการตรวจสอบไปที่ c: /code.jpg ให้ป้อนและดำเนินการต่อเพื่อดำเนินการหาก ("1" .Equals (checkNum [0]))) {// uin เป็นหมายเลข QQ หรือชื่อผู้ใช้ weibo httpget getimg = ใหม่ httpget ("http://captcha.qq.com/getimage?aid=46000101&r=0.3478789969909082&uin=" + qq + "& vc_type =" + checknum [1] + ""); httpresponse response2 = client.execute (getimg); OutputStream OS = ใหม่ fileOutputStream ("C: /code.jpg"); ไบต์ [] b = entityutils.tobytearray (response2.getEntity ()); OS.WRITE (B, 0, B.Length); os.close (); สแกนเนอร์ใน = สแกนเนอร์ใหม่ (System.in); responsedata = in.nextline (); in.close (); } else {responsedata = checkNum [1]; } / ** ********************** รหัสผ่านการเข้ารหัส ****************************** / pass = getPassword (checkNum [2], รหัสผ่าน, responsedata); /** **************************** ********************************************************************************************************* "& AID = 46000101 & Target = TOP & U1 = HTTPS%3A%2FOPEN.T.QQ.com%2FCGI-BIN%2FOAUTH2%2FAUTHORIZE%3FClient_ID%3D" oauth.getredirecturi ()+ "& ptredirect = 1 & h = 1 & from_ui = 1 & dumy = & qlogin_param = abbfew = ddd & ถ้อยคำ =%e6%8e%88%e6%9d%83 & fp = loginerroralert & การกระทำ = 8-13-240977 & g = 1 & t = 1 & httpresponse response2 = client.execute (getimg); httpentity httpentity = response2.getEntity (); string entityxc = entityutils.toString (httpentity); System.out.println (entityxc); } /** * * คำขอแอปพลิเคชัน Weibo Open Platform เพื่อกลับไปที่หน้าการอนุญาตเข้าสู่ระบบ แต่หากไม่มี SessionKey การเข้าสู่ระบบจะไม่สำเร็จ SessionKey พบได้ใน URL ที่วางไว้ในแท็กอินพุตในหน้าส่งคืนดังนั้นคุณต้องได้รับ SessionKey นี้ ในความเป็นจริงคุณสามารถเข้าถึง url โดยตรงในแท็กและกระโดด */สตริงคงที่สาธารณะ geturl () พ่น clientprotocolexception, ioexception {httpget getCode = httpget ใหม่ ("https://open.t.qq.com/cgi-bin/oauth2/authorize "& response_type = code & redirect_uri =" + oauth.getredirecturi () + "& checkstatus = ใช่ & appfrom = & g_tk & checktype = showauth & state ="); httpresponse response3 = client.execute (getCode); httpentity entityqqq = response3.getEntity (); string entityxcc = entityutils.toString (entityQQQ); string form = entityxcc.substring (entityxcc.indexof ("<ฟอร์ม"), entityxcc .indexof ("</form>")); สตริง [] ss = form.split ("/>"); สตริงอินพุต = ""; สำหรับ (int i = 0; i <ss.length; i ++) {ถ้า (ss [i] .indexof ("name =" u1/"")> 0) {input = ss [i]; - } return input.substring (input.indexof ("value =/" ") + 7, input.indexof ("/"type =" "));}/** * แยกวิเคราะห์และตั้งค่าโทเค็น * @param get * @throws exception Response4.getEntity (); String geturlcode = entityutils.toString (EntityQQQ1); string entity = getUrlCode.SubString (getUrlCode.Indexof ("url ="), geturlCode.Indexof ("/"> ")); StringBuffer sb = new StringBuffer (); String [] arr = entity.split ("//?") [1] .split ("&"); สำหรับ (int x = 0; x <arr.length; x ++) {ถ้า (arr [x] .indexof ("รหัส")> = 0 || arr [x] .indexof ("openId")> = 0 || arr [x] .indexof ("openkey")> = 0) {sb.append (arr [x]+") - } // ใช้รหัสเพื่อรับ AccessToken OAuthv2Client.parseAuthorization (sb.substring (0, sb.length () - 1), oauth); oauth.setGrantType ("Authorize_Code"); oauthv2client.accessToken (OAuth); } /**** การโทร (อินเทอร์เฟซบัญชี Tengxun Open Platform) เพื่อรับข้อมูลของบุคคล* @throws Exception* /โมฆะสาธารณะคงที่ getInfo () โยนข้อยกเว้น {// เอาท์พุทโทเค็น หากคุณได้รับโทเค็นนั่นหมายความว่าการเข้าสู่ระบบนั้นประสบความสำเร็จและคุณสามารถดำเนินการต่อไปได้ System.out.println ("token ="+oauth.getAccessToken ()); userapi getUser = ใหม่ userapi (oauth.getoauthversion ()); string userjson = getUser.otherInfo (oauth, "json", "", oauth.getopenid ()); jsonObject userjsonObject = jsonObject.FromObject (userjson); จำนวนเต็ม errCode = (จำนวนเต็ม) userjsonobject.get ("errCode"); if (errCode == 0) {JSONObject userDataJsonObject = (jsonObject) userJsonObject.get ("ข้อมูล"); System.out.println (userdatajsonobject.toString ()); }} โมฆะคงที่สาธารณะหลัก (สตริง [] args) พ่นข้อยกเว้น {init (oauth); เข้าสู่ระบบ ("123145", "xxxx"); httpget get = new httpget (geturl ()); ตั้งถิ่นฐาน (รับ); getinfo (); - 4. จะมีเอาต์พุตบันทึกที่สอดคล้องกันเมื่อส่งสำเร็จ
Sina (บรรทัดสุดท้ายของบันทึก):
2078 Debug [2013-03-14 16:35:29] {"CreateD_AT": "Thu Mar 14 16:35:30 +0800 2013 "," id ": 3555791132949940," mid ":" 3555791132949940 "," Idstr ":" 355579113294940 "," ข้อความ ":" ทดสอบ "," แหล่งที่มา ":" ... "...
Teng Xun:
บันทึกธงสำหรับการเข้าสู่ระบบที่ประสบความสำเร็จ:
ptuicb ('0', '0', 'https: //open.t.qq.com/cgi-bin/oauth2/authorize? client_id = 801216331 & response_type = รหัส & redirect_uri = http:', '1'
qhttpClient httpget [3] การตอบสนอง = {"data": {"เกิด _day": 26, "เกิด _month": 8, "เกิด _year": 2011, "city_code": "2", "comp": null, "country_code": "1", "edu": "
บันทึกไม่ได้อยู่ในรายการเต็ม แต่มีไว้สำหรับการอ้างอิงเท่านั้น
ดาวน์โหลดซอร์สโค้ด: http://xiazai.vevb.com/201607/yuanma/sinaandtencent(vevb.com).rar
ข้างต้นเป็นเนื้อหาทั้งหมดของบทความนี้ ฉันหวังว่ามันจะเป็นประโยชน์ต่อการเรียนรู้ของทุกคนและฉันหวังว่าทุกคนจะสนับสนุน wulin.com มากขึ้น