本人初学乍练 有什么不合适的地方欢迎大家踊跃吐槽
首先参考官方API文档 了解腾讯微博Oauth2.0形式的授权过程
这次主要将的是(code模式)方式的鉴权
http://wiki.open.t.qq.com/index.php/API%E6%96%87%E6%A1%A3
了解大致过程后 下载官方SDK
我使用的是Java SDK
http://wiki.open.t.qq.com/index.php/SDK%E4%B8%8B%E8%BD%BD#Java_SDK
将官方SDK添加到项目中
在项目名上右击 Build Path->Add External Archives...选择要添加的jar文件
首先生成鉴权URL
上一步生成了授权URL 会自动引导你到填写微博账户的登录页面 填写完成 验证通过后 会回调到你刚才提交的redirect_uri地址
如果成功发送了微博 说明授权成功
写的比较仓促 鉴于本人也是初次接触 如果有什么不妥的地方欢迎大家吐槽
首先参考官方API文档 了解腾讯微博Oauth2.0形式的授权过程
这次主要将的是(code模式)方式的鉴权
http://wiki.open.t.qq.com/index.php/API%E6%96%87%E6%A1%A3
了解大致过程后 下载官方SDK
我使用的是Java SDK
http://wiki.open.t.qq.com/index.php/SDK%E4%B8%8B%E8%BD%BD#Java_SDK
将官方SDK添加到项目中
在项目名上右击 Build Path->Add External Archives...选择要添加的jar文件
首先生成鉴权URL
- /**
- * 生成腾讯微博授权URL
- * @param request
- * @param m
- * @return
- * @throws IOException
- */
- @RequestMapping(value = "/tencent/loginV2", method = RequestMethod.GET)
- public String tencentLogin(HttpServletRequest request, Model m) throws IOException {
- String url = "http://open.t.qq.com/cgi-bin/oauth2/authorize?client_id=你申请的App Key&redirect_uri=回调地址&response_type=code";//
- return "redirect:" + url;
- }
上一步生成了授权URL 会自动引导你到填写微博账户的登录页面 填写完成 验证通过后 会回调到你刚才提交的redirect_uri地址
- /**
- * 腾讯微博登录成功后的回调地址
- * @param code 用来换取accesstoken的授权码,有效期为10分钟
- * @param openid 用户统一标识,可以唯一标识一个用户
- * @param openkey 与openid对应的用户key,是验证openid身份的验证密钥
- * @param oAuthV2
- * @param request
- * @param response
- * @param model
- * @return
- * @throws Exception
- */
- @RequestMapping(value = "/tencent/loginV2/callback", method = RequestMethod.GET)
- public String tencentLoginCallback(String code, String openid, String openkey,
- OAuthV2 oAuthV2,//2.0
- HttpServletRequest request, HttpServletResponse response, Model model) throws Exception {
- //设置授权 直接将授权码的参数记录到OAuth类中
- if ((!QStrOperate.hasValue(code)) ||
- (!QStrOperate.hasValue(openid)) ||
- (!QStrOperate.hasValue(openkey))) {
- return EnumErrorCode.ERROR_404.getValue();
- }
- oAuthV2.setAuthorizeCode(code);
- oAuthV2.setOpenid(openid);
- oAuthV2.setOpenkey(openkey);
- oAuthV2.setClientId("申请时得到的AppKey");
- oAuthV2.setClientSecret("申请时得到的AppSecret");//大家可自行到腾讯微博开放平台申请
- oAuthV2.setRedirectUri("http://localhost/tencent/loginV2/callback");
- oAuthV2.setGrantType("authorization_code");
- //检查是否取得code
- if(!QStrOperate.hasValue(oAuthV2.getAuthorizeCode())){
- oAuthV2.setStatus(2);
- return EnumErrorCode.ERROR_404.getValue();
- }else
- oAuthV2.setStatus(0);//没有出错
- //访问Token
- String url = "https://open.t.qq.com/cgi-bin/oauth2/access_token";
- String queryString = QStrOperate.getQueryString(oAuthV2.getAccessTokenByCodeParamsList());
- //请求accesstoken
- String responseData = Q_HTTP_CLIENT.httpGet(url, queryString);
- //将服务器返回的包含access token等的回应包 解析存储到OAuth类中
- if (!parseAccessToken(responseData, oAuthV2)) {// Access Token 授权不通过
- oAuthV2.setStatus(3);
- return EnumErrorCode.ERROR_404.getValue();
- }
- //回调时可debug查看是否获得access token 下面为常用调用API测试方法
- // String response1;
- // String format="json";
- // String clientip="127.0.0.1";
- // String jing ="";
- // String wei ="";
- // String syncflag="";
- // String content="2";// 注意:因为后台会对微博内容进行判重,所以在重复测试时加上变换部分++++++++
- // String reid=null;
- // String ids=null;
- // String id=null;
- // String picpath=System.getProperty("user.dir")+"\\src\\main\\resources\\logo_QWeibo.jpg";
- //
- // TAPI tAPI=new TAPI(oAuthV2.getOauthVersion());//根据oAuth配置对应的连接管理器
- // //取得返回结果
- // response1=tAPI.add(oAuthV2, format, "测试发表文字微博"+content, clientip, jing, wei, syncflag);
- // // json数据使用
- // // response的结果可能是这样,{"data":{"id":"90221131024999","time":1333002978},"errcode":0,"msg":"ok","ret":0}
- // // 下面的代码将取出 id 的对应值,并赋予 reid
- // System.out.println("response = "+response1);
- // JSONObject responseJsonObject;
- // JSONObject dataJsonObject;
- // responseJsonObject= JSONObject.fromObject(response1);
- // dataJsonObject=(JSONObject)responseJsonObject.get("data");
- // id=ids=reid=dataJsonObject.get("id").toString();//对后面用到的 reid 赋值
- // System.out.println("reid = "+ reid);
- // try { Thread.sleep ( 5000 ) ; } catch (InterruptedException ie){}
- //
- // tAPI.addPic(oAuthV2, format, "发表一条带本地图片的微博"+content, clientip, jing, wei, picpath, syncflag);
- // try { Thread.sleep ( 5000 ) ; } catch (InterruptedException ie){}
- //
- // tAPI.addVideo(oAuthV2, format, "发表视频微博"+content, clientip, jing, wei, "http://www.tudou.com/programs/view/yx41TA6rQfE/?resourceId=0_03_05_07", syncflag);
- // try { Thread.sleep ( 5000 ) ; } catch (InterruptedException ie){}
- return "redirect:/";
- }
- /**
- * 得到服务器返回的包含access token等的回应包后,解析存储到OAuth类中
- *
- * @param responseData 格式:access_token=ACCESS_TOKEN&expires_in=60&name=NAME
- * @param oAuth
- * @return
- */
- public static boolean parseAccessToken(String responseData, OAuthV2 oAuth){
- if (!QStrOperate.hasValue(responseData)) {
- return false;
- }
- oAuth.setMsg(responseData);
- String[] tokenArray = responseData.split("&");
- if (tokenArray.length < 2) {
- return false;
- }
- String tmpStr;
- for(int i=0;i<tokenArray.length;i++){
- if (tokenArray[i].startsWith("access_token=")){
- tmpStr=tokenArray[i].substring(tokenArray[i].indexOf('=')+1, tokenArray[i].length());
- if(!QStrOperate.hasValue(tmpStr)){
- return false;
- }
- oAuth.setAccessToken(tmpStr);
- }
- if (tokenArray[i].startsWith("expires_in=")){
- tmpStr=tokenArray[i].substring(tokenArray[i].indexOf('=')+1, tokenArray[i].length());
- if(!QStrOperate.hasValue(tmpStr)){
- return false;
- }
- oAuth.setExpiresIn(tmpStr);
- }
- }
- return true;
- }
如果成功发送了微博 说明授权成功
写的比较仓促 鉴于本人也是初次接触 如果有什么不妥的地方欢迎大家吐槽