首页 > 代码库 > 腾讯微薄分享一(授权)
腾讯微薄分享一(授权)
前两节说了新浪微薄的授权和分享,接下来两篇说下腾讯微薄的授权和分享。
腾讯微薄的分享与新浪的区别在于除了获取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()); }
//splitUrl方法截取url,获取accessToken
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); }
//getOpenId方法,获取openId
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 = http://www.mamicode.com/new Intent();>
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。