码迷,mamicode.com
首页 > 其他好文 > 详细

腾讯微薄分享一(授权)

时间:2014-06-04 21:00:25      阅读:354      评论:0      收藏:0      [点我收藏+]

标签:c   class   blog   code   java   a   

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

腾讯微薄的分享与新浪的区别在于除了获取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

腾讯微薄分享一(授权)

标签:c   class   blog   code   java   a   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!