惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Threatpost
V
Vulnerabilities – Threatpost
TaoSecurity Blog
TaoSecurity Blog
C
Cybersecurity and Infrastructure Security Agency CISA
P
Proofpoint News Feed
G
GRAHAM CLULEY
S
Securelist
P
Palo Alto Networks Blog
MongoDB | Blog
MongoDB | Blog
A
Arctic Wolf
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
WordPress大学
WordPress大学
Project Zero
Project Zero
T
Threat Research - Cisco Blogs
L
Lohrmann on Cybersecurity
C
Cyber Attacks, Cyber Crime and Cyber Security
F
Fortinet All Blogs
博客园 - 叶小钗
B
Blog RSS Feed
C
Cisco Blogs
Google DeepMind News
Google DeepMind News
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Apple Machine Learning Research
Apple Machine Learning Research
G
Google Developers Blog
K
Kaspersky official blog
D
Docker
Latest news
Latest news
Cisco Talos Blog
Cisco Talos Blog
T
Tor Project blog
Cyberwarzone
Cyberwarzone
Security Latest
Security Latest
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Spread Privacy
Spread Privacy
Microsoft Azure Blog
Microsoft Azure Blog
C
Check Point Blog
J
Java Code Geeks
Simon Willison's Weblog
Simon Willison's Weblog
T
Tenable Blog
Recent Announcements
Recent Announcements
T
Tailwind CSS Blog
H
Help Net Security
L
LINUX DO - 热门话题
T
The Exploit Database - CXSecurity.com
Jina AI
Jina AI
S
SegmentFault 最新的问题
MyScale Blog
MyScale Blog
NISL@THU
NISL@THU
美团技术团队
腾讯CDC

博客园 - pipisong

Caused by: org.apache.catalina.LifecycleException错误的处理 (转帖)HTTP协议 (转帖)工程師的缺德行為:叫朋友去學C/C++ (转帖)修改 Linux 主机名 (转帖)oracle 10g中的几个概念(sid/db_name/server_name) (转帖)rman中改变控制文件的备份路径 (转帖)修改ORACLE自动归档目录 (转帖) oracle是否归档模式及修改模式 (转帖) Oracle实例恢复(Oracle instance recovery) 硬盘分区表知识——详解硬盘MBR(转) 关于RAID与SCSI的一些基本概念(转) USB安裝XP的方法<转> SATA光驱GHOST系统的解决办法《转帖》 snmputil是个很有意思的东东 更新一下 Windows XP自动登录设置 06年国庆长假最后一天有感 06年6月17日子夜有感 <转贴>使用NTFS的连接点移动Document and Setting
(转帖)网站微信登录-python 实现
pipisong · 2015-02-15 · via 博客园 - pipisong

最近微信登录开放公测,为了方便微信用户使用,我们的产品也决定加上微信登录功能,然后就有了这篇笔记。

根据需求选择相应的登录方式

微信现在提供两种登录接入方式

  • 移动应用微信登录
  • 网站应用微信登录

这里我们使用的是网站应用微信登录

按照 官方流程

1 注册并通过开放平台开发者资质认证

注册微信开放平台帐号后,在帐号中心中填写开发者资质认证申请,并等待认证通过。

2 创建网站应用

通过填写网站应用名称、简介和图标,以及各平台下载地址等资料,创建网站应用

3 接入微信登录

在资源中心查阅网站应用开发文档,开发接入微信登陆功能,让用户可使用微信登录你的网站应用

如果已经完成上面的操作,请继续往下看

微信网站应用微信登录是基于OAuth2.0协议标准构建的微信OAuth2.0授权登录系统。

微信OAuth2.0授权登录目前支持authorization_code模式,适用于拥有server端的应用授权。该模式整体流程为:

  1.  第三方发起微信授权登录请求,微信用户允许授权第三方应用后,微信会拉起应用或重定向到第三方网站,并且带上授权临时票据code参数;

  2.  通过code参数加上AppID和AppSecret等,通过API换取access_token;

  3.  通过access_token进行接口调用,获取用户基本数据资源或帮助用户实现基本操作。

获取access_token 时序图

具体流程请参考官方文档,我们这里只说一下python的实现方法。官方文档地址 点这里

