zoukankan      html  css  js  c++  java
  • 简单的新浪微博OAuth认证实现

    System.setProperty("weibo4j.oauth.consumerKey", Weibo.CONSUMER_KEY);
    System.setProperty("weibo4j.oauth.consumerSecret", Weibo.CONSUMER_SECRET);
     
        Weibo weibo = new Weibo();
        // set callback url, desktop app please set to null
        // http://callback_url?oauth_token=xxx&oauth_verifier=xxx
        //1。根据app key第三方应用向新浪获取requestToken
        RequestToken requestToken = weibo.getOAuthRequestToken();
       
        System.out.println("1.......Got request token成功");
        System.out.println("Request token: "+ requestToken.getToken());
        System.out.println("Request token secret: "+ requestToken.getTokenSecret());
        AccessToken accessToken = null;
       //2。用户从新浪获取verifier_code 如果是Android或Iphone应用可以callback =json&userId=xxs&password=XXX
        System.out.println("Open the following URL and grant access to your account:");
        System.out.println(requestToken.getAuthorizationURL());
        BareBonesBrowserLaunch.openURL(requestToken.getAuthorizationURL());
       //3。用户输入验证码授权信任第三方应用
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        while (null == accessToken) {
    System.out.print("Hit enter when it's done.[Enter]:");
    String pin = br.readLine();
    System.out.println("pin: " + br.toString());
    try{
    //4。通过传递requestToken和用户验证码获取AccessToken
       accessToken = requestToken.getAccessToken(pin);
    } catch (WeiboException te) {
       if(401 == te.getStatusCode()){
    System.out.println("Unable to get the access token.");
       }else{
    te.printStackTrace();
       }
    }
        }
       
        System.out.println("Got access token.");
        System.out.println("Access token: "+ accessToken.getToken());
        System.out.println("Access token secret: "+ accessToken.getTokenSecret());
       //使用AccessToken来操作用户的所有接口
       /* Weibo weibo=new Weibo();
     
        以后就可以用下面accessToken访问用户的资料了
        * weibo.setToken(accessToken.getToken(), accessToken.getTokenSecret());
        //发布微博
    Status status = weibo.updateStatus("test message6 ");
    System.out.println("Successfully updated the status to ["
    + status.getText() + "].");
       
        try {
    Thread.sleep(3000);
    } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }*/
        System.exit(0);
    } catch (WeiboException te) {
        System.out.println("Failed to get timeline: " + te.getMessage());
        System.exit( -1);
    } catch (IOException ioe) {
        System.out.println("Failed to read the system input.");
        System.exit( -1);
    }
     
  • 相关阅读:
    Spring配置文件中关于Shiro的配置
    关于Realm的源码分析
    配置SpringMVC配置
    Shiro的登录验证及授权多Realm情况【基于SpringMVC框架下】
    关于Nginx配置说明
    Hibernate中No row with the given identifier exists问题的原因及解决
    关于Shiro的角色授权
    关于Shiro的认证策略
    关于hibernate的cache总结及并发问题
    Shiro多Realm数据交互实现过程
  • 原文地址:https://www.cnblogs.com/xiaochao1234/p/3707406.html
Copyright © 2011-2022 走看看