微博登陆
/**
微博登陆的回调类,通过联网获取用户数据
*/
public class WeiBoLogin implements WeiboAuthListener{
private Activity activity;
public static Oauth2AccessToken accessToken;
String imei,imsi;
public WeiBoLogin(Activity activity,String imei,String imsi) {
super();
this.activity = activity;
this.imei=imei;
this.imsi=imsi;
}
private void recordUser(final String access_token, final String uid,
final String screen_name) {
AsyncRunner.HttpGet(new BaseRequestListener() {
@Override
public void onReading() {
super.onReading();
}
@Override
public void onRequesting() throws BarfooError, JSONException {
super.onRequesting();
JSONObject jsonObject=new JSONObject();
Bundle bundle = new Bundle();
bundle.putString("source", Constants.APP_KEY);
bundle.putString("access_token", access_token);
bundle.putString("uid", uid);
JSONObject jsonArray = BaseHttp.httpGet(BaseHttp.build_api(
"https://api.weibo.com/2/users/show.json", bundle));
MyApplication.writeWeibo(jsonArray.getString("id"),
jsonArray.getString("screen_name"),
jsonArray.getString("profile_image_url"),
jsonArray.getString("description"));
}
@Override
public void onBarfooError(BarfooError e) {
super.onBarfooError(e);
}
@Override
public void onDone() {
super.onDone();
}
});
}
@Override
public void onCancel() {
Toast.makeText(activity, "取消授权", Toast.LENGTH_SHORT)
.show();
}
@Override
public void onComplete(Bundle values) {
Toast.makeText(activity, "登陆成功", 0).show();
accessToken = Oauth2AccessToken.parseAccessToken(values);
String token = values.getString("access_token");
String expires_in = values.getString("expires_in");
final String idstr = values.getString("uid");
final String username = values.getString("userName");
accessToken = new Oauth2AccessToken(token, expires_in);
recordUser(token, idstr, username);
UserCenterUtil.login("", "", imei, imsi, idstr, "3", activity);
if (accessToken.isSessionValid()) {
String date = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss")
.format(new java.util.Date(accessToken.getExpiresTime()));
AccessTokenKeeper.writeAccessToken(activity,
accessToken);
} else {
String code = values.getString("code");
}
}
@Override
public void onWeiboException(WeiboException arg0) {
Toast.makeText(activity, "登录失败", Toast.LENGTH_SHORT)
.show();
}
}
//微博登陆唤起登陆页面 ,sso授权
WeiboAuth weiboAuth = new WeiboAuth(LoginActivity.this,
Constants.APP_KEY, Constants.REDIRECT_URL, Constants.SCOPE);
mSsoHandler = new SsoHandler(LoginActivity.this, weiboAuth);
mSsoHandler.authorize(new WeiBoLogin(LoginActivity.this,imei,imsi));
//微博分享
// 微博分享
public static void sendMessageWb(boolean hasText, boolean hasImage,
Activity activity, IWeiboShareAPI mWeiboShareAPI, Bitmap bitmap) {
WeiboMultiMessage weiboMessage = new WeiboMultiMessage();// 初始化微博的分享消息
if (hasText) {
weiboMessage.textObject = getTextObj(activity);
}
if (hasImage) {
weiboMessage.imageObject = new ImageObject();
weiboMessage.imageObject.setImageObject(bitmap);
}
SendMultiMessageToWeiboRequest request = new SendMultiMessageToWeiboRequest();
request.transaction = String.valueOf(System.currentTimeMillis());
request.multiMessage = weiboMessage;
mWeiboShareAPI.sendRequest(request); // 发送请求消息到微博,唤起微博分享界面
}