参考python-instagram 我写了一个 python-weixin (https://github.com/zongxiao/python-weixin)一个微信python SDK

不过现在还只有微信接入、获取用户信息、 刷新refresh_token 等简单功能

首先 需要把代码clone到本地

然后执行 

使用方式非常简单

复制代码

 1 from weixin.client import WeixinAPI
 2 
 3 APP_ID = 'your app id'
 4 APP_SECRET = 'your app secret'
 5 REDIRECT_URI = 'http://your_domain.com/redirect_uri'  # 这里一定要注意 地址一定要加上http/https
 6 
 7 scope = ("snsapi_login", )
 8 api = WeixinAPI(appid=APP_ID,
 9                         app_secret=APP_SECRET,
10                         redirect_uri=REDIRECT_URI)
11 
12 authorize_url = api.get_authorize_url(scope=scope)

复制代码

现在将 

authorize_url 地址在浏览器打开, 将跳转到微信登录页面,使用手机扫码登录后将跳转到
http://your_domain.com/redirect_uri?code=CODE&state=STATE 页面

现在我们就可以使用code 来获取登录的 access_token

access_token = api.exchange_code_for_access_token(code=code)

access_token 信息为

复制代码

{ 
"access_token":"ACCESS_TOKEN", 
"expires_in":7200, 
"refresh_token":"REFRESH_TOKEN",
"openid":"OPENID", 
"scope":"SCOPE" 
}

复制代码

参数说明
access_token 接口调用凭证(有效期目前为2个小时)
expires_in access_token接口调用凭证超时时间,单位(秒)
refresh_token 用户刷新access_token(有效期目前为30天)
openid 授权用户唯一标识
scope 用户授权的作用域,使用逗号(,)分隔

获取access_token后,就可以进行接口调用,有以下前提:

  1.  access_token有效且未超时;

  2.  微信用户已授权给第三方应用帐号相应接口作用域(scope)。

对于接口作用域(scope),能调用的接口有以下:

授权作用域(scope)接口接口说明
snsapi_base /sns/oauth2/access_token 通过code换取access_token、refresh_token和已授权scope
/sns/oauth2/refresh_token 刷新或续期access_token使用
/sns/auth 检查access_token有效性
snsapi_userinfo /sns/userinfo 获取用户个人信息

复制代码

api = WeixinAPI(appid=APP_ID,
                app_secret=APP_SECRET,
                redirect_uri=REDIRECT_URI)

# 刷新或续期access_token使用
refresh_token = api.exchange_refresh_token_for_access_token(refresh_token=auth_info['refresh_token'])

api = WeixinAPI(access_token=auth_info['access_token'])

# 获取用户个人信息
user = api.user(openid=auth_info['openid'])

# 检查access_token有效性
v = api.validate_token(openid=auth_info['openid'])

复制代码

现在就微信登录就完成了

下面是用 flask 实现的完整的例子

复制代码

from flask import Flask
from flask import Markup
from flask import redirect
from flask import request
from flask import jsonify

from weixin.client import WeixinAPI
from weixin.oauth2 import OAuth2AuthExchangeError

app = Flask(__name__)

APP_ID = 'appid'
APP_SECRET = 'app secret'
REDIRECT_URI = 'http://localhost.com/authorization'


@app.route("/authorization")
def authorization():
    code = request.args.get('code')
    api = WeixinAPI(appid=APP_ID,
                    app_secret=APP_SECRET,
                    redirect_uri=REDIRECT_URI)
    auth_info = api.exchange_code_for_access_token(code=code)
    api = WeixinAPI(access_token=auth_info['access_token'])
    resp = api.user(openid=auth_info['openid'])
    return jsonify(resp)


@app.route("/login")
def login():
    api = WeixinAPI(appid=APP_ID,
                    app_secret=APP_SECRET,
                    redirect_uri=REDIRECT_URI)
    redirect_uri = api.get_authorize_login_url(scope=("snsapi_login",))
    return redirect(redirect_uri)


@app.route("/")
def hello():
    return Markup('<a href="%s">weixin login!</a>') % '/login'

if __name__ == "__main__":
    app.run(debug=True)

复制代码

参考链接:

微信网站应用接入文档 

网站应用创建地址

python-weixin github 地址  https://github.com/zongxiao/python-weixin