用WeiboAuthListener获取code
用下面的代码获取token。。半成品的sdk真让人捉急。
- String code = values.getString("code");
- if(code != null){
- mText.setText("取得认证code: Code: " + code);
- Toast.makeText(MainActivity.this, "认证code成功", Toast.LENGTH_SHORT).show();
- }
- try {
- URL url = new URL("https://api.weibo.com/oauth2/access_token");
- URLConnection connection = url.openConnection();
- connection.setDoOutput(true);
- OutputStreamWriter out = new OutputStreamWriter(connection
- .getOutputStream(), "utf-8");
- out.write("client_id=id&client_secret=secret&grant_type=authorization_code" +
- "&code="+code+"&redirect_uri=uri");
- out.flush();
- out.close();
- String sCurrentLine;
- String sTotalString;
- sCurrentLine = "";
- sTotalString = "";
- InputStream l_urlStream;
- l_urlStream = connection.getInputStream();
- BufferedReader l_reader = new BufferedReader(new InputStreamReader(
- l_urlStream));
- while ((sCurrentLine = l_reader.readLine()) != null) {
- sTotalString += sCurrentLine;
- }
- JSONObject o=new JSONObject(sTotalString);
- System.out.println(o.getString("access_token"));
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }