Here is a code example in Python on how to register a binding between a phone and datastore
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 | #!/usr/bin/python """ This is an example on how to register a binding between a phone and datastore. """ import httplib import oauth.oauth as oauth # There are nine 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 three binding parameters MODEL_UUID = "c6c65a90-53a2-102b-beff-000ea60275d1" # Model ID - this is a Sony Ericsson W890i PHONE_NUMBER = "+4799999999" # Remember area code FOREIGN_USER_ID = "customer-id" # The protected resource's URL. Port 80 needs special treatment due to OAuth signature calculation. RESOURCE_PATH = "/v2/syncengine/phone_datastore_bindings" if PORT == 80: RESOURCE_URL = "http://%s%s" % (SERVER, RESOURCE_PATH) else: RESOURCE_URL = "http://%s:%d%s" % (SERVER, PORT, RESOURCE_PATH) # 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:model_uuid>%(model)s</oc:model_uuid> <oc:phone_number>%(phone_number)s</oc:phone_number> <oc:foreign_user_id>%(foreign_id)s</oc:foreign_user_id> </entry> """ atom = atom_template % dict(model=MODEL_UUID, phone_number=PHONE_NUMBER, foreign_id=FOREIGN_USER_ID) # 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" # Do the request! self.connection.request("POST", RESOURCE_URL, body=atom, headers=headers) response = self.connection.getresponse() return response.read() def run_example(): print print "** Example on how to register a binding between a phone and datastore **" 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." # Do the binding! print print "Trying to bind a phone with datastore ..." oauth_request = oauth.OAuthRequest.from_consumer_and_token(consumer, token=token, http_url=RESOURCE_URL, http_method="POST") oauth_request.sign_request(signature_method, consumer, token) print "...This was returned from the resource:" print print client.access_resource(oauth_request) if __name__ == '__main__': run_example() |