首页 > 代码库 > 微博授权登陆
微博授权登陆
微博使用AS会找不到jar包 需要将so文件放在main下jniLibs目录下
从下载的sdk中复制
AbsOpenAPI,
AccessTokenKeeper,
UsersAPI
.java文件
自定义userInfo接收用户信息
/**
* Created by Administrator on 2016/10/3.
*/
public class SinaUserInfo {
private String uid;//用户ID
private String name;//用户昵称
private String avatarHd;//用户头像url
public SinaUserInfo() {
super();
// TODO Auto-generated constructor stub
}
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAvatarHd() {
return avatarHd;
}
public void setAvatarHd(String avatarHd) {
this.avatarHd = avatarHd;
}
@Override
public String toString() {
return "SinaUserInfo{" +
"uid=‘" + uid + ‘\‘‘ +
", name=‘" + name + ‘\‘‘ +
", avatarHd=‘" + avatarHd + ‘\‘‘ +
‘}‘;
}
}
授权页面
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;
import com.sina.weibo.sdk.api.share.IWeiboShareAPI;
import com.sina.weibo.sdk.api.share.WeiboShareSDK;
import com.sina.weibo.sdk.auth.AuthInfo;
import com.sina.weibo.sdk.auth.Oauth2AccessToken;
import com.sina.weibo.sdk.auth.WeiboAuthListener;
import com.sina.weibo.sdk.auth.sso.SsoHandler;
import com.sina.weibo.sdk.exception.WeiboException;
import com.sina.weibo.sdk.net.RequestListener;
import org.json.JSONException;
import org.json.JSONObject;
import seal.yousheng.com.loginlist.sina.AccessTokenKeeper;
import seal.yousheng.com.loginlist.sina.SinaUserInfo;
import seal.yousheng.com.loginlist.sina.UsersAPI;
public class SinalActivity extends AppCompatActivity {
private Context context;
private IWeiboShareAPI weiboShareAPI;
private SsoHandler ssoHandler;
private AuthInfo authInfo;
private Oauth2AccessToken accessToken;
private SinaUserInfo userInfo;
private String id;
public static final String SWB_REDIRECT_URL = "https://api.weibo.com/oauth2/default.html";//新浪微博回调页面
public static final String SWB_SCOPE = "email,direct_messages_read,direct_messages_write,"
+ "friendships_groups_read,friendships_groups_write,statuses_to_me_read,"
+ "follow_app_official_microblog," + "invitation_write";
public Handler handler = new Handler(){
public void handleMessage(Message msg) {
if(msg.what == 1){
info.setText("用户id:"+userInfo.getUid()+"\n"+"用户名:"+userInfo.getName()+"\n"+"用户头像url:"+userInfo.getAvatarHd());
Toast.makeText(SinalActivity.this,userInfo.toString(),Toast.LENGTH_SHORT).show();
}
}
};
private TextView info;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sinal);
context = this;
id=Constants.SINAAPPID;
//初始化
weiboShareAPI = WeiboShareSDK.createWeiboAPI(context, id);
info = (TextView) findViewById(R.id.userInfo);
loginSina();
}
private void loginSina() {
authInfo = new AuthInfo(context, id, SWB_REDIRECT_URL, null);
ssoHandler = new SsoHandler(SinalActivity.this, authInfo);
ssoHandler.authorize(new AuthListener());
}
public class AuthListener implements WeiboAuthListener {
@Override
public void onComplete(Bundle bundle) {
accessToken = Oauth2AccessToken.parseAccessToken(bundle); // 从Bundle中解析Token
String phoneNum = accessToken.getPhoneNum();// 从这里获取用户输入的 电话号码信息
if (accessToken.isSessionValid()) {
AccessTokenKeeper.writeAccessToken(SinalActivity.this, accessToken); // 保存Token
Toast.makeText(context, "授权成功", Toast.LENGTH_SHORT)
.show();
} else {
// 以下几种情况,您会收到 Code:
// 1. 当您未在平台上注册的应用程序的包名与签名时;
// 2. 当您注册的应用程序包名与签名不正确时;
// 3. 当您在平台上注册的包名和签名与您当前测试的应用的包名和签名不匹配时。
String code = bundle.getString("code");
String message = "授权失败";
if (!TextUtils.isEmpty(code)) {
message = message + "\nObtained the code: " + code;
}
Toast.makeText(context, message, Toast.LENGTH_LONG)
.show();
}
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
UsersAPI usersAPI = new UsersAPI(context, id, accessToken);
usersAPI.show(Long.valueOf(accessToken.getUid()), new SinaRequestListener());
}
}).start();
}
@Override
public void onWeiboException(WeiboException e) {
Toast.makeText(context, "Auth exception : " + e.getMessage(),
Toast.LENGTH_LONG).show();
}
@Override
public void onCancel() {
// TODO Auto-generated method stub
Toast.makeText(context, "授权取消", Toast.LENGTH_LONG).show();
}
}
public class SinaRequestListener implements RequestListener { //新浪微博请求接口
@Override
public void onComplete(String response) {
// TODO Auto-generated method stub
try {
JSONObject jsonObject = new JSONObject(response);
String idStr = jsonObject.getString("idstr");// 唯一标识符(uid)
String name = jsonObject.getString("name");// 姓名
String avatarHd = jsonObject.getString("avatar_hd");// 头像
userInfo = new SinaUserInfo();
userInfo.setUid(idStr);
userInfo.setName(name);
userInfo.setAvatarHd(avatarHd);
Message message = Message.obtain();
message.what = 1;
handler.sendMessage(message);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void onWeiboException(WeiboException e) {
Toast.makeText(context, "Auth exception : " + e.getMessage(),
Toast.LENGTH_LONG).show();
Log.i("mylog", "Auth exception : " + e.getMessage());
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(ssoHandler != null){
ssoHandler.authorizeCallBack(requestCode, resultCode, data);
}
}
}
配置文件
<!--新浪授权登陆-->
<activity
android:name="com.sina.weibo.sdk.component.WeiboSdkBrowser"
android:configChanges="keyboardHidden|orientation"
android:exported="false"
android:windowSoftInputMode="adjustResize" >
</activity>
微博授权登陆
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。