码迷,mamicode.com
首页 > 其他好文 > 详细

简单的新浪微博OAuth认证实现

时间:2014-05-09 18:32:05      阅读:312      评论:0      收藏:0      [点我收藏+]

标签:android   des   style   blog   class   code   

bubuko.com,布布扣
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);
}
 
bubuko.com,布布扣

 

简单的新浪微博OAuth认证实现,布布扣,bubuko.com

简单的新浪微博OAuth认证实现

标签:android   des   style   blog   class   code   

原文地址:http://www.cnblogs.com/xiaochao1234/p/3707406.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!