zoukankan      html  css  js  c++  java
  • 愿闻其翔记(二)

    今天终于把微博的SSO全部捣鼓出来了。

    搜索了各种文章,官方文档,都没用啊都没用。

    其实整套流程是这样的。

    首先通过Oauth2AccessToken的isSessionValid来判断当前用户的授权是否过期,没过期的话就可以继续使用token。

    如果过期的话就重新走一遍授权流程。

    然后,我们说说第一次使用和授权过期的流程。

    按照官方的文档,实例化Weibo对象,创建SsoHandler。

    使用SsoHandler来进行认证,实现一个WeiboAuthListener来接受认证回调。

    在WeiboAuthListener的实例化类里面Override onComplete方法

    这个方法是获得一个认证的code,需要凭此code去获取token。

    所以大概可以写成这个样子。

    @Override
            public void onComplete(Bundle values) {
    
                authCode = values.getString("code");
                if (authCode != null) {
                    new Thread(getToken).start();
    
                    return;
                } else {
                    Toast.makeText(LoginChoice.this, "授权失败", Toast.LENGTH_SHORT)
                            .show();
                }
            }

    authCode为当前Activity的属性。

    当code不为空的时候说明接下来就要去获取token,但是获取token这个事儿官方的demo里面没有。。

    代码是在开放平台的Q&A中找到的。

    因为4.0不能在主程序中建立网络链接,所以这个地方需要创建一个线程,来做网络请求

    Runnable getToken = new Runnable() {
                @Override
                public void run() {
                    WeiboParameters params = new WeiboParameters();
                    params.add("client_id", ConstantS.CLIENT_ID); //在新浪微博申请的应用的appKey
                    params.add("client_secret", ConstantS.CLIENT_SECRET); //应用的 app secret
                    params.add("grant_type", "authorization_code");
                    params.add("forcelogin", "false");
                    params.add("redirect_uri", ConstantS.REDIRECT_URL); //由于是手机端所以回调页面只要和开放平台中设置的一直就行了
                    params.add("code", authCode);
                    String result = "";
                    try {
                        result = HttpManager
                                .openUrl(
                                        "https://api.weibo.com/oauth2/access_token",
                                        "POST", params, null);
                        JSONObject json = new JSONObject(result);
    
                        String token = json.getString("access_token");
                        String expires_in = json.getString("expires_in");
    
                        someActivity.tokenKeeper = new Oauth2AccessToken(token,
                                expires_in);
    if (someActivity.tokenKeeper.isSessionValid()) {
    AccessTokenKeeper.keepAccessToken(
    this, //按照实际情况替换 someActivity.tokenKeeper);
                  //跳换Activity Intent intent
    = new Intent(); intent.setClass(this, targetActivity.class); //按照实际情况替换这两个值 startActivity(intent); this.finish(); } } catch (WeiboException e) { e.printStackTrace(); } catch (JSONException e) { e.printStackTrace(); } } };

    这样就能完成,token的获取了。

    并且把token和过期时间通过AccessTokenKeeper存储了。

    这样在进入app的时候,只要判断token是否isSessionValid()就行了。

    取消授权

    在开发测试的时候,要来回的授权和取消授权。

    取消授权也是需要发送一个网络请求,所以也需要新开一个线程来完成这个事。

    Runnable logout = new Runnable(){
            @Override
            public void run(){
           //调用取消授权的接口 HttpGet httpGet
    = new HttpGet("https://api.weibo.com/oauth2/revokeoauth2?access_token="+EnterActivity.tokenKeeper.getToken());
    HttpClient httpClient
    = new DefaultHttpClient(); InputStream inputStream = null; // 响应 HttpResponse mHttpResponse = null; // 实体 HttpEntity mHttpEntity = null; try { // 发送请求并获得响应对象 mHttpResponse = httpClient.execute(httpGet); // 获得响应的消息实体 mHttpEntity = mHttpResponse.getEntity(); // 获取一个输入流 inputStream = mHttpEntity.getContent(); BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream)); String result = ""; String line = ""; while (null != (line = bufferedReader.readLine())) { result += line; } AccessTokenKeeper.clear(getApplicationContext()); //清除tokenKeeper的数据
    } catch (Exception e) { e.printStackTrace(); } finally { try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } };

    至此,基本的大概流程就完成了。

  • 相关阅读:
    1029 旧键盘 (20 分)C、Java、python
    1028 人口普查 (20 分)C语言
    1027 打印沙漏 (20 分)C语言
    luoguP2709 小B的询问
    luoguP2709 小B的询问
    bzoj5016 [Snoi2017]一个简单的询问
    bzoj5016 [Snoi2017]一个简单的询问
    luoguP1972 [SDOI2009]HH的项链(莫队)
    luoguP1972 [SDOI2009]HH的项链(莫队)
    Tyvj1091
  • 原文地址:https://www.cnblogs.com/leftice/p/3318972.html
Copyright © 2011-2022 走看看