在开始之前,请确定已经拥有自己地域名,并完成备案,否则需要先购买域名并备案后再接入QQ授权登录!!!
首先鸣谢这位大佬 @JasonQ1an,很耐心地帮助我解决了域名更改问题!!!
step0:
需要按照上面大佬的文章,将域名换成自己备案好的域名!
JasonQ1an大佬的域名更换文章
step1:
在qq互联官网申请权限!!
需要注意的申请时图片必须是100*100要严格符合要求,并且要在网页上放置好qq登录按钮,其他信息按下图填写即可(注意只有网站介绍可以自定义,其他的必须按照下图填写),并且触发函数与apply_code,receive_code要写好。此时点链接应该是qq授权界面,并且返回100008,在通过后即可正常授权。
step2:
qq_login的js与css(以下所有*号需要换成自己的)
1.js需要添加或修改的部分
<div class="ac-game-settings-acwing">
<img width="30" src="https://app***.acapp.acwing.com.cn/static/image/settings/acwing_logo.png" calss="acwing_login">
<img width="30" src="https://app***.acapp.acwing.com.cn/static/image/settings/qq_login.png" class="qq_login">
</div>
this.$qq_login = this.$settings.find('.qq_login');
add_listening_events() {
let outer = this;
this.add_listening_events_login();
this.add_listening_events_register();
this.$acwing_login.click(function(){
outer.acwing_login();
});
this.$qq_login.click(function(){
outer.qq_login();
});
}
qq_login() {
$.ajax({
url : "https://app***.acapp.acwing.com.cn/settings/qq/apply_code/",
type : "GET",
success : function(resp) {
if(resp.result === "success") {
window.location.replace(resp.apply_code_url);
}
}
})
}
2.css需要修改或添加的
.ac-game-settings-acwing > div {
color: white;
font-size: 1.5vh;
text-align: center;
display: block;
}
.ac-game-settings-acwing {
display: flex;
flex-direction: row;
justify-content: space-evenly;
align-items: center;
height: 7vh;
}
.acwing_login {
display: inline-flex;
width: 30px;
height: 30px;
border-radius: 50%;
}
.qq_login{
display: inline-flex;
width: 30px;
height: 30px;
border-radius: 50%;
}
step3:
写apply_code与receive_code以及它们的url
1.apply_code
from django.http import JsonResponse
from urllib.parse import quote
from random import randint
from django.core.cache import cache
def get_state():
res = ""
for i in range(8):
res+=str(randint(0,9))
return res
def apply_code(request) :
response_type = "code"
client_id = "*******"
redirect_uri = "https://app***.acapp.acwing.com.cn/settings/qq/receive_code"
state = get_state()
scope = "get_user_info"
cache.set(state,True,7200) #有效期
apply_code_url = "https://graph.qq.com/oauth2.0/authorize"
return JsonResponse({
'result' : "success",
'apply_code_url' : apply_code_url + "?response_type=%s&client_id=%s&redirect_uri=%s&state=%s&scope=%s" %(response_type,client_id,redirect_uri,state,scope)
})
2.receive_code
from django.shortcuts import redirect
from django.core.cache import cache
import requests
from urllib.parse import quote
from django.contrib.auth.models import User
from game.models.player.player import Player
from django.contrib.auth import login
from random import randint
def receive_code(request):
data = request.GET
code = data.get('code')
state = data.get('state')
if not cache.has_key(state):
return redirect("index")
cache.delete(state)
apply_access_token_url = "https://graph.qq.com/oauth2.0/token"
params = {
'grant_type' : "authorization_code",
'client_id' : "******",
'client_secret' : "*************",
'code' : code,
'redirect_uri' : "https://app***.acapp.acwing.com.cn/settings/qq/receive_code",
'fmt' : "json"
}
access_token_res = requests.get(apply_access_token_url,params=params).json()
access_token = access_token_res['access_token']
get_user_openid_url = "https://graph.qq.com/oauth2.0/me"
params = {
'access_token' : access_token,
'fmt' : "json"
}
openid_res = requests.get(get_user_openid_url,params=params).json()
openid = openid_res['openid']
players = Player.objects.filter(openid=openid)
if players.exists():
login(request,players[0].user)
return redirect("index")
get_userinfo_url = "https://graph.qq.com/user/get_user_info"
params = {
'access_token':access_token,
'oauth_consumer_key':"******",
'openid':openid,
'fmt' : "json"
}
userinfo_res = requests.get(get_userinfo_url,params=params).json()
username = userinfo_res['nickname']
photo = userinfo_res['figureurl_qq_1']
while User.objects.filter(username=username).exists():
username += str(randint(0,9))
user = User.objects.create(username=username)
player = Player.objects.create(user=user,photo=photo,openid=openid)
login(request,user)
return redirect("index")
3.url有三处需要修改,与acwing一键登录配置方式相同,参照y总视频就可以了。
为啥我按你的一部一部来的,点qq没反应啊,也不报错
佬,找工作的化,django和sprinboot是都学,还是建议只学一个
目前python和java都基础语法都会点
有没有兄弟遇到在QQ互联官网无法提交审核的情况
双击两下头像试试
问一下他说 “对不起,该网站尚未开通QQ帐号登录(错误码:100008)”这个怎么解决呀
解决了吗
大佬,请问client_id是app_id,client_secret是app_secret吗?
你好,请问
oauth_consumer_key
在哪里能找到- -那个就是client_id
优质大作文
hh