Django三方登基本原理讲解

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_33867131/article/details/83505018
# 1、调取oauth2/authorize接口,获取返回值code
def get_auth_url():
    # 微博的oauth2/authorize接口地址
    weibo_auth_url = "https://api.weibo.com/oauth2/authorize"
    # 回调地址(自己网站)
    redirect_url = "http://25.106.22.232:8000/complete/weibo/"
    auth_url = weibo_auth_url + "?client_id={client_id}&redirect_url=
{redirect_url}".format(client_id=237999617, re_url=redirect_url)
                                                                         
# 2、拿着code,调取微博oauth2/access_token接口,获取返回值access_token(有了access_token我
们就可以尽情的调用微博的各种接口,例如:获取用户的各种信息)
def get_access_token(code="971408f0569897d6ec44a227857335fe"):
    access_token_url = "https://api.weibo.com/oauth2/access_token"
    # 因为我们使用POST提交数据,所以我们需要导入requests
    import requests
    re_dict = requests.post(access_token_url, data={
        "client_id": "237999671",
        "client_secret": "2c60b652a3a0c649d1bb12ab8bb86a24",
        "grant_type": "authorization_code",
        "code": code,
        "redirect_url": "http://25.106.22.232:8000/complete/weibo/",
    })
    pass  # 在这里打个断点,debug运行


# 3、例如我们拿着access_token,调取用户信息接口,去获取用户信息,用于三方登录
def get_user_info(access_token="", uid=""):
    user_url = "https://api.weibo.com/2/users/show.json?access_token={token}&uid=
{uid}".format(token=access_token, uid=uid)

    print(user_url)

# 调用函数
if __name__ == '__main__':
    get_auth_url()
    get_access_token(code="971408f0569897d6ec44a227857335fe")
    get_user_info(access_token="", uid="1272188234")

猜你喜欢

转载自blog.csdn.net/qq_33867131/article/details/83505018