All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.shawven.security.connect.provider.qq.connet.QQOAuth2Template Maven / Gradle / Ivy


package com.github.shawven.security.connect.provider.qq.connet;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.social.oauth2.AccessGrant;
import org.springframework.social.oauth2.OAuth2Template;
import org.springframework.util.MultiValueMap;


public class QQOAuth2Template extends OAuth2Template {

	private Logger logger = LoggerFactory.getLogger(getClass());

	public QQOAuth2Template(String clientId, String clientSecret, String authorizeUrl, String accessTokenUrl) {
		super(clientId, clientSecret, authorizeUrl, accessTokenUrl);
		setUseParametersForClientAuthentication(true);
	}


	@Override
	protected AccessGrant postForAccessGrant(String accessTokenUrl, MultiValueMap parameters) {
		String responseStr = getRestTemplate().postForObject(accessTokenUrl, parameters, String.class);

		logger.info("获取qq accessToke的响应:"+responseStr);

		String[] items = StringUtils.split(responseStr, "&");
		if (items == null || items.length < 3) {
            throw new RuntimeException("qq accessToke的响应无效");
        }

		String accessToken = StringUtils.substringAfterLast(items[0], "=");
		Long expiresIn = new Long(StringUtils.substringAfterLast(items[1], "="));
		String refreshToken = StringUtils.substringAfterLast(items[2], "=");

		return new AccessGrant(accessToken, null, refreshToken, expiresIn);
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy