I integrate requests_toolbelt with Google Photos api, the video file does not open even though the file is the same size.
The video upload process is successful, but the video is corrupted and cannot be opened. It gives 0xc00d36c4 error.
import os
import pickle
import requests
from requests_toolbelt import MultipartEncoder
import requests
url = 'https://photoslibrary.googleapis.com/v1/uploads'
token = pickle.load(open('token.pickle', 'rb'))
headers = {
'Authorization': 'Bearer ' + token.token,
'Content-type': 'application/octet-stream',
'X-Goog-Upload-Protocol' : 'raw',
'X-Goog-Upload-File-Name' : 'test.mp4'
}
media = MultipartEncoder(fields = {'file': open('test.mp4', 'rb')})
response = requests.post(url, data=media, headers=headers)
request_body = {
'newMediaItems': [
{
'simpleMediaItem': {
'uploadToken': response.content.decode('utf-8')
}
}
]
}
upload_response = service.mediaItems().batchCreate(body=request_body).execute()
Related
This is a python script to make a post call with respective header and body, but I am getting error Full authentication is required to access this resource
import requests
from requests.auth import HTTPBasicAuth
url = 'https://localhost:8000/bucket/items'
body = {
"serialNumbers": [
"AASD-ASD"
]
}
bearer_token = "07eb4900-016f-4523-a9b7-e5d6a87a7f8e"
jwt_token = "jhbasdfiunnX.faf-"
headers = {'content-type': 'application/json',
"authentication":"bearer "+ '07eb4900-a9b7-e5d6a87a7f8e',
"cookie":"jwt="+jwt_token}
headers = {"Cookie":"JWT="+jwt_token, "Authentication":"Bearer "+bearer_token, "Content-Type": "application/json" }
response = requests.post(url, body, auth=HTTPBasicAuth("abcd", "password"),headers=headers).json()
print(response)
import json
import requests
user_f = open('categories.json',)
users = json.load(user_f)
media_api = 'http://127.0.0.1:8000/api/categories/'
files=[
('category_image', ("Traditional-Art.png", open("Traditional-Art.png",'rb'),'image/jpeg'))
]
json = {
"category_name": "cat",
"category_description": "desc",
"user": 16
}
headers = {
"Accept": 'application/json, text/plain, */*',
"enctype":"multipart/form-data"
}
response = requests.request("POST", media_api, headers=headers, data=json, files=files)
print(response.json())
Here, i am trying to upload image to django rest-framework and getting below error
`{u'category_image': [u'Upload a valid image. The file you uploaded was either not an image or a corrupted image.']}`
this works when image is jpg format but, not working in png .
Am i missing anything or do i need to modify something ?
Please take a look
what you have done is wrong either you try to send the data as json or as form do something like it may help you.
import requests
from requests_toolbelt.multipart.encoder import MultipartEncoder
multipart_data = MultipartEncoder(
fields={
# a file upload field
'file': ('file.zip', open('file.zip', 'rb'), 'text/plain')
# plain text fields
'field0': 'value0',
'field1': 'value1',
}
)
response = requests.post('http://httpbin.org/post',
data=multipart_data,
headers={'Content-Type': multipart_data.content_type})
I hope your are fine.
I would like to upload an image in my wordpress library from my computer.
I wrote few lines in Python that use Wordpress API.
It is working when I use 'Content-Type': 'application/x-www-form-urlencoded'
However, the content added is not an image.
When I change it with 'Content-Type': 'image/jpg' I get an error message which says:
Error 403 please forward this error screen to the site owner
What should I do, contact my web hosting company ?
Thank you all for you help
Here is my code:
from requests_toolbelt.multipart.encoder import MultipartEncoder
import requests
import base64
import json
import time
import os
user = "user"
password = "pass"
url = "https://example.com/wp-json/wp/v2"
data_string = user + ':' + password
token = base64.b64encode(data_string.encode())
# headers={'Authorization': 'Basic ' + token.decode('utf-8'), 'Content-Type': 'image/jpg','Content-Disposition' : 'attachment; filename=%s'% "test.jpg"}
headers={'Authorization': 'Basic ' + token.decode('utf-8'), 'Content-Type': 'application/x-www-form-urlencoded','Content-Disposition' : 'attachment; filename=%s'% "test.jpg"}
video = {
"title": "test",
"description": "description",
"media-type": "image",
}
r = requests.post(url + "/media", headers=headers, json=video)
print(r.text)
I solved the problem with this code
from requests_toolbelt.multipart.encoder import MultipartEncoder
import requests
import base64
import json
import time
import os
user = "user"
password = "pass"
url = "https://example.com/wp-json/wp/v2"
data_string = user + ':' + password
token = base64.b64encode(data_string.encode())
headers = {'Authorization': 'Basic ' + token.decode('utf-8')}
image = {
"file": open("toto.png", "rb"),
"caption": "caption",
"description": "description",
}
r = requests.post(url + "/media", headers=headers, files=image)
print(r.text)
print(r)
I'm trying to test the Shazam API Detect feature on a mono sample as provided by this tutorial.
My code reads the raw audio file and sends it as base64 plaintext in the body of a POST requests to the Shazam API.
The audio file can be downloaded via this link:
import requests
import base64
def shazam(payload):
url = "https://shazam.p.rapidapi.com/songs/detect"
payload = open(payload,"rb").read()
payload = base64.b64encode(payload)
payload = str(payload)
headers = {
'x-rapidapi-host': "shazam.p.rapidapi.com",
'x-rapidapi-key':str(open("./api.txt","r").read().strip()),
'content-type': "text/plain",
'accept': "text/plain"
}
response = requests.request("POST", url, data=payload, headers=headers)
print(response.text)
shazam("/home/samples/mono.raw")
Any ideas where I'm going wrong?
I'am having problems with this API as well been searching up and down to find a solution :(
it work on me
from pydub import AudioSegment
import base64
import requests
import json
file_path="./test.raw"
url = "https://rapidapi.p.rapidapi.com/songs/detect"
encode_string = base64.b64encode(open(file_path, "rb").read())
payload=encode_string
print(type(payload))
headers = {
'content-type': "text/plain",
'x-rapidapi-key': "<<<you key>>>",
'x-rapidapi-host': "shazam.p.rapidapi.com"
}
response = requests.request("POST", url, data=payload, headers=headers)
print(json.dumps(json.loads(response.text)))
I am using the python code below to upload a file to our marketing automation tool Marketo. It works when I have the file downloaded locally (/home/tyron/Downloads/yoda.jpeg) but I would like to be able to upload a file straight from Dropbox to Marketo without having to download it inbetween because I will be hosting this code in Zapier.
What is the best way to do this upload when given a link to the dropbox file?
import requests
url = "xxxxxx/rest/asset/v1/files.json"
payload = {'name': 'test',
'file': '/home/tyron/Downloads/yoda.jpe',
'folder': '{"id":367,"type":"Folder"}'}
files = [
('file', open('/home/tyron/Downloads/yoda.jpeg','rb'))
]
headers = {
'Authorization': 'Bearer yyyyyyyy'
}
response = requests.request("POST", url, headers=headers, data = payload, files = files)
print(response.text.encode('utf8'))
Thanks to Greg's suggestion in the comments I was able to upload the file to Marketo using the temporary link endpoint from dropbox.
Python Code to Get Dropbox Endpoint
import re
from urllib.parse import urlparse
from urllib.parse import unquote
import requests
import json
link = "https://www.dropbox.com/preview/Telnyx/...../file"
path = urlparse(link).path
path = re.search("(/Telnyx.*$)",path).group(0).lower()
path = unquote(path)
print(path)
url = "https://api.dropboxapi.com/2/files/get_temporary_link"
payload = {"path": path}
headers = {
'Authorization': 'Bearer xxxxxxxxxxxxxx',
'Content-Type': 'application/json'
}
response = requests.request("POST", url, headers=headers, data = json.dumps(payload))
print(response)
link = re.search('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_#.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+', response.text).group(0)
return {'id': 1234, 'rawHTML': link}
Python Code to Upload to Marketo
import requests
import urllib
import json
import re
path = "https://dl.dropboxusercontent.com/apitl/1/......." #returned from previous function
f = urllib.request.urlopen(path)
mime_type = f.info().get_content_type()
f_ext = f.info().get_content_type().split("/")[1]
url = "https://xxx-xxx-xxx.mktorest.com/rest/asset/v1/files.json"
f_name = 'Name.' + f_ext
payload = {
'folder': '{"id":xxxx","type":"Folder"}'
}
headers = {
'Authorization': 'Bearer xxxxxxxx'
}
files = {'file': (f_name, f, mime_type)}
response = requests.request("POST", url, headers=headers, data = payload, files = files)
print(response.text.encode('utf8'))
f_url = re.search('"url":"(.*)","folder',response.text).group(1)
return {'id': 1234, 'rawHTML': f_url}