1

My application for sends and receives data from/to my phone - basically a 2-way communication through the pushbullet API.

I am trying to take a file from my phone and when it's uploaded do something with it, (play it for example if it's an audiofile).

But when I upload the file on my phone and then I list the pushes on my computer and get that exact push, the file-URLL is restricted.

I got following XML error-response showing "Access Denied" as message: XML error-response: Access Denied

403: Forbidden

How would I approach this?

Here is the code for the application:


def play_sound(url):
    #open the url and then write the contents into a local file
    open("play.mp3", 'wb').write(urlopen(url))
 
    #playsound through the playsound library
    playsound("play.mp3", False)


pb = pushbullet.Pushbullet(API_KEY, ENCRYPTION_PASSWORD)

pushes = pb.get_pushes()
past_pushes = len(pushes)
while True:
    time.sleep(3)

    # checks for new pushes on the phone and then scans them for commands
    pushes = pb.get_pushes()
    number_pushes = len(pushes) - past_pushes

    if number_pushes != 0:
        past_pushes = (len(pushes) - number_pushes)

        try:
            for i in range(number_pushes):
                push = pushes[i]

                push_body = push.get("body")

                if push_body is not None:
                    play = False

                    if push_body == "play":
                        play = True
                elif play:
                    #only runs if the user has asked to play something 
                    #beforehand

                    play = False
                    url = push.get('file_url')

                    #play sound from url
                    #this is where I get my 403: forbidden error
                    if url is not None and ".mp3" in url:
                        play_sound(url)

        except Exception as e:
            print(e)
bob bob
  • 15
  • 4
  • 1
    Sounds like you have not provided a valid api key to the request. You'll need to show your code if you want help – OneCricketeer Feb 07 '22 at 08:14
  • @OneCricketeer codepile.net/pile/R9mmbK9q here is my code. It's not the best I am only a beginner at python. note that there should be be a while True loop for the bottom code under the function. I just removed it so the code would be cleaner – bob bob Feb 07 '22 at 15:39
  • Please [edit] the question rather than use links – OneCricketeer Feb 07 '22 at 15:41
  • @OneCricketeer Thank you, I fixed it. – bob bob Feb 07 '22 at 16:08
  • 1) Where is `pb` defined? 2) You should give the caught exception a name and print it rather than simply "error". It might give you more information – OneCricketeer Feb 08 '22 at 14:49
  • @OneCricketeer pb is defined outside the while loop as shown in the updated code above. The exception only occurs if I upload the file after I ask for play. If the file is uploaded beforehand and I just pass it a link then the link will be visible and could be read. but if I just upload the file on my phone just on the spot then I get the url it will give an error, if I open the url in a browser it will show me the xml file. – bob bob Feb 08 '22 at 16:44
  • Like I said, an HTTP 403 would indicate one of `API_KEY` or `ENCRYPTION_PASSWORD` is not correct – OneCricketeer Feb 08 '22 at 16:48
  • Actually, sorry, no. It's `urlopen()` that creates an un-authenticated HTTP call – OneCricketeer Feb 08 '22 at 16:50
  • @OneCricketeer What do you mean by not correct. It was able to push files and read pushes, delete them an all the other things. I just grabbed the url and wrote it in a file and then played that file. Should I have read the file using the api and not just grab it? – bob bob Feb 08 '22 at 16:51
  • @OneCricketeer mhm, Then how do I make that call authenticated? – bob bob Feb 08 '22 at 16:54

1 Answers1

0

From the docs...

To authenticate for the API, use your access token in a header like Access-Token: <your_access_token_here>.

You're using urlopen(url) without any header information, so the request is denied.

So, try something like the following

from urllib.request import Request, urlopen

req = Request('https://dl.pushbulletusercontent.com/...')
req.add_header('Access-Token', '<your token here>')
content = urlopen(req).read()

with open('sound.mp3', 'wb') as f:
  f.write(content)

Reference: How do I set headers using python's urllib?

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • returns HTTP Error 403: Forbidden. If I change Access-Token to Authorization the error code changes to http error 401: unauthorized. Is the api key not the way to authorize my http call? – bob bob Feb 08 '22 at 17:31
  • I'm not really sure. I've not programmatically accessed Pushbullet. My only suggestion would be to inspect the http call the browser actually makes if you're not able to reach out to some developers for Pushbullet directly – OneCricketeer Feb 09 '22 at 15:25