Skip to content

Commit 38ead3c

Browse files
committed
fix wxpub_oauth; add long_description
1 parent 329d096 commit 38ead3c

7 files changed

Lines changed: 72 additions & 13 deletions

File tree

LONG_DESCRIPTION.rst

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
Ping++ Python SDK
2+
===================================
3+
4+
Ping++ 官方 Python SDK。
5+
6+
安装
7+
-----
8+
9+
使用 pip 安装
10+
11+
$ pip install pingpp
12+
13+
API 文档及接入文档
14+
--------------------
15+
16+
- https://www.pingxx.com/api
17+
- https://www.pingxx.com/docs/overview

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2.6.1
1+
2.6.2

example/wxpub_oauth.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# -*- coding: utf-8 -*-
2+
3+
import pingpp
4+
5+
6+
# 微信公众号平台的 App ID
7+
wx_app_id = 'wx9283883h477376s'
8+
# 微信公众号平台的 App Secret
9+
wx_app_secret = 'da38db82d0e495a037aec19df178572b'
10+
11+
# -------
12+
# 获取 openid
13+
# 微信回调的页面,用于接收 code 来获取 openid
14+
redirect_url = 'https://example.com/authorize'
15+
16+
oauth_url = pingpp.WxpubOauth.create_oauth_url_for_code(
17+
wx_app_id, redirect_url, False)
18+
print(oauth_url)
19+
20+
# 在微信中打开 oauth_url,页面会跳转到 redirect_url,并带有参数 code
21+
code = 'CODE_IN_URL_QUERY'
22+
openid = pingpp.WxpubOauth.get_openid(
23+
wx_app_id, wx_app_secret, code)
24+
print(openid)
25+
26+
# -------
27+
# 获取 jsapi_ticket
28+
jsapi_ticket = pingpp.WxpubOauth.get_jsapi_ticket(wx_app_id, wx_app_secret)
29+
print(jsapi_ticket)

pingpp/http_client.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# -*- coding: utf-8 -*-
22
from __future__ import absolute_import, division, print_function
33

4-
import os
54
import sys
65
import textwrap
76
import warnings

pingpp/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# -*- coding: utf-8 -*-
22

3-
VERSION = '2.6.1'
3+
VERSION = '2.6.2'

pingpp/wxpub_oauth.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
import hashlib
44

5-
from pingpp import http_client, util
5+
from pingpp import http_client, util, proxy, ca_bundle
66
from pingpp.six.moves.urllib.parse import urlencode, quote_plus
7+
from pingpp import verify_ssl_certs as verify
78

89

910
class WxpubOauth:
@@ -23,11 +24,13 @@ def get_openid(app_id, app_secret, code):
2324
:return: openid 微信公众号授权用户唯一标识, 可用于微信网页内支付
2425
"""
2526
url = WxpubOauth.create_oauth_url_for_openid(app_id, app_secret, code)
26-
client = http_client.new_default_http_client()
27-
rbody, rcode = client.request('GET', url, {})
27+
client = http_client.new_default_http_client(
28+
verify_ssl_certs=verify, proxy=proxy, ca_bundle=ca_bundle)
29+
rbody, rcode, headers = client.request('GET', url, {})
2830
if rcode == 200:
2931
data = util.json.loads(rbody)
30-
return data['openid']
32+
if 'openid' in data:
33+
return data['openid']
3134

3235
return None
3336

@@ -88,24 +91,31 @@ def get_jsapi_ticket(app_id, app_secret):
8891
query_str = urlencode(data)
8992
access_token_url = \
9093
'https://api.weixin.qq.com/cgi-bin/token?' + query_str
91-
client = http_client.new_default_http_client()
92-
rbody, rcode = client.request('GET', access_token_url, {})
94+
client = http_client.new_default_http_client(
95+
verify_ssl_certs=verify, proxy=proxy, ca_bundle=ca_bundle)
96+
rbody, rcode, headers = client.request('GET', access_token_url, {})
9397
rbody = util.json.loads(rbody)
9498
if rcode != 200:
9599
return rbody
96100

101+
if 'access_token' not in rbody:
102+
return None
103+
97104
data = dict()
98105
data['access_token'] = rbody['access_token']
99106
data['type'] = 'jsapi'
100107
query_str = urlencode(data)
101108
jsapi_ticket_url = \
102109
'https://api.weixin.qq.com/cgi-bin/ticket/getticket?' + query_str
103-
client = http_client.new_default_http_client()
104-
rbody, rcode = client.request('GET', jsapi_ticket_url, {})
110+
client = http_client.new_default_http_client(
111+
verify_ssl_certs=verify, proxy=proxy, ca_bundle=ca_bundle)
112+
rbody, rcode, headers = client.request('GET', jsapi_ticket_url, {})
105113
data = util.json.loads(rbody)
106114
if rcode == 200:
107115
return data
108116

117+
return None
118+
109119
@staticmethod
110120
def get_signature(charge, jsapi_ticket, url):
111121
"""
@@ -126,5 +136,5 @@ def get_signature(charge, jsapi_ticket, url):
126136
key.lower(),
127137
sign_dict[key]
128138
) for key in sorted(sign_dict)])
129-
sign_dict['signature'] = hashlib.sha1(string).hexdigest()
130-
return sign_dict['signature']
139+
140+
return hashlib.sha1(string).hexdigest()

setup.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
install_requires.append('requests >= 0.8.8')
2626
install_requires.append('pycryptodome >= 3.4.7')
2727

28+
with open('LONG_DESCRIPTION.rst') as f:
29+
long_description = f.read()
30+
2831
# Don't import pingpp module here, since deps may not be installed
2932
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'pingpp'))
3033
from version import VERSION # noqa
@@ -43,6 +46,7 @@
4346
'pingpp.api_resources.abstract'],
4447
version=VERSION,
4548
description='Ping++ python bindings',
49+
long_description=long_description,
4650
author='Ping++',
4751
author_email='[email protected]',
4852
url='https://pingxx.com/',

0 commit comments

Comments
 (0)