ObexCode Logo ObexCode Logo

How to request a setup SMS

Here is a code example in Python on how to request a setup-SMS from the Sync Server API.

 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
#!/usr/bin/python
"""
This test is used to let the Sync Server API send a setup SMS to a registered phone.

You run this test you need
- server info (server and port)
- OAuth API and access token
- A binding ID (for a phone)
"""

import httplib
import oauth.oauth as oauth

# There are seven (or eight) settings which have to be manually set prior to running this example.
# First, the six OAuth parameters
SERVER = "<yourserver.com>"  # The server on which Sync Server API is installed
PORT = <your portnumber>  # The server port number 
CONSUMER_KEY = "<your consumer key>"  # Your OAuth API token key (see "My Profile")
CONSUMER_SECRET = "<your consumer secret>"  # Your Oauth API token secret ("My Profile")
ACCESS_TOKEN_KEY = "<your access token key>"  # Your OAuth API request token key (see "My Profile")
ACCESS_TOKEN_SECRET = "<your access token secret>"  # Your Oauth API request token secret ("My Profile")
# Then, the binding stuff
BINDING_ID = <your binding ID> # The binding ID returned after binding the phone to datastore.
PINCODE = "1881" # Optional, if not set "1234" is used.

# The protected resource's URL. Port 80 needs special treatment due to OAuth signature calculation.
if PORT == 80:
    RESOURCE_URL = "http://%s/v2/syncengine/phone_datastore_bindings/%d/setup" % (SERVER, BINDING_ID)
else:
    RESOURCE_URL = "http://%s:%d/v2/syncengine/phone_datastore_bindings/%d/setup" % (SERVER, PORT, BINDING_ID)

# HTTP method
HTTP_METHOD = "POST"

# Example client using httplib with headers
class SimpleOAuthClient(oauth.OAuthClient):

    def __init__(self, server, port=httplib.HTTP_PORT, access_token_key="", access_token_secret=""):
        self.server = server
        self.port = port
        self.access_token_key = access_token_key
        self.access_token_secret = access_token_secret
        self.connection = httplib.HTTPConnection("%s:%d" % (self.server, self.port))

    def access_resource(self, oauth_request):
        # Create Atom entry
        atom_template = """
            <entry xmlns="http://www.w3.org/2005/Atom"
                   xmlns:oc="http://schemas.obexcode.com/syncserver/2009">
              <oc:pincode>%(pincode)s</oc:pincode>
            </entry>
        """
        atom = atom_template % dict(pincode=PINCODE)

        # Fill the header with authorization, content type and accept info
        headers = oauth_request.to_header()
        headers["Content-Type"] = "application/atom+xml"
        headers["Accept"] = "application/atom+xml"
        self.connection.request(HTTP_METHOD, RESOURCE_URL, body=atom, headers=headers)
        response = self.connection.getresponse()
        return response.read()

def run_example():
    print
    print "** Example on how to request a setup SMS **"
    print

    # Initialize the OAuth client and connect it to the Sync Server API server
    client = SimpleOAuthClient(SERVER, PORT, ACCESS_TOKEN_KEY, ACCESS_TOKEN_SECRET)
    print " - OAuth client initialized."

    # Initialize the OAuth consumer using the configured key and secret
    consumer = oauth.OAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET)
    print " - OAuth consumer initialized."

    # Use plaintext signature method
    signature_method = oauth.OAuthSignatureMethod_HMAC_SHA1()
    print " - OAuth signature method set."

    # Pack request token as an OAuthToken object
    # Remember to authorize it prior to running this test!
    token=oauth.OAuthToken.from_string("oauth_token_secret=%s&oauth_token=%s" % (ACCESS_TOKEN_SECRET, ACCESS_TOKEN_KEY))
    print " - OAuth request token packed as OAuthToken object."

    # Ask for setup sms!
    print
    print "Asking for setup SMS, using URL:\n%s..." % RESOURCE_URL
    oauth_request = oauth.OAuthRequest.from_consumer_and_token(consumer, token=token, http_url=RESOURCE_URL, http_method=HTTP_METHOD)
    oauth_request.sign_request(signature_method, consumer, token)

    print "...This was returned from the resource:"
    print
    print client.access_resource(oauth_request)

    print
    print "If successful, you will recieve an SMS in a few minutes."

if __name__ == '__main__':
    run_example()