-
Notifications
You must be signed in to change notification settings - Fork 0
/
square.py
75 lines (54 loc) · 2.54 KB
/
square.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
from __future__ import print_function
import uuid
import squareconnect
from squareconnect.rest import ApiException
from squareconnect.apis.orders_api import OrdersApi
from squareconnect.apis.locations_api import LocationsApi
# create an instance of the Transaction API class
api_instance = OrdersApi()
# setup authorization
api_instance.api_client.configuration.access_token = 'sq0csp-jtvhhszasG77z-op_JckYCA1FmlbykW5yaMnD3Q-8cM'
api_instance.batch_retrieve_orders('3KK1N9SVK8QP1')
##Connect OAuth Flow
from bottle import get, request, static_file, run, response
import squareconnect
from squareconnect.apis.o_auth_api import OAuthApi
from squareconnect.models.obtain_token_request import ObtainTokenRequest
OrdersApi.batch_retrieve_orders('3KK1N9SVK8QP1',BatchRetrieveOrdersRequest)
# Your application's ID and secret, available from your application dashboard.
application_id = 'REPLACE_ME'
application_secret = 'REPLACE_ME'
# OAuth API instance
oauth_api = OAuthApi()
# Serves the link that merchants click to authorize your application
@get('/')
def authorize():
return '''<a href="https://connect.squareup.com/oauth2/authorize?client_id={0}">Click here</a>
to authorize the application.'''.format(application_id)
# Serves requsts from Square to your application's redirect URL
# Note that you need to set your application's Redirect URL to
# http://localhost:8080/callback from your application dashboard
@get('/callback')
def callback():
# Extract the returned authorization code from the URL
authorization_code = request.query.get('code')
if authorization_code:
# Provide the code in a request to the Obtain Token endpoint
oauth_request_body = ObtainTokenRequest()
oauth_request_body.client_id = application_id
oauth_request_body.client_secret = application_secret
oauth_request_body.code = authorization_code
response = oauth_api.obtain_token(oauth_request_body)
if response.access_token:
# Here, instead of printing the access token, your application server should store it securely
# and use it in subsequent requests to the Connect API on behalf of the merchant.
print ('Access token: ' + response.access_token)
return 'Authorization succeeded!'
# The response from the Obtain Token endpoint did not include an access token. Something went wrong.
else:
return 'Code exchange failed!'
# The request to the Redirect URL did not include an authorization code. Something went wrong.
else:
return 'Authorization failed!'
# Start up the server
run(host='localhost', port=8080)