首页 > 其他 > 详细

腾讯微薄分享一(授权)

时间:2014-06-04 20:56:51      阅读:392      评论:0      收藏:0      [点我收藏+]

前两节说了新浪微薄的授权和分享,接下来两篇说下腾讯微薄的授权和分享。

腾讯微薄的分享与新浪的区别在于除了获取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 = new Intent();
				data.putExtra("oauth", oAuth);
				setResult(RESULT_CODE, data);
				finish();
			} else
				finish();
			super.onPostExecute(result);
		}

	};
	fetchOpenIdTask.execute();
}


腾讯微薄分享一(授权),布布扣,bubuko.com

腾讯微薄分享一(授权)

原文:http://blog.csdn.net/familyhappylife/article/details/27198773

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!