主要类说明:
1. Weibo: 微博API 接口类,对外提供weibo api的调用,包括登录,API调用,微博分享等功能。
2. AsyncWeiboRunner:微博api异步执行类,封装了回调接口,通过创建线程来调用Weibo中的接口方法。
3. Utility:互联网工具类,包括接口请求GET/POST封装,BASE64等encode,decode方法。
4. WeiboException:微博异常封装类,封装了微博的各个异常
很多Android应用会加入分享到微博或人人的功能,今天我们就来看一下用新浪提供的Android SDK写一个发微博的Demo程序。
首先不得不说说关于OAuth授权认证的事情,因为现在的开放平台API如新浪、腾讯、人人SDK都必须在这个基础上才能调用:
OAuth是一种国际通用的授权方式,它的特点是不需要用户在第三方应用输入用户名及密码。OAuth的技术说明可参看官方网站http://oauth.net。
新浪微博通过OAuth建立普通用户、客户端程序、新浪微博三者之间的相互信任关系,让客户端程序不需要知道用户的账号和密码、用户也能浏览、发布微博,这样有效的保护了用户账号的安全性。
结合新浪微博的OAuth认证来说说具体的功能实现,首先罗列一下四组关键字组:
第一组:(App Key和App Secret),就是在新浪开发平台建一个新的应用获取App Key和App Secret
第二组:(Request Token和Request Secret)
第三组:(oauth_verifier)
第四组:(user_id、Access Token和Access Secret)
新浪微博的OAuth认证过程如下,当用户第一次使用客户端软件时,程序用第一组参数向新浪微博发起请求,然后新浪微博经过验证后就返回第二组参数给程序;当程序获取第二组参数时作为参数跳至新浪微博的授权页面(由下面WebView类来实现),然后用户在新浪的这个页面里输入自己的微博账号和密码,完成授权后根据程序设定的回调地址把第三组参数返回,接下程序把第二组参数和第三组参数作为参数再次向新浪微博发起请求,新浪微博返回第四组参数给程序,从此就可以根据第四组参数调用各种API了。
步骤图示如下:
按照OAuth协议规范,输入账号密码之后需要跳转到callbackURL,所以在开发Android应用中, 我们需要自己实现一个WebView类,供OAuth认证时打开Url, Android应用中其实原来是不用实现的,但是当用户认证是选择用UC等第三方的浏览器进行用户认证时,用户输入账号密码后点击授权按钮后不会跳转,只有用Android自带的浏览器才没有问题可,惜是大多数的用户都会用第三方的浏览器,这样导致认证不能正常进行,所以需要自己实现一个WebView,其实也不难,只需一个WebV控件即可。
实际开发中,我们需要一个SinaAccessInfo类,保存用户的user_id、Access Token和Access Secret,微博系统中,OAuth的Access token不会过期,所以可以用Android SQLite保存,程序运行,数据库如果为空,则调用startOAuthView()方法跳到WebView授权页面。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
private
CommonsHttpOAuthConsumer httpOauthConsumer;
private
OAuthProvider httpOauthprovider;
private
final
static
String callBackUrl =
"sinaweibocallbackurl://Sina_SplashActivity"
;
//你申请的应用的API Key
httpOauthConsumer =
new
CommonsHttpOAuthConsumer(
getString(R.string.app_sina_consumer_key),
getString(R.string.app_sina_consumer_secret));
//省略
private
void
startOAuthView() {
try
{
String authUrl = httpOauthprovider.retrieveRequestToken(
httpOauthConsumer, callBackUrl);
Intent intent =
new
Intent();
Bundle bundle =
new
Bundle();
bundle.putString(
"url"
, authUrl);
intent.putExtras(bundle);
intent.setClass(mContext, SinaWebViewActivity.
class
);
startActivity(intent);
}
catch
(Exception e) {
Log.i(
"yilee"
, e.getMessage());
}
}
|
其中:
callBackUrl是指用户在WebView输入账号密码后的回调类。需要在AndroidManifest.xml说明。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
public
class
SinaWebViewActivity
extends
Activity {
private
WebView webView;
//在xml中定义的控件
private
Intent intent =
null
;
public
static
SinaWebViewActivity webInstance =
null
;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.sina_web);
setTitle(
"分享到新浪微博"
);
webInstance =
this
;
webView = (WebView) findViewById(R.id.web);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(
true
);
webSettings.setSaveFormData(
true
);
webSettings.setSavePassword(
true
);
webSettings.setSupportZoom(
true
);
webSettings.setBuiltInZoomControls(
true
);
webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
webView.setOnTouchListener(
new
OnTouchListener() {
@Override
public
boolean
onTouch(View v, MotionEvent event) {
webView.requestFocus();
return
false
;
}
});
intent =
this
.getIntent();
if
(!intent.equals(
null
)) {
Bundle b = intent.getExtras();
if
(b !=
null
&& b.containsKey(
"url"
)) {
webView.loadUrl(b.getString(
"url"
));
webView.setWebChromeClient(
new
WebChromeClient() {
public
void
onProgressChanged(WebView view,
int
progress) {
setTitle(
"稍等片刻,页面加载中…"
+ progress +
"%"
);
setProgress(progress *
100
);
if
(progress ==
100
) {
setTitle(R.string.app_name);
}
}
});
}
}
}
}
|
经过这个步骤,我们已经可以得到user_id、Access Token和Access Secret,便可以用来发表微博了!
1
2
3
4
|
Weibo weibo = OAuthConstant.getInstance().getWeibo();
weibo.setToken(accessToken, accessSecret);
File file = OperateBitmap.readFile(bitmap_name);
Status status=weibo.uploadStatus(msg, file);
|
其中:
accessToken和accessSecret就是通过WebView得到的;
bitmap_name是图片地址。
status是返回的信息,可以知道知否发送成功。
这样便可以成功发一条带有图片的微博了,在此基础上,我们可以继续完善,完成一个新浪微博的Android客户端也不错。。。
Posted on 2011-08-04