一、开放平台
1.web端
(1)淘宝开放平台
注册淘宝账号,关联实名认证的支付宝账号,并进行实人认证
在控制台-服务发布中点击创建新应用
选择需要的应用标签,进行申请,这里以百川无线应用为例
申请时,会让注册百川账号,进行内容迁移
进入阿里百川,创建应用,app地址最好写正式的,不然后面还要重新下载
点击左侧的API申请,选择刚创建的应用
点击我的产品后台-开通百川电商sdk和云账号
这时返回淘宝开发者平台-应用管理,就能看到相应信息
点击应用管理-设置回调地址
http://127.0.0.1:8000/login/taobao-t
已经获得app_key和app_secret
点击sdk下载,解压(本项目中采用的是social_django三方登录库,不用这一步)
三方设置——阿里百川文档:https://www.kancloud.cn/moxiuxun/taoke_app/709080
2.移动应用
(1)QQ互联
已经创建过网站应用的,在移动应用里点击关联腾讯开放平台
填写网站应用的AppID和AppKey
未完,待续
二、问题解决
1.三方平台中如果username/nickname为中文时,用django自带的用户模型存储,会出现不规则乱码,影响使用
找到自定义用户模型扩展继承的AbstractUser类
位置:虚拟环境下\Lib\site-packages\django\contrib\auth\models.py
说明:six.PY3 返回一个表示当前运行环境是否为python3的boolean值
发现调用了两个大类:UnicodeUsernameValidator和ASCIIUsernameValidator
进入UnicodeUsernameValidator类
修改定义的规则
regex = r'^[\S.@+-]+$'
三、开放平台文档/接口工具
1.平台文档
微博:https://open.weibo.com/wiki/%E5%BE%AE%E5%8D%9AAPI
(需要审核才能申请开通调用用户信息接口-挺麻烦的)
淘宝:http://open.taobao.com/doc.htm?docId=102635&docType=1
百度:http://developer.baidu.com/wiki/index.php?title=docs/oauth
github:https://developer.github.com/v3/oauth_authorizations/
开源中国:https://www.oschina.net/openapi/docs
蚂蚁金服:https://openhome.alipay.com/developmentDocument.htm
优酷土豆:https://doc.open.youku.com/?docid=483
2.接口管理工具
eoLinker:[https://www.eolinker.com]( https://s.growingio.com/gNKZYz),必须说的是 eoLinker 开源免费的。
一些产品的开源项目和链接:
开源支持:[https://www.eolinker.com/#/os/download]( https://www.eolinker.com/#/os/download)
Github:[https://github.com/eolinker]( https://github.com/eolinker)
码云:[https://gitee.com/eoLinker-API-Management]( https://gitee.com/eoLinker-API-Management)
Coding:[https://coding.net/u/eolinker/project]( https://coding.net/u/eolinker/project)
Blog:[http://blog.eolinker.com]( http://blog.eolinker.com )
视频教程:[http://blog.eolinker.com/#/course/]( http://blog.eolinker.com/#/course/)
3.其他文档
1. 微信硬件设备开发文档: http://iot.weixin.qq.com/index.html
2. 微信-蓝牙开发demo: http://iot.weixin.qq.com/document-4_1.html
3. 申请微信公众平台接口测试账号: http://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login
4. 微信公众平台开发文档: http://mp.weixin.qq.com/wiki/home/index.html
5. 微信JS-SDK开发的Demo: http://demo.open.weixin.qq.com/jssdk/
6. 微信JS-SDK开发文档: http://mp.weixin.qq.com/wiki/7/aaa137b55fb2e0456bf8dd9148dd613f.html
四、social_django添加三方登录代码
1.百度
# coding:utf-8
"""
注意:登录可成功,退出时 清除session还是有残留信息
Baidu OAuth2 backend, docs at:
http://developer.baidu.com/wiki/index.php?title=docs/oauth
"""
from .oauth import BaseOAuth2
class BaiduOAuth2(BaseOAuth2):
"""Baidu OAuth authentication backend"""
name = 'baidu'
ID_KEY = 'uid'
AUTHORIZATION_URL = 'http://openapi.baidu.com/oauth/2.0/authorize'
ACCESS_TOKEN_URL = 'https://openapi.baidu.com/oauth/2.0/token'
ACCESS_TOKEN_METHOD = 'POST'
REDIRECT_STATE = False
EXTRA_DATA = [
('userid', 'userid'),
('username', 'username'),
('portrait', 'portrait'),
('sex', 'sex')
]
def get_user_details(self, response):
"""Return user details from Baidu.
"""
username = response.get('username')
return {'username': username,
'first_name': username}
def get_uid(self, access_token):
"""Return uid by access_token"""
data = self.get_json(
'https://openapi.baidu.com/rest/2.0/passport/users/getLoggedInUser',
method='POST',
params={'access_token': access_token}
)
return data['uid']
def user_data(self, access_token, *args, **kwargs):
uid = self.get_uid(access_token)
response = self.get_json(
'https://openapi.baidu.com/rest/2.0/passport/users/getInfo', params={
'access_token': access_token,
}
)
response['uid'] = uid
return response
baidu.py
2.淘宝修改
# 自定义补充
class TAOBAOAuth2(BaseOAuth2):
"""Taobao OAuth authentication mechanism"""
name = 'taobao'
ID_KEY = 'taobao_open_uid'
ACCESS_TOKEN_METHOD = 'POST'
AUTHORIZATION_URL = 'https://oauth.taobao.com/authorize'
ACCESS_TOKEN_URL = 'https://oauth.taobao.com/token'
EXTRA_DATA = [
('taobao_user_id', 'taobao_user_id'),
('taobao_user_nick', 'taobao_user_nick'),
('access_token', 'access_token'),
('token_type', 'token_type')
]
def user_data(self, access_token, *args, **kwargs):
"""Return user data provided"""
try:
return self.get_json('http://gw.api.taobao.com/router/rest', params={
'method': 'taobao.user.get',
'fomate': 'json',
'v': '2.0',
'access_token': access_token
})
except ValueError:
return None
def get_user_details(self, response):
"""Return user details from Taobao account"""
fullname, first_name, last_name = self.get_user_names(
response.get('name')
)
username = response.get('taobao_user_nick')
return {
'username': username,
'first_name': first_name,
}
taobao.py
说明:
3.开源中国
# coding:utf-8
"""
Oschina OAuth2 backend, docs at:
https://www.oschina.net/openapi/docs
"""
from .oauth import BaseOAuth2
class OschinaOAuth2(BaseOAuth2):
"""Oschina OAuth authentication backend"""
name = 'oschina'
ID_KEY = 'uid'
AUTHORIZATION_URL = 'https://www.oschina.net/action/oauth2/authorize'
ACCESS_TOKEN_URL = 'https://www.oschina.net/action/openapi/token'
ACCESS_TOKEN_METHOD = 'POST'
REDIRECT_STATE = False
EXTRA_DATA = [
('name', 'username'),
('portrait', 'portrait'),
('gender', 'gender')
]
def get_user_details(self, response):
"""Return user details from Oschina.
"""
username = response.get('name')
return {'username': username,
'first_name': username}
def user_data(self, access_token, response=None, *args, **kwargs):
"""Return user data"""
user_data = self.get_json(
'https://www.oschina.net/action/openapi/my_information',
params={'access_token': access_token}
)
return user_data
五、参考文档:
1.Django-1.10支持中文用户注册登录:https://www.cnblogs.com/dribs/p/8376040.html
2.求推荐一个开源的 API 文档管理系统:https://www.v2ex.com/t/106648
3.bilibili开放平台相关:https://www.helplib.com/c/mutia_127096