用WeiboAuthListener获取code
用下面的代码获取token。。半成品的sdk真让人捉急。
[java] view plaincopy
class="dp-j" start="1">
-
[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) {
-
- e.printStackTrace();
- }