-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_api.py
134 lines (104 loc) · 3.35 KB
/
test_api.py
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# -*- coding: utf-8 -*-
import requests
import urllib
import urllib2
try:
import urlparse
except ImportError:
import urllib.parse as urlparse
import threading
import json
import sys
import time
URL = "http://192.168.33.10:8888"
#URL = "http://api.goubuli.mobi"
access_token = ""
refresh_tokne = ""
def LoginNumber():
url = URL + "/verify_code?"
NUMBER = "13800000000"
values = {'zone' : '86', 'number' : NUMBER}
params = urllib.urlencode(values)
url += params
r = requests.post(url)
print r.content
resp = json.loads(r.content)
code = resp["code"]
url = URL + "/auth/token"
values = {"zone":"86", "number":NUMBER, "code":code}
data = json.dumps(values)
r = requests.post(url, data=data)
print r.content
resp = json.loads(r.content)
print "access token:", resp["access_token"]
print "refresh token:", resp["refresh_token"]
access_token = resp["access_token"]
refresh_token = resp["refresh_token"]
orgs = resp["organizations"]
org_id = orgs[0]['id']
url = URL + "/member/login_organization"
headers = {}
headers["Authorization"] = "Bearer " + access_token
headers['Content-Type'] = 'application/json'
values = {"org_id":org_id}
data = json.dumps(values)
r = requests.post(url, headers=headers, data=data)
print r.status_code, r.content
url = URL + "/auth/refresh_token"
headers = {}
headers["Authorization"] = "Bearer " + access_token
values = {"refresh_token":refresh_token}
data = json.dumps(values)
r = requests.post(url, data=data, headers = headers)
print r.content
resp = json.loads(r.content)
print "access token:", resp["access_token"]
print "refresh token:", resp["refresh_token"]
access_token = resp["access_token"]
refresh_token = resp["refresh_token"]
return access_token, refresh_token
def sync_contact():
url = URL + "/contact/sync?sync_key=0"
headers = {}
headers["Authorization"] = "Bearer " + access_token
#or
#headers['Refresh-Token'] = refresh_token
headers['Content-Type'] = 'application/json'
r = requests.get(url, headers=headers)
print r.status_code, r.content
#表单上传图片
def TestAvatar():
url = URL + "/avatars"
files = {'file': ('test.jpg', open('data/test.jpg', 'rb'), "image/jpeg")}
headers = {}
headers["Authorization"] = "Bearer " + access_token
r = requests.post(url, headers=headers, files=files)
print r.status_code, r.content
#二维码登录
def TestQRCode():
url = URL + "/qrcode/session"
r = requests.get(url)
assert(r.status_code == 200)
obj = json.loads(r.content)
sid = obj["sid"]
print "new sid:", sid
def scan_qrcode():
time.sleep(4)
headers = {}
headers["Authorization"] = "Bearer " + access_token
url = URL + "/qrcode/scan"
obj = {"sid":sid}
r = requests.post(url, headers=headers, data=json.dumps(obj))
print "scan:", r.status_code
return
t = threading.Thread(target=scan_qrcode)
t.start()
url = URL + "/qrcode/login?sid=%s"%sid
r = requests.get(url)
assert(r.status_code == 200)
print "qrcode login success:", r.content
t.join()
access_token, refresh_token = LoginNumber()
TestAvatar()
#sync_contact()
#TestQRCode()