当前位置:首页 > Web开发 > 正文

需要在 https://connect.qq.com/index.html 申请应用

2024-03-31 Web开发

在添加QQ登录成果之前,需要在https://connect.qq.com/index.html申请应用,然后得到APP ID、APP Key等信息,此处不再赘述。

网站添加QQ登录成果的法式如下:

1.填写QQ登录的链接

A.在对应的登录链接处填下如内容

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

参数说明:

response_type的值固定为code;

client_id的值为申请的appid;

redirect_uri的值为申请时填写的回调地点;

访谒这个链接之后就会进入QQ登录的界面。

技术图片

 B.点击登录之后,会返回我们申请的回调地点并携带code码,需要我们编写对应的controller措置惩罚惩罚。

技术图片

2.操作得到的Code,获取Access Token

A.发送请求:

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

参数说明:

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;
}  

网站引入QQ登录

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/web/32106.html