-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
135 lines (112 loc) · 4.67 KB
/
Copy pathapp.py
File metadata and controls
135 lines (112 loc) · 4.67 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
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
134
135
from flask import Flask, redirect, url_for, session, render_template
from authlib.integrations.flask_client import OAuth
from flask import request
import requests
app = Flask(__name__)
app.secret_key = "112233"
oauth = OAuth(app)
adobe = oauth.register(
name = "adobe",
client_id = "App-Client-ID",
client_secret = "App-Client-Secret",
access_token_url = "https://secure.adobesign.com/oauth/token",
authorize_url = "https://secure.adobesign.com/public/oauth/v2",
api_base_url = "https://secure.na1.adobesign.com/api/rest/v6/",
client_kwargs = {"scope":"user_read agreement_write widget_write widget_read"},
)
@app.route("/")
def home():
"""Home route."""
return "Welcome to Adobe Acrobat Sign OAuth Demo <a href='/login'>Login with Adobe</a>"
@app.route("/login")
def login():
"""Start the OAuth Flow."""
redirect_uri = url_for("authorize", _external=True)
return adobe.authorize_redirect(redirect_uri)
@app.route("/authorize")
def authorize():
"""Handle the callback from Adobe Sign."""
code = request.args.get("code")
if not code:
return "Error: Auth code note found"
token = adobe.authorize_access_token()
session["token"] = token
response = adobe.get("users/me")
if response.status_code == 200:
user_info = response.json()
session["user"]= user_info
return render_template("dashboard.html", user=user_info)
else:
return f"Test API call failed: {response.status_code} - {response.json()} - {token}"
@app.route("/upload-transient", methods =["GET", "POST"])
def upload_transient():
"""Handle transient document upload"""
if request.method == "POST":
if "file" not in request.files:
return "No file uploaded", 400
file = request.files["file"]
if file.filename == "":
return "No file selected", 400
upload_url = "https://api.na1.adobesign.com/api/rest/v6/transientDocuments"
headers = {
"Authorization": f"Bearer {session['token']['access_token']}",
}
files = {
"File": (file.filename, file.stream, file.mimetype),
"File-Name" : (None, file.filename)
}
response = requests.post(upload_url, headers = headers, files=files)
if response.status_code ==201:
response_data = response.json()
transient_document_id = response_data["transientDocumentId"]
return render_template(
"upload_success.html", transient_document_id=transient_document_id
)
else:
return f"Failed to upload document: {response.status_code} - {response.json()}"
return render_template("upload_transient.html")
@app.route("/create-webform", methods = ["POST"])
def create_webform():
"""Create a webform"""
try:
transient_document_id = request.form.get("transient_document_id")
print(f"Transient ID: {transient_document_id}")
if not transient_document_id:
return "Transient Document ID is required", 400
webform_url = "https://api.na1.adobesign.com/api/rest/v6/widgets"
headers = {
"Authorization": f"Bearer {session['token']['access_token']}",
"Content-Type":"application/json"
}
payload = {
"widgetParticipantSetInfo": {
"role": "SIGNER",
"memberInfos": [{
"email": ""}]},
"name": "Sample Web Form",
"fileInfos": [{"transientDocumentId":transient_document_id}],
"state": "ACTIVE"}
response = requests.post(webform_url, headers=headers, json=payload)
if response.status_code == 201:
response_data = response.json()
formId = response_data['id']
response2 = requests.get(webform_url, headers=headers)
formList = response2.json()
formList = formList['userWidgetList']
url = ""
for i in formList:
if i['id'] == formId:
url = i['url']
return f"Web form created! Access it here: <a href='{url}' target='_blank'>Web Form Link </a>"
else:
return f"Failed to create web form: {response.status_code} - {response.json()} - TRANSENT ID: {transient_document_id} - token: {session['token']['access_token']}"
except Exception as e:
return f"An error occured: {str(e)}"
@app.route("/logout")
def logout():
"""Logout the user."""
session.pop("user", None)
session.pop("token", None)
return redirect(url_for("home"))
if __name__ == "__main__":
app.run(ssl_context=("cert.pem", "key.pem"), debug=True)