Android 微博登录

最近公司有要求接入微博的登录,这里记录下最新的接入方法, 

一 、首先你要自己去微博平台申请好  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中添加权限

 <uses-permission android:name="android.permission.INTERNET" />
  
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

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<String, Object> map = new HashMap<>();
                map.put("access_token", token.getAccessToken());
                map.put("uid", token.getUid());
                Set<Map.Entry<String, Object>> entries = map.entrySet();
                StringBuffer absUrl = new StringBuffer();
                int size = 0;
                for (Map.Entry<String, Object> 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": "<a href="http://weibo.com" rel="nofollow">新浪微博</a>",
        "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	用户UID
idstr	              string	字符串型的用户UID
screen_name	          string	用户昵称
name	              string	友好显示名称
province	          int	用户所在省级ID
city	              int	用户所在城市ID
location	          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	用户备注信息,只有在查询用户关系时才返回此字段
status	object	用户的最近一条微博信息字段 详细
allow_all_comment	boolean	是否允许所有人对我的微博进行评论,true:是,false:否
avatar_large	string	用户头像地址(大图),180×180像素
avatar_hd	string	用户头像地址(高清),高清头像原图
verified_reason	string	认证原因
follow_me	boolean	该用户是否关注当前登录用户,true:是,false:否
online_status	int	用户的在线状态,0:不在线、1:在线
bi_followers_count	int	用户的互粉数
lang	string	用户当前的语言版本,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

 

 

 

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值