微博登录思维导图

微博登录思维导图

在微博创建应用

1.第一步
1
2.第二步
2
3.第三步
3
4.第四步
4
5.第五步
5
6.第六步
6

代码示例

vue代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<template>
<div>
<img :src="wb_src" alt="点击跳转微博第三方登录" @click="sina" class="imgcode">
</div>
</template>

<script>
export default {
data () {
return {
//微博登录url
//图片路径
wb_src:'http://127.0.0.1:8000/static/sina.png',
}
},
methods:{
//微博第三方登录
sina(){
//拼接url
//App Key
let client_id = 2731357014;
//授权回调页
let redirect_uri = 'http://127.0.0.1:8000/md_admin/weibo';
// //请求url
let url = 'https://api.weibo.com/oauth2/authorize?client_id='+client_id+'&redirect_uri='+redirect_uri;
// //跳转微博登录页,//返回code,然后后端去接收
window.location.href = url
}
}
}
django代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
urls路径配置:path('md_admin/weibo/',wb_back),

#新浪微博回调方法
def wb_back(request):
#接受参数
code = request.GET.get('code',None)
print('code是%s'%code)
#定义token的获取地址
url = 'https://api.weibo.com/oauth2/access_token'
#定义参数
data = {
'client_id':'2731357014',#App Key
'client_secret':'3eb11995fdd72c3b33b3971caac21ffe',#App Secret
'grant_type':'authorization_code',#类型
'code':code,#获取到的code
'redirect_uri':'http://127.0.0.1:8000/md_admin/weibo'
}
re = requests.post(url=url,data=data)
print('re的json是:',re.json())

#换取新浪微博用户昵称
weibo_url = 'https://api.weibo.com/2/users/show.json'
params = {
'access_token':re.json()['access_token'],
'uid':re.json()['uid']
}
res = requests.get(url=weibo_url,params=params)
print('res的json是:',res.json())
print(res.json())
#定义新浪id和用户id
sina_id = ''
user_id = ''

#判断是否使用新浪微博登陆过
user = User.objects.filter(username=str(res.json()['name'])).first()
if user:
#代表该账号曾经登陆过
sina_id = user.username
user_id = user.id
else:
#首次登录,入库
user = User(username=str(res.json()['name']),password="")
user.save()
user = User.objects.filter(username=str(res.json()['name'])).first()
sina_id = user.username
user_id = user.id
print('用户是:',sina_id,user_id)
#重定向到首页
return redirect('http://127.0.0.1:8080?sina_id='+str(sina_id)+'&user_id='+str(user_id))
# return HttpResponse('回调成功')

关于错误

1
2
3
今日写微博登录犯了一个很典型的错误:
就是单词打错:所有的client都拼写成了clinet
因为是在访问url,所以报了一个'请求不合法错误'

评论





载入天数...载入时分秒...

Blog content follows the Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0) License

Use WZH as theme, total visits times