-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathfa_api.py
More file actions
51 lines (39 loc) · 2.15 KB
/
fa_api.py
File metadata and controls
51 lines (39 loc) · 2.15 KB
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
#!/usr/bin/env python
import urllib,simplejson,cgi
## Set some values we'll be needing through the process
CLIENT_ID="xxxx" # Issued by FormAssembly host
CLIENT_SECRET="xxxx" # Issued by FormAssembly host
RETURN_URL="https://localhost/" # We're doing commandline so this is pointless
AUTH_ENDPOINT="https://xxxxxx/oauth/login" # Replace xxxxxx with correct url
TOKEN_REQUEST_ENDPOINT="https://xxxxxx/oauth/access_token" # Replace xxxxxx with correct url
## API Endpoint to test
## https://server/api_v1/forms/index.xml
API_REQUEST="https://xxxxxx/api_v1/forms/index"
## Build the query string
API_AUTH_QUERY=urllib.urlencode({"type":"web",
"client_id":CLIENT_ID,
"redirect_uri":RETURN_URL,
"response_type":"code"})
## Build our authorization endpoint to display to user ('Adam')
AUTH_URI=AUTH_ENDPOINT+"?"+API_AUTH_QUERY
## Since we're on the commandline, display authorization url to user ('Adam').
print "Go to URL: "+AUTH_URI
print "When directed to https://localhost/?code=XXXXXXXXXX copy and paste code here: (do not include the ending #)\n"
code = raw_input("CODE>")
#Decode any URL encoded values
code = cgi.parse_qs("code="+code)["code"][0]
## We've got the code value, so we can now generate the server-side request for an access_token
TOKEN_REQUEST_DATA=urllib.urlencode({"grant_type":"authorization_code","type":"web_server","client_id":CLIENT_ID,"client_secret":CLIENT_SECRET,"redirect_uri":RETURN_URL,"code":code})
token_request_results=urllib.urlopen(TOKEN_REQUEST_ENDPOINT,TOKEN_REQUEST_DATA)
data = token_request_results.read()
print data
## Let's parse the response into an object from the json response, and grab the access_token
token=simplejson.loads(data)["access_token"]
format = raw_input("What format would you like the form list in? (json,xml,plist)> ")
## Make another server-side request for the API endpoint, using the access_token
FULL_API_REQUEST=API_REQUEST+"."+format+"?"+urllib.urlencode({"access_token":token})
api_response=urllib.urlopen(FULL_API_REQUEST)
## Display the output
print "\nURL:"+FULL_API_REQUEST+"\n"
for line in api_response:
print line