下载SDK
https://github.com/sinaweibosdk/weibo_android_sdk
获取欲接入应用的签名
使用sdk中 app_signatures.apk 获取应用的签名
在微博开放平台注册应用
http://open.weibo.com/
注册时需要提供应用的签名
在项目中导入weiboSDK
- 添加权限
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<!-- 用于调用 JNI -->
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
- 添加授权页activity
<!-- 必须注册在微博授权,分享微博时候用到 -->
<activity android:name="com.sina.weibo.sdk.component.WeiboSdkBrowser"
android:configChanges="keyboardHidden|orientation"
android:windowSoftInputMode="adjustResize"
android:exported="false" >
</activity>
- 获取token
token是服务器标识用户的方式,获取token后才能进行发微博等操作
mAccessToken = AccessTokenKeeper.readAccessToken(MainActivity.this);
if (!mAccessToken.isSessionValid()) {
AuthInfo authInfo = new AuthInfo(this, Constants.APP_KEY, Constants.REDIRECT_URL, Constants.SCOPE);
mSsoHandler = new SsoHandler(this, authInfo);
mSsoHandler.authorize(new WeiboAuthListener() {
@Override
public void onComplete(Bundle bundle) {
Oauth2AccessToken accessToken = Oauth2AccessToken.parseAccessToken(bundle);
if (accessToken.isSessionValid()) {
AccessTokenKeeper.writeAccessToken(MainActivity.this, accessToken);
Log.d("Oauth2AccessToken", accessToken.toString());
} else {
Log.w("Oauth2AccessToken", "session is empty");
}
}
@Override
public void onWeiboException(WeiboException e) {
Toast.makeText(MainActivity.this,
"Auth exception : " + e.getMessage(), Toast.LENGTH_LONG).show();
}
@Override
public void onCancel() {
Toast.makeText(MainActivity.this,
"请登录", Toast.LENGTH_LONG).show();
}
});
}
AccessTokenKeeper
是SDK中带的保存token的类,使用SharedPreferences
存储
网友评论