Android(安卓)微博登录
16lz
2021-12-04
最近公司有要求接入微博的登录,这里记录下最新的接入方法,
一 、首先你要自己去微博平台申请好 appkey 、
二、Androidstudio 集成
1.在Project的build.gradle文件中添加依赖配置:
allprojects { repositories { maven { url 'https://dl.bintray.com/thelasterstar/maven/' } } }
2.在Module的build.gradle文件中添加依赖和属性配置
android { defaultConfig { ndk {// ᦡᗝඪ೮ጱSOପຝ abiFilters 'armeabi' //, 'armeabi-v7a','arm64-v8a' } } } dependencies { compile 'com.sina.weibo.sdk:core:9.12.0:openDefaultRelease@aar' }
3.在AndroidManifest.xml中添加权限
4.在Proguard混淆文件中增加以下配置:
-keep public class com.sina.weibo.sdk.**{*;}
三、使用SDK
//微博 public static String WeiB_appkey = "您自己的appkey"; /** * 当前 DEMO 应用的回调页,第三方应用可以使用自己的回调页。 * 建议使用默认回调页:https://api.weibo.com/oauth2/default.html */ public static final String REDIRECT_URL = "http://www.sina.com"; /** * WeiboSDKDemo 应用对应的权限,第三方开发者一般不需要这么多,可直接设置成空即可。 * 详情请查看 Demo 中对应的注释。 */ public static final String SCOPE = "email,direct_messages_read,direct_messages_write," + "friendships_groups_read,friendships_groups_write,statuses_to_me_read," + "follow_app_official_microblog," + "invitation_write";
/** * 微博登录 */ private void WeiBLogin() { AuthInfo authInfo = new AuthInfo(this, WeiB_appkey, REDIRECT_URL, SCOPE); mWBAPI = WBAPIFactory.createWBAPI(this); mWBAPI.registerApp(this, authInfo); mWBAPI.authorizeClient(new WbAuthListener() { @Override public void onComplete(Oauth2AccessToken token) { //调用微博的用户请求地址,获取用户信息,新版的微博,用户信息将不再直接返回,必须调用以下的接口才能返回用户的详细信息 https://api.weibo.com/2/users/show.json Map map = new HashMap<>(); map.put("access_token", token.getAccessToken()); map.put("uid", token.getUid()); Set> entries = map.entrySet(); StringBuffer absUrl = new StringBuffer(); int size = 0; for (Map.Entry item : entries) { if (size == 0) { absUrl.append("https://api.weibo.com/2/users/show.json").append("?").append(item.getKey()).append("=").append(item.getValue()); size++; } else { absUrl.append("&").append(item.getKey()).append("=").append(item.getValue()); } } OkHttpClient client = new OkHttpClient(); Request request = new Request.Builder() .url(absUrl.toString()) .build(); client.newCall(request).enqueue(new okhttp3.Callback() { @Override public void onFailure(Call call, IOException e) { } @Override public void onResponse(Call call, Response response) throws IOException { if (response.isSuccessful()) { String result = response.body().string(); if (result == null) { return; } WeiBEntity weib = new Gson().fromJson(result, WeiBEntity.class); if (weib == null) { return; } userInfoT = new UserInfoT(); if (weib.getScreen_name() != null && !weib.getScreen_name().isEmpty()) { userInfoT.setNickname(userInfoT.getNickname()); } if (weib.getAvatar_hd() != null && !weib.getAvatar_hd().isEmpty()) { userInfoT.setHeadurl(weib.getAvatar_hd()); } if (weib.getGender() != null && !weib.getGender().isEmpty()) { //性别,m:男、f:女、n:未知 if (weib.getGender().equals("m")) { userInfoT.setGender(1); } else if (weib.getGender().equals("f")) { userInfoT.setGender(2); } else if (weib.getGender().equals("n")) { userInfoT.setGender(1); } } if (weib.getDescription() != null && !weib.getDescription().isEmpty()) { userInfoT.setDistrict(weib.getDescription()); } // todo 上传数据 这里是将第三方的平台的用户信息传递给后台, } @Override protected void onError(Call call, int statusCode, String errMessage) { errMessage.toString(); } }); } } }); } @Override public void onError(UiError error) { Toast.makeText(LoginActivity.this, "微博授权出错", Toast.LENGTH_SHORT).show(); } @Override public void onCancel() { Toast.makeText(LoginActivity.this, "微博授权取消", Toast.LENGTH_SHORT).show(); } }); }
微博用户接口返回json 示例
{ "id": 1404376560, "screen_name": "zaku", "name": "zaku", "province": "11", "city": "5", "location": "北京 朝阳区", "description": "人生五十年,乃如梦如幻;有生斯有死,壮士复何憾。", "url": "http://blog.sina.com.cn/zaku", "profile_image_url": "http://tp1.sinaimg.cn/1404376560/50/0/1", "domain": "zaku", "gender": "m", "followers_count": 1204, "friends_count": 447, "statuses_count": 2908, "favourites_count": 0, "created_at": "Fri Aug 28 00:00:00 +0800 2009", "following": false, "allow_all_act_msg": false, "geo_enabled": true, "verified": false, "status": { "created_at": "Tue May 24 18:04:53 +0800 2011", "id": 11142488790, "text": "我的相机到了。", "source": "新浪微博", "favorited": false, "truncated": false, "in_reply_to_status_id": "", "in_reply_to_user_id": "", "in_reply_to_screen_name": "", "geo": null, "mid": "5610221544300749636", "annotations": [], "reposts_count": 5, "comments_count": 8 }, "allow_all_comment": true, "avatar_large": "http://tp1.sinaimg.cn/1404376560/180/0/1", "verified_reason": "", "follow_me": false, "online_status": 0, "bi_followers_count": 215}
返回字段说明
返回值字段字段类型字段说明id int64用户UIDidstr string字符串型的用户UIDscreen_name string用户昵称name string友好显示名称province int用户所在省级IDcity int用户所在城市IDlocation string用户所在地description string用户个人描述url string用户博客地址profile_image_url string用户头像地址(中图),50×50像素profile_url string用户的微博统一URL地址domain string用户的个性化域名weihao string用户的微号gender string性别,m:男、f:女、n:未知followers_count int粉丝数friends_count int关注数statuses_count int微博数favourites_count int收藏数created_at string用户创建(注册)时间following boolean暂未支持allow_all_act_msg boolean是否允许所有人给我发私信,true:是,false:否geo_enabled boolean是否允许标识用户的地理位置,true:是,false:否verified boolean是否是微博认证用户,即加V用户,true:是,false:否verified_type int暂未支持remark string用户备注信息,只有在查询用户关系时才返回此字段statusobject用户的最近一条微博信息字段 详细allow_all_commentboolean是否允许所有人对我的微博进行评论,true:是,false:否avatar_largestring用户头像地址(大图),180×180像素avatar_hdstring用户头像地址(高清),高清头像原图verified_reasonstring认证原因follow_meboolean该用户是否关注当前登录用户,true:是,false:否online_statusint用户的在线状态,0:不在线、1:在线bi_followers_countint用户的互粉数langstring用户当前的语言版本,zh-cn:简体中文,zh-tw:繁体中文,en:英语
注意:1.如果您的App需要上传到 Google play store , 您需要将READ_PHONE_STATE权限屏蔽掉或者移除,否则可能会被下架
2. REDIRECT_RUL 的地址 必须跟平台上填写的一致哦,否则会提示 error :redirect_uri_mismatch
更多问题请在这里查阅哦:https://open.weibo.com/wiki/%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98
更多相关文章
- Android(安卓)12 预览版发布,64g 手机用户哭了
- Google 告诉你 Android(安卓)4.0 的新功能 [视频]
- Android的Task和Activity(一)
- Android省市区3级联动的地址列表的实现
- Android中读取电话本Contacts联系人的所有电话号信息
- android IP getIP
- 《Android(安卓)Dev Guide》系列教程8:用户界面
- Android(安卓)Studio查看应用数字签名-android学习之旅(76)
- Android优秀开源项目收集