如何用python登录qq

528次阅读
没有评论

如何用python登录qq

应用接入前,首先需进行申请,获得对应的appid与appkey,以保证后续流程中可正确对网站与用户进行验证与授权。http://wiki.connect.qq.com/__trashed-2

QQ登录开发文档连接 http://wiki.connect.qq.com/准备工作_oauth2-0

腾讯QQ互联平台没有python SDK,我们使用封装好的SDK包

安装:pip install QQLoginTool

导入:from QQLoginTool.QQtool import OAuthQQ

OAuthQQ类中的方法:

__init__(self, client_id=None, client_secret=None, redirect_uri=None, state=None):

·client_id : 申请QQ登录成功后,分配给应用的appid。

·client_secret:申请QQ登录成功后,分配给网站的appkey。

·redirect_uri:成功授权后的回调地址,必须是注册appid时填写的主域名下的地址,建议设置为网站首页或网站的用户中心。注意需要将url进行URLEncode。

·state:client端的状态值。用于第三方应用防止CSRF攻击,成功授权后回调时会原样带回。请务必严格按照流程检查用户与state参数状态的绑定。

get_qq_url(self)   # 获取QQ登录网页网址
get_access_token(self, code)  # 获取access_token值
get_open_id(self, access_token)  # 获取open_id值

下面以Django为例实现QQ第三方登录

过程:

获取QQ登录网页网址

接口设计:

请求方式:GET /?state=xxx

请求参数:

如何用python登录qq

返回数据:JSON

{
    login_url": "https://graph.qq.com/oauth2.0/show?which=Login&display=pc&response_type=code&client_id=**&
    redirect_uri=**&state=**&scope=**"
}

如何用python登录qq

代码实现:

from QQLoginTool.QQtool import OAuthQQ
from django.conf import settings
from rest_framework.views import APIView
...
# 获取login_url
class OauthQQLogin(APIView):
    def get(self, request):
        # 获取前端传入的参数
        state = request.query_params.get('next', None)
        # 判断是否有,如果没有后端创建一个
        if not state:
            state = '/'
        # 实例化对象
        oauth = OAuthQQ(client_id=settings.QQ_CLIENT_ID, client_secret=settings.QQ_CLIENT_SECRET, redirect_uri=
        settings.QQ_REDIRECT_URI, state=state)
        # 获取login_url
        login_url = oauth.get_qq_url()
        # 返回login_url
        return Response({'login_url': login_url})

获取openid

在QQ将用户重定向到此网页的时候,重定向的网址会携带QQ提供的code参数,用于获取用户信息使用,我们需要将这个code参数发送给后端,在后端中使用code参数向QQ请求用户的身份信息

/oauth_callback.html?code=****&state=%2F

oauth_callback回调页,用于扫码后接受Authorization Code

通过Authorization Code获取Access Token

然后通过Access Token获取openid

接口设计:

请求方式:GET /?code=xxx

请求参数:

如何用python登录qq

返回数据:JSON

{
    "openid": xxxx 
}

如何用python登录qq

代码实现:

from QQLoginTool.QQtool import OAuthQQ
from django.conf import settings
from itsdangerous import TimedJSONWebSignatureSerializer as TJS
from rest_framework.views import APIView
...
# 获取openid 
class OauthQQToken(APIView):
    def get(self, request):
        # 获取前端传入的code
        code = request.query_params.get('code', None)
        # 判断是否有,如果没有直接return
        if not code:
        return Response({'message': '缺少code'})
    oauth = OAuthQQ(client_id=settings.QQ_CLIENT_ID, client_secret=settings.QQ_CLIENT_SECRET,redirect_uri=
    settings.QQ_REDIRECT_URI)
        try:
        # 使用code向QQ服务器请求access_token
        access_token = oauth.get_access_token(code)
        # 使用access_token获取openid
        openid = oauth.get_open_id(access_token)
        except:
        return Response({'message': 'QQ服务异常'})       
    ...
<a href="https://www.python51.com/">Python教程学习网</a>,免费的在线学习python平台,欢迎关注!
神龙|纯净稳定代理IP免费测试>>>>>>>>天启|企业级代理IP免费测试>>>>>>>>IPIPGO|全球住宅代理IP免费测试

相关文章:

版权声明:wuyou2020-01-25发表,共计2669字。
新手QQ群:570568346,欢迎进群讨论 Python51学习
评论(没有评论)