我也是小白,以下内容均为个人理解,大家一定要带思考的去阅读。
在添加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的值为申请时填写的回调地址;
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.请求之后就能获得用户信息,例如昵称、性别等。
参考代码(Java):
1.处理回传code的代码
2.请求处理部分参考OkHttp(网址https://square.github.io/okhttp)
//1.得到QQ用户的token 传入code值
// 成功返回token值 失败返回null
public String getQQToken(QQAccessTokenDTO accessTokenDTO) {
String appid = accessTokenDTO.getAppId();
String appkey = accessTokenDTO.getAppKey();
String redirectURI = accessTokenDTO.getRedirectURI();
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;
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(asStr) //这个网址不要写错了
.build();
try {
Response response = client.newCall(request).execute();
String string = response.body().string();
// access_token=F72B107675F7A78C157F4509C86E320E&expires_in=7776000&refresh_token=ED29C1505D78251D4A897B5D2C10E357
String qqtoken = string.split("&")[0].split("=")[1];
return qqtoken;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
3.如何在本地测试QQ登录接口?
修改host文件,参考路径:C:WINDOWSsystem32driversetc
用处:当访问设定的域名时,解析到指定的ip地址上(前面的ip地址好像不能带端口号),输入127.0.0.1即为本机。
保存后在cmd命令行窗口中输入 ipconfig /flushdns刷新DNS缓存。
4.win10中系统和压缩内存占用80端口?
我的电脑--管理--服务,关闭“World Wide Web Publishing Service”服务。
查看指定端口占用情况 netstat -ano | findstr "80"