error message in microsoft emotion api video python3 - python-3.x

Im trying to get the emotions from a video
Below is my code,
Always when I run this code i get this error
b'{"error":{"code":"BadArgument","message":"Failed to deserialize JSON request."}}' any idea why?
import http.client, urllib.request, urllib.parse, urllib.error, base64, sys
headers = {
'Content-Type': 'application/json',
'Ocp-Apim-Subscription-Key': 'xxxxxxxxxxx',
}
params = urllib.parse.urlencode({
})
body = "{ 'url': 'http://www.dropbox.com/s/zfmaswf8s9c58om/blog2.mp4' }"
try:
conn = http.client.HTTPSConnection('westus.api.cognitive.microsoft.com')
conn.request("POST", "/emotion/v1.0/recognizeinvideo?%s" % params, "
{body}", headers)
response = conn.getresponse()
data = response.read()
print(data)
conn.close()
except Exception as e:
print(e.args)

You forgot to substitute the placeholder {body} with the real thing.
conn.request("POST", "/emotion/v1.0/recognizeinvideo?%s" % params, body, headers)

Related

Upload attachments using clickup api

I'm trying to upload the attachments to a task on clickup.
Clickup API
Click up does provide example code the below is clickup example code
from urllib2 import Request, urlopen
values = """
attachment: raw_image_file (file)
filename: example.png (string)"""
headers = {
'Authorization': '\'access_token\'',
'Content-Type': 'multipart/form-data'
}
request = Request('https://private-anon-f799579c66-clickup20.apiary-mock.com/api/v2/task/{task_id}/attachment?custom_task_ids=&team_id=
', data=values, headers=headers)
response_body = urlopen(request).read()
print response_body
I used it as reference and used requests lib for the project
Here's the code using request lib
import requests
attachment_headers = {'Authorization': self.access_token, 'Content-Type': 'multipart/form-data'}
r = requests.post(f"https://private-anon-df9b125a00-clickup20.apiary-mock.com/api/v2/task/{task_id}/attachment",
files={"attachment": ("attachment", open("attachment.png", "rb")), "filename": "example.png"},
headers=attachment_headers)
print(r)
print(r.json())
I do get the status code as 200 and no error message but when I check on clickup task it doesn't show any attachments
Thanks for the help in advance!
You must remove the 'filename' part of your files param.
file = {"attachment": ('choose_your_name.png', open('file.png', 'rb'))}
headers = {'Authorization': config.clickup_access_token}
request = requests.post(f'https://api.clickup.com/api/v2/task/{task_id}/attachment', files=file, headers=headers)
print(request.json())

how to add content_type explicitly for json and file in post request

I would like to add content type explicity for multipart form data before sending post request
Below is my sample code i managed to add conten type for file data but couldn't figure out how to add content type correctly for json data, i would like to add "application/json; charset=utf-8" for json data
import requests
import json
import traceback
def uploadLogs(fileName):
f = open(fileName, 'rb')
payload = { "var1":"this", "var2" : "that"
}
files = {'file': ('current', f, "text/plain; charset=us-ascii")}
data = {'info': json.dumps(payload)}
headers = {'type': 'myReport', "Keep-Alive": "timeout=100"}
try:
url = "http://localhost:8009/upload"
response = requests.post(url, data=data, files=files, headers=headers)
print(response.request.body)
print(response.request.headers)
print(response.status_code)
if (response != None and (response.status_code == 200 or
response.status_code == 201)):
return True
except:
traceback.print_exc()
return False
filename = "C:\\sample.txt"
print(uploadLogs(filename))
If someone knows how to do please suggest

How to upload video as a multipart/form body content in Python for Video Indexer API?

I am trying to upload a video file from local machine using VideoIndexer API in Python. I am expected to upload video as a multipart/form body content. Following is my code (Variable values like accountID, name, location are not pasted below),
# Request headers
headers = {
'x-ms-client-request-id': '',
'Content-Type': 'multipart/form-data',
'Ocp-Apim-Subscription-Key': 'xxxxxxxxx',
}
# Request parameters
params = urllib.parse.urlencode({
'privacy': 'Private',
'description': 'testing',
})
try:
conn = http.client.HTTPSConnection('api.videoindexer.ai')
conn.request("POST", "/" + location + "/Accounts/" + accountId + "/Videos?name=" + name + "&accessToken=" + accessToken + "&%s" % params, body = open('TestAudio1.mp4', 'rb').read(), headers = headers)
response = conn.getresponse()
data = response.read()
print(data)
conn.close()
except Exception as e:
print("[Errno {0}] {1}".format(e.errno, e.strerror))
I am getting the following error,
{"ErrorType":"INVALID_INPUT","Message":"Input is invalid. Input must specify either a video url, an asset id or provide a multipart content body."}
How can I fix this issue?

Microsoft Cognitive Services - Speaker Recognition API - Verification - error-SpeakerInvalid

I am still facing the error in the verification process
{"error":{"code":"BadRequest","message":"SpeakerInvalid"}}'
My audio is correct as it is getting enrolled easily
##code for API CALL speaker verification
import http.client, urllib.request, urllib.parse, urllib.error, base64
subscription_key = 'XXXXXXXXXXXXXXXXXXXXXXX'
headers = {
# Request headers
"Content-Type": 'multipart/form-data',
"Ocp-Apim-Subscription-Key": subscription_key,
}
params = urllib.parse.urlencode({
'verificationProfileId':'445b849b-6418-4443-961b-77bd88196223',
})
#body = {
#}
try:
conn = http.client.HTTPSConnection('speaker-recognition-api.cognitiveservices.azure.com')
body = open('pp.wav','rb') //pp.wav is my audio file
conn.request("POST", "/spid/v1.0/verify?verificationProfileId=445b849b-6418-4443-961b-77bd88196223?%s" % params, body, headers)
response = conn.getresponse()
data = response.read()
print(data)
conn.close()
except Exception as e:
print("[Errno {0}] {1}".format(e.errno, e.strerror))
I could reproduce your problem. You are getting this error cause there is a ? in the end of your url, however behind verify there is already a ?. So if you want to add params to your request url you should use & just like the sample code in this API doc:Speaker Recognition - Verification .
Below is my work code.
try:
conn = http.client.HTTPSConnection('geospeaker.cognitiveservices.azure.com')
body=open("output4.wav","rb")
conn.request("POST", "/spid/v1.0/verify?verificationProfileId=1ae143b0-c301-4345-9295-3e34ad367092?%s" % params, body, headers)
response = conn.getresponse()
data = response.read()
print(data)
conn.close()
except OSError as e:
print("[Errno {0}] {1}".format(e.errno, e.strerror))

How to upload a video file using python in video indexer api?

I am trying to upload a video in Video Indexer API using Python:
import http.client, urllib.request, urllib.parse, urllib.error, base64
headers = {
# Request headers
'Content-Type': 'multipart/form-data',
'Ocp-Apim-Subscription-Key': '******************',
}
params = urllib.parse.urlencode({
# Request parameters
'name': 'xxxx',
'privacy': 'Private',
'language': 'English',
})
try:
conn = http.client.HTTPSConnection('videobreakdown.azure-api.net')
conn.request("POST", "/Breakdowns/Api/Partner/Breakdowns?%s" % params, "{body}", headers)
response = conn.getresponse()
data = response.read()
print(data)
conn.close()
except Exception as e:
print("[Errno {0}] {1}".format(e.errno, e.strerror))
But I am not able to specify how to give the video file in the {body} section.
Kindly help me.
This works for me:
import requests
import urllib.parse
import json
headers = {
'Ocp-Apim-Subscription-Key': 'YOUR-API-KEY',
}
form_data = {'file': open('YOUR-VIDEO.mp4', 'rb')}
params = urllib.parse.urlencode({
'name': 'video.mp4',
'privacy': 'Private',
'language': 'English',
})
try:
url = 'https://videobreakdown.azure-api.net/Breakdowns/Api/Partner/Breakdowns?'
r = requests.post(url, params=params, files=form_data, headers=headers)
print(r.url)
print(json.dumps(r.json(), indent=2))
except Exception as e:
print("[Errno {0}] {1}".format(e.errno, e.strerror))

Resources