Search code examples
pythonflaskpython-requestsm4a

Save m4a file after using authorization header to download


I am trying to download the m4a file of a recorded Zoom meeting from Zoom cloud. Here is the Zoom documentation on completed recording webhooks I am referencing. Specifically, I am trying to implement the section where Zoom describes the download_token in its schema explanation.

This is what I have so far:

from flask import Flask, request
import sys
import pprint
pp = pprint.PrettyPrinter(indent=2)

import requests

app = Flask(__name__)
@app.route('/notification/', methods = ['GET', 'POST'])
def notification():
    if request.method == 'POST':
        content = request.json
        # pp.pprint(content)
        if content['event'] == 'recording.completed':
            process_recording(content['download_token'], content['payload']['object']['recording_files'])
    return 'This should be where the webhook sends info to'

def process_recording(download_token, recordings_list):
    recording = next(
                (recording for recording in recordings_list if recording["recording_type"] == 'audio_only'),
                None)
    
    url = recording['download_url']
    headers = {
        'content-type': 'application/json',
        'authorization': 'Bearer ' + download_token + ' --header content-type:'
    }

    response = requests.get(url, headers=headers)

    if response:
        print('Success!')
    else:
        print('An error has occurred.')

Am I converting the curl command correctly? The curl command should be in this format:

curl --request GET \
  --url (download_url) \
  --header 'authorization: Bearer (download_token) \
  --header 'content-type: application/json'

Furthermore, how do I save the m4a file? What exactly does my response variable store?


Solution

  • Your header is wrong...

    compare...

        headers = {
            'content-type': 'application/json',
            'authorization': 'Bearer ' + download_token + ' --header content-type:'
        }
    

    with

      --header 'authorization: Bearer (download_token) \
      --header 'content-type: application/json'
    

    The value for the authorization header should be

    "Bearer (%s)" % download_token
    

    or

    f"Bearer ({download_token})"
    

    if you use Python 3.6 or higher.