用WeiboAuthListener擷取code
用下面的代碼擷取token。。半成品的sdk真讓人捉急。
[java] view plaincopy
-
[java] view plaincopy
- String code = values.getString("code");
- if(code != null){
- mText.setText("取得認證code: \r\n 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();
- }