0

I'm working on a simple command-line Pushbullet Python project, and have the following code:

from settings import *
import urllib
import urllib2


def pushSms(number, message):
    url = 'https://api.pushbullet.com/v2/ephemerals'
    values = {
        "type": "push",
        "push": {
            "type": "messaging_extension_reply",
            "package_name": "com.pushbullet.android",
            "source_user_iden": settings["PUSHBULLET_USER_IDEN"],
            "target_device_iden": settings["PUSHBULLET_SMS_IDEN"],
            "conversation_iden": number,
            "message": message
        }
    }

    headers = {"Authorization" : "Bearer " + settings["PUSHBULLET_API_KEY"]}

    data = urllib.urlencode(values)
    req = urllib2.Request(url, data, headers)
    response = urllib2.urlopen(req)
    return response

Example usage might be pushSms("555 555 5555", "Hi there!").

This takes advantage of the Pushbullet android app access to SMS, as documented here. I've checked my settings variables and they're all valid (in fact, they're currently in use in a JavaScript version of nearly this exact code in another project of mine.

My suspicion is that this is a basic Python syntax/urllib2 misuse or error, but I've been staring/Googling for hours and can't see my error. Thoughts?

j6m8
  • 2,261
  • 2
  • 26
  • 34

1 Answers1

1

I can't tell for certain (the response from the server may contain more information), but because we accept both form encoded and json requests, you probably need to set the header "Content-Type: application/json" on the request.

If that's not the case, could you post the body of the 400 response?

Chris Pushbullet
  • 1,039
  • 9
  • 10
  • Confirmed that adding the content-type header resolved this problem. To wit, using the `requests` library turned out to be far easier than urllib... – j6m8 Jul 01 '15 at 18:02