首页 > 代码库 > 简单的新浪微博OAuth认证实现

简单的新浪微博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);
}
 

 

<iframe id="google_ads_frame2" vspace="0" height="250" marginHeight="0" src="http://googleads.g.doubleclick.net/pagead/ads?client=ca-pub-3447371224873639&output=html&h=250&slotname=8660799060&adk=1970350646&w=300&lmt=1399271423&flash=0&url=http%3A%2F%2Fwww.cnblogs.com%2Fxiaochao1234%2Fp%2F3707406.html&dt=1399271427341&shv=r20140429&cbv=r20140417&saldr=sb&prev_slotnames=4356862740&correlator=1399271427191&frm=20&ga_vid=429972749.1397695120&ga_sid=1399267813&ga_hid=29020834&ga_fc=1&u_tz=480&u_his=145&u_java=1&u_h=768&u_w=1364&u_ah=740&u_aw=1364&u_cd=16&u_nplug=0&u_nmime=0&dff=verdana&dfs=16&adx=0&ady=1752&biw=314&bih=74&eid=317150304&oid=3&rx=0&eae=0&docm=9&vis=0&fu=0&ifi=2&xpc=GWzPXrkuXb&p=http%3A//www.cnblogs.com&dtd=41" frameBorder="0" width="300" allowTransparency="true" name="google_ads_frame2" marginWidth="0" scrolling="no" hspace="0"></iframe><iframe id="google_ads_frame3" vspace="0" height="250" marginHeight="0" src="http://googleads.g.doubleclick.net/pagead/ads?client=ca-pub-3447371224873639&output=html&h=250&slotname=8660799060&adk=1970350646&w=300&lmt=1399271423&flash=0&url=http%3A%2F%2Fwww.cnblogs.com%2Fxiaochao1234%2Fp%2F3707406.html&dt=1399271427389&shv=r20140429&cbv=r20140417&saldr=sb&prev_slotnames=4356862740%2C8660799060&correlator=1399271427191&frm=20&ga_vid=429972749.1397695120&ga_sid=1399267813&ga_hid=29020834&ga_fc=1&u_tz=480&u_his=145&u_java=1&u_h=768&u_w=1364&u_ah=740&u_aw=1364&u_cd=16&u_nplug=0&u_nmime=0&dff=verdana&dfs=16&adx=306&ady=2002&biw=314&bih=74&eid=317150304&oid=3&rx=0&eae=0&docm=9&vis=0&fu=0&ifi=3&xpc=irxIOJq0GO&p=http%3A//www.cnblogs.com&dtd=42" frameBorder="0" width="300" allowTransparency="true" name="google_ads_frame3" marginWidth="0" scrolling="no" hspace="0"></iframe>