Save m4a file after using authorization header to download - python

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?

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.

Related

Convert CURL API command to Python API using requests and json

I am connecting through an API to receive data. From the website API documentation, the instructions use either two CURL methods to connect the API; however, I need to connect using python.
1st Method
Curl Example
curl -d '' -X POST 'https://api.bcda.cms.gov/auth/token' \
-H "accept: application/json" \
-H "authorization: Basic <Client_Secret>"
My Python Conversion:
import requests
import json
url = 'https://api.bcda.cms.gov/auth/token'
headers = {"accept": "application/json", "authorization": 'Basic',
'<API_Key>': '<API_Secret>'}
r = requests.post(url = url, data ={}, headers = headers)
print(r)
2nd Method Curl
curl -d '' -X POST 'https://api.bcda.cms.gov/auth/token' \
--user <Client_Key>:<Client_Secret> \
-H "accept: application/json"
My 2nd Python conversion code:
import requests
import json
url = 'https://api.bcda.cms.gov/auth/token'
user = {"<Client_Key>":"<Client_Secret>", "accept": "application/json"}
r = requests.post(url = url, headers = user)
print(r)
I am receiving a 403 connection error, meaning "response status code indicates that the server understands the request but refuses to authorize it."
You should use auth parameter and not headers to convert --user option
headers = {'accept': 'application/json'}
r = requests.post(url=url, headers=headers, auth=(client_key, client_secret))

How to turn python dict into Curl

For some reasons my requests to an API is failing. This is the code:
def create_alert(alert_dict)
url = host + '/api/alerts'
headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer tokengen'
}
response = requests.request('POST', url, headers = headers, data=json.dumps(alert_dict))
return response
alert_dict = {
"priority": "1",
"to": to,
"from": from_
}
response = create_alert(alert_dict)
print(response)
The way I'm doing it is saving everything to a dictionary, then making the POST request with it. But for some reason im getting a 500 error.
This is the curl that is working
curl --location --request POST "http://newhost.com/api/alerts" \
--header "Content-Type: application/json" \
--header "Authorization: Bearer tokengen" \
--data "{
\"to\": \"sample#email.com\",
\"from\": \"from#email.com\",
\"priority\": \"1\",
}"
How should I convert my dict in a way that it would work just like the curl --data when requested

convert curl get to rest api to python

I have this curl command:
curl -X GET --header 'Accept: application/vnd.pagerduty+json;version=2' --header 'Authorization: Token token=y_NbAkKc66ryYTWUXYEu' 'https://api.pagerduty.com/services?time_zone=UTC&sort_by=name'
I need to convert it to python using requests library
import requests
def support(self):
services_list = requests.get('I need to convert the link to pass it here as a parameter')
import requests
def support():
headers = {
'Accept': 'application/vnd.pagerduty+json;version=2',
'Authorization': 'Token token=y_NbAkKc66ryYTWUXYEu'}
payloads = (
('time_zone', 'UTC'),
('sort_by', 'name'),)
services_list = requests.get('https://api.pagerduty.com/services', headers=headers, params=payloads)

Convert from curl python Request Error error 401

Im having trouble converting the curl request into a python code request.
Working Curl request
curl -X POST "http://xxxxxx" -H "accept: application/json" -H
"Content-Type: application/x-www-form-urlencoded" -H "Authorization:
Token 882a6ec053ff6dbac623eff400f67c0bb6ade399" -d "name=namename"
Not working python request
headers = {
'Authorization ': 'Token ' + "token",
'Content-Type': 'application/json',
}
data= {'name': "name"}
r = requests.post(
host_scheme + "://" + host_netloc + "/xxxxx",
data=json.dumps(data),
headers=headers
)
The response of the error is it cannot read the token {"detail": "Authentication credentials were not provided."} when using the python code above.
Any suggestions?
requests.post("http://xxxxxx",
data='name=namename',
headers={
"Authorization": "Token 882a6ec053ff6dbac623eff400f67c0bb6ade399",
"Content-Type": "application/x-www-form-urlencoded",
"accept": "application/json"
},
cookies={},
)
I used Uncurl. I had to remove the -X POST.

cURL to python Requests: form with file attachment

I have the following cURL request which works successfully:
curl https://daDomain.com/v2-beta/media \
--header "Authorization: Bearer TOKEN" \
--form media=#recording.mp3 \
--form 'configuration={"configuration":{"keywords":{"groups":["data"]}}}'
Below is the python-requests version of the code, which when called returns a 400 error saying a "mediaUrl is not a valid URL".
import requests
headers = {}
headers["Authorization"] = "Bearer TOKEN"
url = "https://daDomain.com/v2-beta/media"
configuration = '{"configuration":{"keywords":{"groups":["data"]}}}'
filepath = 'recording.mp3'
files = {'media' : open('filepath.mp3', 'rb'), 'configuration' : configuration}
r = requests.post(url, headers = headers, files=files)

Categories

Resources