Skip to content

Instantly share code, notes, and snippets.

@arschles
Created May 31, 2012 06:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arschles/2841555 to your computer and use it in GitHub Desktop.
Save arschles/2841555 to your computer and use it in GitHub Desktop.
StackMob with Python
import oauth.oauth as oauth
import httplib
import json
import sys
PUBLIC_KEY = "68023efc-6b09-42d0-9b43-18c531d79a98"
PRIVATE_KEY = "f41c5002-64c0-46c1-b249-b667866ba305"
class Client:
def __init__(self, url, key, secret):
self.url = url
self.connection = httplib.HTTPConnection(self.url)
self.consumer = oauth.OAuthConsumer(key, secret)
def send_push(self, username, message):
request = oauth.OAuthRequest.from_consumer_and_token(self.consumer, http_method="POST", http_url="http://" + self.url + "/push_users_universal")
request.sign_request(oauth.OAuthSignatureMethod_HMAC_SHA1(), self.consumer, None)
headers = request.to_header()
headers["Content-Type"] = "application/json"
headers["Accept"] = "application/vnd.stackmob+json; version=0"
body = {"kvPairs": {"message": message}, "userIds": [username]}
self.connection.set_debuglevel(1)
self.connection.request(request.http_method, "/push_users_universal", body=json.dumps(body), headers=headers)
return self.connection.getresponse()
def main():
success = True
versions = set()
client = Client("push.mob1.stackmob.com", PUBLIC_KEY, PRIVATE_KEY)
response = client.send_push("user", "message")
print response.status
print response.read()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment