前两节说了新浪微薄的授权和分享,接下来两篇说下腾讯微薄的授权和分享。
腾讯微薄的分享与新浪的区别在于除了获取accessToken之外还需要获取openId
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.auth); web = (WebView) findViewById(R.id.web); StringBuffer sb = new StringBuffer("https://graph.qq.com/oauth2.0/authorize"); sb.append("?response_type=token"); sb.append("&client_id=" + Constant.TENCONNECT_APP_ID); try { sb.append("&redirect_uri=" + URLEncoder.encode(Constant.TENCONNECT_REDIRECT_URL, "UTF-8")); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } sb.append("&scope=add_share,add_pic_t"); sb.append("&state=share"); sb.append("&display=mobile"); web.getSettings().setJavaScriptEnabled(true); web.setWebViewClient(new WebViewClient() { @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { LogInfo.i(TAG, "page start url:" + url); if (url.startsWith(Constant.TENCONNECT_REDIRECT_URL)) { splitUrl(url); } else super.onPageStarted(view, url, favicon); } }); web.loadUrl(sb.toString()); }
private void splitUrl(String url) { String paramStr = url.substring(url.indexOf("#") + 1); String[] paramsArray = paramStr.split("&"); Map<String, Object> nvPair = new HashMap<String, Object>(); if (paramsArray != null && paramsArray.length != 0) { for (String param : paramsArray) { String[] paramPair = param.split("="); if (paramPair.length > 1) nvPair.put(paramPair[0], paramPair[1]); } } String accessToken = nvPair.get("access_token").toString(); long expiresIn = Long.parseLong(nvPair.get("expires_in").toString()); oAuth = new OAuth(accessToken, expiresIn, System.currentTimeMillis()); getOpenid(accessToken); }
private void getOpenid(final String accessToken) { LogInfo.i(TAG, "accessToken:" + accessToken); if (fetchOpenIdTask != null && fetchOpenIdTask.getStatus() == Status.RUNNING) return; fetchOpenIdTask = new AsyncTask<Object, Object, String>() { @Override protected String doInBackground(Object... params) { byte[] b = HttpClientConnector.getInstance(AuthWebActivity.this).requestByGet("https://graph.qq.com/oauth2.0/me?access_token=" + accessToken); String result = new String(b); LogInfo.i(TAG, result); Json json = new Json(result.substring(9, result.length() - 1)); return json.getString("openid"); } @Override protected void onPostExecute(String result) { removeDialog(0); if (!TextUtils.isEmpty(result)) { oAuth.openId = result; Intent data = new Intent(); data.putExtra("oauth", oAuth); setResult(RESULT_CODE, data); finish(); } else finish(); super.onPostExecute(result); } }; fetchOpenIdTask.execute(); }
原文:http://blog.csdn.net/familyhappylife/article/details/27198773