新浪微博webView方式授权获取不到accessToken

做了几个新浪分享的项目,昨天遇到了获取不到accessToken,而是授权成功只返回个code,这让我比较纠结,看了新浪官方说需要用code值算accessToken,又看了官方的demo,里面写个暂时不提供code算法,让我很郁闷,又搜了下新浪提供了接口可以通过code取accessToken,先把代码粘上:(多余的就不粘了,只贴关键部分)

/* 新浪微博授权成功回调方法 /

class AuthDialogListener implements WeiboAuthListener {

    @Override
    public void onComplete(Bundle values) {
        final String code = values.getString("code");
        token = values.getString("access_token");
        expires_in = values.getString("expires_in");
        if(TommyTools.isNull(token)){
            accessToken = new Oauth2AccessToken(token, expires_in);
            if (accessToken.isSessionValid()) {
                AccessTokenKeeper.keepAccessToken(activity, accessToken);
                Toast.makeText(activity, "认证成功", Toast.LENGTH_SHORT).show();
                                }
        }else if (code != null) {//下面是通过code取accessToken
            new Thread(){

                @Override
                public void run() {
                    String result = "";  
                    WeiboParameters params = new WeiboParameters();
                    params.add("client_id", Constant.APP_KEY);
                    params.add("client_secret", "1f39c10940fa0d3b1001e439c4a99ccb");
                    params.add("grant_type", "authorization_code");
                    params.add("redirect_uri", Constant.REDIRECT_URL);
                    params.add("code", code);
                    try {
                        result = HttpManager.openUrl("https://api.weibo.com/oauth2/access_token", "POST",
                                params, null);
                    } catch (WeiboException e) {
                        e.printStackTrace();
                    }
                    JSONObject json;
                    try {
                        json = new JSONObject(result);
                        token = json.getString("access_token");
                        expires_in = json.getString("expires_in");
                        handler.sendEmptyMessage(1);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

            }.start();
        }else{
            return;
        }
    }

    @Override
    public void onError(WeiboDialogError e) {
        Toast.makeText(activity, "Auth error : " + e.getMessage(), Toast.LENGTH_LONG).show();
    }

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

    @Override
    public void onWeiboException(WeiboException arg0) {

    }

}

Handler handler = new Handler(){

    @Override
    public void handleMessage(Message msg) {
        if(msg.what == 1){
            if(TommyTools.isNull(token) && TommyTools.isNull(expires_in)){
                accessToken = new Oauth2AccessToken(token, expires_in);
                if (accessToken.isSessionValid()) {
                    AccessTokenKeeper.keepAccessToken(activity, accessToken);
                    Toast.makeText(activity, "认证成功", Toast.LENGTH_SHORT).show();
                }
            }
        }
    }

};

可以看到,我在授权成功回调那里做了个判断,先判断token为不为空,直接说原因,是因为在申请appKey时android需要填写包名和应用签名,如果打的包用的签名和填写在上面的一致,授权成功会直接返回token,不需要在调接口获取,如果签名不一致,授权就只返回一个code码,然后通过code码再获取token。

你可能感兴趣的:(新浪授权,获取不到accessToken)