安卓客户端微博登陆(文件不存在 C8998)

第三方微博登陆文档说明还算比较好,坑也比较少。有可能遇到的是:文件不存在 C8998 的解决方法

新浪微博开放平台地址:

http://open.weibo.com

打开网站,选择移动应用。然后创建自己的移动应用。和申请微信登陆差不多,首先填写应用名称,自己的应用名称和别人的一样也是不能正常使用,需要换一个名称。

然后进入申请详情页面,填写应用包名和应用签名,如图


填写一个就行了,获得签名方法在微信登陆里有介绍。然后提交审核。审核通过后下载 sdk,新浪把SDK 及Demo放到github 上了,有时候下载比较慢。

先在Manifest中申请权限

  <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    <uses-permission android:name="android.permission.CHANGE_WIFI_MULTICAST_STATE" />

然后把相应Activit 添加进去

 <!-- 必须注册在微博授权,分享微博时候用到 -->
        <activity android:name="com.sina.weibo.sdk.component.WeiboSdkBrowser"
            android:configChanges="keyboardHidden|orientation"
            android:windowSoftInputMode="adjustResize"
            android:exported="false" >
        </activity>

        <!-- 手机短信注册页面 -->
        <activity android:name="com.sina.weibo.sdk.register.mobile.MobileRegisterActivity"
            android:configChanges="keyboardHidden|orientation"
            android:screenOrientation="portrait"
            android:windowSoftInputMode="adjustResize"
            android:exported="false" >
        </activity>

        <!-- 注册选择国家页面 -->
        <activity android:name="com.sina.weibo.sdk.register.mobile.SelectCountryActivity"
            android:configChanges="keyboardHidden|orientation"
            android:windowSoftInputMode="adjustResize"
            android:exported="false" >
        </activity>




进行微博授权:

    public final  static  String APPID_WEIBO ="2096726876";
    public final  static  String REDIRECT_URL ="http://www.17zhuanba.cn";
    public final  static  String APP_KEY_WEIBO ="2e307b6b94a500ff1b8b910133da53da";


AuthInfo mAuthInfo = new AuthInfo(this, LoginConfig.APPID_WEIBO,
                        LoginConfig.REDIRECT_URL, "all");
                mSsoHandler = new SsoHandler(ActivityLogin.this, mAuthInfo);
                mSsoHandler.authorize(new AuthListener());


  /** 封装了 "access_token","expires_in","refresh_token",并提供了他们的管理功能  */
    private Oauth2AccessToken mAccessToken;
    /** 注意:SsoHandler 仅当 SDK 支持 SSO 时有效 */
    private SsoHandler mSsoHandler;

    /**
     * 微博认证授权回调类。
     * 1. SSO 授权时,需要在 {@link #onActivityResult} 中调用 {@link SsoHandler#authorizeCallBack} 后,
     *    该回调才会被执行。
     * 2. 非 SSO 授权时,当授权结束后,该回调就会被执行。
     * 当授权成功后,请保存该 access_token、expires_in、uid 等信息到 SharedPreferences 中。
     */
    class AuthListener implements WeiboAuthListener {

        @Override
        public void onComplete(Bundle values) {
            // 从 Bundle 中解析 Token
            mAccessToken = Oauth2AccessToken.parseAccessToken(values);
            //从这里获取用户输入的 电话号码信息
            String  phoneNum =  mAccessToken.getPhoneNum();
            if (mAccessToken.isSessionValid()) {
                // 显示 Token
                updateTokenView(false);

                // 保存 Token 到 SharedPreferences
//                AccessTokenKeeper.writeAccessToken(ActivityLogin.this, mAccessToken);
                Toast.makeText(ActivityLogin.this,
                       "success", Toast.LENGTH_SHORT).show();
            } else {
                // 以下几种情况,您会收到 Code:
                // 1. 当您未在平台上注册的应用程序的包名与签名时;
                // 2. 当您注册的应用程序包名与签名不正确时;
                // 3. 当您在平台上注册的包名和签名与您当前测试的应用的包名和签名不匹配时。
                String code = values.getString("code");
                String message = "failed";
                if (!TextUtils.isEmpty(code)) {
                    message = message + "\nObtained the code: " + code;
                }
                Toast.makeText(ActivityLogin.this, message, Toast.LENGTH_LONG).show();
            }
        }

        @Override
        public void onCancel() {
            Toast.makeText(ActivityLogin.this,
                    "cancel", Toast.LENGTH_LONG).show();
        }

        @Override
        public void onWeiboException(WeiboException e) {
            Toast.makeText(ActivityLogin.this,
                    "Auth exception : " + e.getMessage(), Toast.LENGTH_LONG).show();
        }
    }

    /**
     * 显示当前 Token 信息。
     *
     * @param hasExisted 配置文件中是否已存在 token 信息并且合法
     */
    private void updateTokenView(boolean hasExisted) {
        String date = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(
                new java.util.Date(mAccessToken.getExpiresTime()));

        String message = String.format("das", mAccessToken.getToken(), date);
   

    }


在onActivityResult 中进行回调

 @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

 
        // SSO 授权回调
        // 重要:发起 SSO 登陆的 Activity 必须重写 onActivityResults
        if (mSsoHandler != null) {
            mSsoHandler.authorizeCallBack(requestCode, resultCode, data);
        }
    }



授权成功返回的   mAccessToken = Oauth2AccessToken.parseAccessToken(values);  mAccessToken 数据为:


uid: 5614667346, access_token: 2.00k9ZyHG78etRC1d7e5b8f0aLCIHBC, refresh_token: 2.00k9ZyHG78etRCec91c58daet8Bw9C, phone_num: , expires_in: 1629379187343


中间可能出现: (文件不存在 C8998) ,这个可能是后台填写的签名和运行时签名不一致,也可能是后台的 授权回调页和 app上不一样 ,APP 上是这个REDIRECT_URL




通过 mAccessToken.getToken() 可获得 Token

然后再获取微博用户相关个人信息:请求地址: 

https://api.weibo.com/2/users/show.json?access_token=" +
                        mAccessToken.getToken()
                        + "&uid=" +
                        mAccessToken.getUid();


请求方式为 get 方式;请求到的结果如下:

{
    "id":5614667346,
    "idstr":"5614667346",
    "class":1,
    "screen_name":"635",
    "name":"635",
    "province":"11",
    "city":"1",
    "location":"北京 东城区",
    "description":"",
    "url":"",
    "profile_image_url":"http://tva4.sinaimg.cn/crop.0.0.100.100.50/0067YzsKjw8esdxi5brbxj302s02s3yb.jpg",
    "cover_image_phone":"http://ww1.sinaimg.cn/crop.0.0.640.640.640/549d0121tw1egm1kjly3jj20hs0hsq4f.jpg",
    "profile_url":"u/5614667346",
    "domain":"",
    "weihao":"",
    "gender":"m",
    "followers_count":1,
    "friends_count":4,
    "pagefriends_count":0,
    "statuses_count":3,
    "favourites_count":18,
    "created_at":"Sat May 23 09:29:20 +0800 2015",
    "following":false,
    "allow_all_act_msg":false,
    "geo_enabled":true,
    "verified":false,
    "verified_type":-1,
    "remark":"",
    "status":{
        "created_at":"Thu Feb 11 15:06:54 +0800 2016",
        "id":3941349520237219,
        "mid":"3941349520237219",
        "idstr":"3941349520237219",
        "text":"#5吴雨翔的红包#一不小",
        "textLength":166,
        "source_allowclick":0,
        "source_type":1,
        "source":"<a href="http://app.weibo.com/t/feed/380tOv" rel="nofollow">粉丝红包</a>",
        "favorited":false,
        "truncated":false,
        "in_reply_to_status_id":"",
        "in_reply_to_user_id":"",
        "in_reply_to_screen_name":"",
        "pic_urls":[

        ],
        "geo":null,
        "reposts_count":0,
        "comments_count":0,
        "attitudes_count":0,
        "isLongText":false,
        "mlevel":0,
        "visible":{
            "type":0,
            "list_id":0
        },
        "biz_ids":[
            230632
        ],
        "biz_feature":0,
        "page_type":32,
        "hasActionTypeCard":0,
        "darwin_tags":[

        ],
        "hot_weibo_tags":[

        ],
        "text_tag_tips":[

        ],
        "userType":0,
        "positive_recom_flag":0,
        "gif_ids":"",
        "is_show_bulletin":0
    },
    "ptype":0,
    "allow_all_comment":true,
    "avatar_large":"http://tva4.sinaimg.cn/crop.0.0.100.100.180/0067YzsKjw8esdxi5brbxj302s02s3yb.jpg",
    "avatar_hd":"http://tva4.sinaimg.cn/crop.0.0.100.100.1024/0067YzsKjw8esdxi5brbxj302s02s3yb.jpg",
    "verified_reason":"",
    "verified_trade":"",
    "verified_reason_url":"",
    "verified_source":"",
    "verified_source_url":"",
    "follow_me":false,
    "online_status":0,
    "bi_followers_count":0,
    "lang":"zh-cn",
    "star":0,
    "mbtype":0,
    "mbrank":0,
    "block_word":0,
    "block_app":0,
    "credit_score":80,
    "user_ability":0,
    "urank":4
}





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值