Java使用新浪微博API通过账号密码方式登陆微博的实例
程序员文章站
2024-03-09 13:45:11
今天下了个新浪微博的api研究研究,目前实现了发布微博功能,包括带图片的微博。为了安全,新浪微博的api中并没有提供用微博帐号密码登录的功能,而是采用oauth授权,用户通...
今天下了个新浪微博的api研究研究,目前实现了发布微博功能,包括带图片的微博。为了安全,新浪微博的api中并没有提供用微博帐号密码登录的功能,而是采用oauth授权,用户通过浏览器访问新浪网站登录,登录成功后,浏览器再返回key和secret给程序。
main.xml:
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <button android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/login" android:text="登录" /> <edittext android:id="@+id/status" android:layout_width="fill_parent" android:layout_height="300sp" android:hint="输入微博消息" /> <button android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/send" android:text="发布" /> </linearlayout>
一个登录按钮,一个输入框,一个发布按钮
因为要接收浏览器返回的数据,所以,androidmanifest.xml注册activity的时候要加个intent-filter
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.pocketdigi.weibo" android:versioncode="1" android:versionname="1.0"> <uses-sdk android:minsdkversion="7" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".main" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> <intent-filter> <action android:name="android.intent.action.view" /> <category android:name="android.intent.category.default" /> <category android:name="android.intent.category.browsable" /> <data android:scheme="sina" android:host="weibo" /> <!-- 监控sina://weibo这样的地址 --> </intent-filter> </activity> </application> <uses-permission android:name="android.permission.internet"></uses-permission> </manifest>
intent-filter必须分成两段写,如果合在一起写,就启动不了了。
为了简便,直接把新浪sample里的oauthconstant类拷过来:
package weibo4android.androidexamples; import weibo4android.weibo; import weibo4android.http.accesstoken; import weibo4android.http.requesttoken; public class oauthconstant { private static weibo weibo=null; private static oauthconstant instance=null; private requesttoken requesttoken; private accesstoken accesstoken; private string token; private string tokensecret; private oauthconstant(){}; public static synchronized oauthconstant getinstance(){ if(instance==null) instance= new oauthconstant(); return instance; } public weibo getweibo(){ if(weibo==null) weibo= new weibo(); return weibo; } public accesstoken getaccesstoken() { return accesstoken; } public void setaccesstoken(accesstoken accesstoken) { this.accesstoken = accesstoken; this.token=accesstoken.gettoken(); this.tokensecret=accesstoken.gettokensecret(); } public requesttoken getrequesttoken() { return requesttoken; } public void setrequesttoken(requesttoken requesttoken) { this.requesttoken = requesttoken; } public string gettoken() { return token; } public void settoken(string token) { this.token = token; } public string gettokensecret() { return tokensecret; } public void settokensecret(string tokensecret) { this.tokensecret = tokensecret; } }
接下来就是最关键的主程序:
package com.pocketdigi.weibo; import java.io.file; import weibo4android.weibo; import weibo4android.weiboexception; import weibo4android.http.accesstoken; import weibo4android.http.requesttoken; import android.app.activity; import android.content.intent; import android.content.sharedpreferences; import android.content.sharedpreferences.editor; import android.net.uri; import android.os.bundle; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.edittext; import android.widget.toast; public class main extends activity { /** called when the activity is first created. */ string key = "", secret = ""; button login,send; edittext status; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); system.setproperty("weibo4j.oauth.consumerkey", "3997936609"); system.setproperty("weibo4j.oauth.consumersecret", "8bc9e3bfd6ae8e3b2b8bda9079918950"); //设置在新浪应用开放平台申请的应用的key和secret login=(button)findviewbyid(r.id.login); send=(button)findviewbyid(r.id.send); status=(edittext)findviewbyid(r.id.status); login.setonclicklistener(new onclicklistener(){ @override public void onclick(view v) { // todo auto-generated method stub login(); //登录 }}); send.setonclicklistener(new onclicklistener(){ @override public void onclick(view v) { // todo auto-generated method stub string text=string.valueof(status.gettext()); weibo weibo = new weibo(); weibo.settoken(key,secret); try { //weibo.updatestatus(text); //只发文字 file f=new file("/sdcard/wallpaper/129567208597069400.jpg"); weibo.uploadstatus(text,f ); //发文字+图片,这里需要导入commons-httpclient-3.0.1.jar,自己网上下 //在实际项目上,最好放thread里,因为按下去的时候按钮会卡 } catch (weiboexception e) { // todo auto-generated catch block e.printstacktrace(); } }}); } @override protected void onstart() { // todo auto-generated method stub super.onstart(); //启动时执行检测是否来自网页登录返回 //如果是,获取key和secret //否则读取sharedpreferences //若得不到key和secret,直接跳转登录 uri uri = this.getintent().getdata(); if (uri != null) { //如果是浏览器返回 try { requesttoken requesttoken = oauthconstant.getinstance() .getrequesttoken(); accesstoken accesstoken = requesttoken.getaccesstoken(uri .getqueryparameter("oauth_verifier")); oauthconstant.getinstance().setaccesstoken(accesstoken); // 保存 editor sharedata = getsharedpreferences("weibo", 0).edit(); sharedata.putstring("key", accesstoken.gettoken()); sharedata.putstring("secret", accesstoken.gettokensecret()); sharedata.commit(); key = accesstoken.gettoken(); secret = accesstoken.gettokensecret(); } catch (weiboexception e) { e.printstacktrace(); } } else { //如果是用户自己启动 sharedpreferences settings = getsharedpreferences("weibo", 0); key = settings.getstring("key", ""); secret = settings.getstring("secret", ""); } if (key.equals("") || secret.equals("")) { toast.maketext(this, "尚未登录", toast.length_long).show(); login(); //跳转到浏览器登录 } } public void login(){ weibo weibo = oauthconstant.getinstance().getweibo(); requesttoken requesttoken; try { requesttoken =weibo.getoauthrequesttoken("sina://weibo"); //为了避免与同类应用冲突,还是自己改下uri吧 uri uri2 = uri.parse(requesttoken.getauthenticationurl()+ "&from=xweibo"); oauthconstant.getinstance().setrequesttoken(requesttoken); startactivity(new intent(intent.action_view, uri2)); } catch (weiboexception e) { e.printstacktrace(); } } }
发图片需要导入commons-httpclient-3.0.1.jar,否则启动报错,当然weibo4android-1.2.0.jar是不可少的
推荐阅读