Website introduced QQ login

I was white, the following are personal understanding, we must take the thinking to read.

Before you add QQ login function, you need https://connect.qq.com/index.html apply for the application, and then get APP ID, APP Key and other information, not repeat them here.

 

Step website added QQ login feature as follows:

1. Fill a link QQ login

A. In the login link at the corresponding fill in the content, such as

https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id=&redirect_uri=&state=

Parameter Description:

Response_type code value is fixed;

Application client_id value AppID;

redirect_uri fill callback address is to apply;

After accessing this link will enter the QQ login interface.

 

 B. After clicking Sign in, we will return the callback address application and carry code code , we need to write the corresponding controller processing.

 

 

2. obtained using Code, the Token acquisition Access .

A. Send Request:

"https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&client_id=" + appid + "&client_secret="+ appkey + "&redirect_uri=" + redirectURI + "&code=" + code

Parameter Description:

grant_type的值固定为authorization_code;

client_id的值为申请的appid;

client_secret的值为申请的appkey;

redirect_uri的值为申请时填写的回调地址;(注意:需要将url进行编码!!!)

code值为获取到的Authorization Code值

B.请求之后会返回如下内容,绿色部分就是token值。

 

3.利用得到的token,获取OpenID。

A.发送请求:

"https://graph.qq.com/oauth2.0/me?access_token=" + accessToken;

参数说明:

access_token值为获取到的Access Token值

B.请求之后会返回如下内容,绿色部分就是openid值。

 

4.利用得到的openid,获取用户信息。

A.发送请求:

https://graph.qq.com/user/get_user_info?access_token=" + access_token+ "&oauth_consumer_key=" + appid + "&openid=" + openid

B.请求之后就能获得用户信息,例如昵称、性别等。

 

参考代码:

1.处理code

2.请求处理部分参考OkHttp(网址https://square.github.io/okhttp)

//1.得到QQ用户的token
public String getQQAccessToken(AccessTokenDTO accessTokenDTO){
String appid = accessTokenDTO.getClient_id();
String appkey = accessTokenDTO.getClient_secret();
String redirectURI = accessTokenDTO.getRedirect_uri();
String code = accessTokenDTO.getCode();

String asStr ="https://graph.qq.com/oauth2.0/token?grant_type=authorization_code&client_id=" + appid + "&client_secret="+ appkey + "&redirect_uri=" + redirectURI + "&code=" + code;

MediaType mediaType = MediaType.get("application/json; charset=utf-8");
OkHttpClient client = new OkHttpClient();
//RequestBody对象转换成json对象
RequestBody body = RequestBody.create(mediaType, JSON.toJSONString(accessTokenDTO));
Request request = new Request.Builder()
.url(asStr)
.post(body)
.build();
try {
Response response = client.newCall(request).execute();
String string = response.body().string();
  //处理得到的字符串,以获得token。

String token = string.split("&")[0].split("=")[1];
return token;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
 

Guess you like

Origin www.cnblogs.com/yang37/p/12158700.html