[英]Python, App Engine: HTTP multipart POST to Vk.Ads API
I tried to sole this problem myself for a few days searching on examples and documentations, also it wasn't solved on ruSO . 我花了几天的时间尝试寻找示例和文档来解决这个问题, 但是在ruSO上并没有解决 。 So, I hope that solution will be found here, on enSO.
因此,我希望能在enSO上找到解决方案。
I develop a service for automatic creation of ads at Vk social network using Python and Google App Engine. 我开发了一项服务,可使用Python和Google App Engine在Vk社交网络上自动创建广告。 Initially, pictures for ads are loaded to my server ( part 1 ), then they are uploaded to Vk server at some time ( parts 2.1 and 2.2 ).
最初,广告图片会加载到我的服务器( 第1部分 ),然后有时将它们上传到Vk服务器(第2.1和2.2部分 )。 It seems that pictures are loaded and stored on my server correctly (I downloaded them and compared with original ones — every byte is the same).
似乎图片已正确加载并存储在我的服务器上(我下载了图片并与原始图片进行了比较-每个字节都相同)。 But I attach the part 1 code just in case.
但是我附上第1部分的代码以防万一。
To upload a picture to Vk.Ads firstly I need to get a URL — this is simple, so skip it. 要将图片上传到Vk.Ads,首先我需要获取一个URL ,这很简单,因此请跳过它。 Secondly, I need to send a POST request to this link with field
file
with binary content of the photo ( API documentation ). 其次,我需要使用带有照片二进制内容的字段
file
向该链接发送POST请求( API文档 )。 I created two ways for that ( 2.1 and 2.2 ), but both of them returns errcode: 2
which means corrupted file
. 我为此创建了两种方法( 2.1和2.2 ),但是它们都返回
errcode: 2
表示corrupted file
。 To my mind, the problem is about the requests, but I don't exclude the possibility of it files uploading/storage on my server, or some strange work of the API. 在我看来,问题在于请求,但我不排除文件在服务器上上载/存储或API的某些奇怪工作的可能性。 I'll appreciate any answers and comments.
我将不胜感激任何答案和评论。
import webapp2
from google.appengine.ext import ndb
# stores pictures on the server
class Photo(ndb.Model):
name = ndb.StringProperty()
img = ndb.BlobProperty()
@staticmethod
def get(name):
retval = Photo.query(Photo.name == name).get()
return retval
@staticmethod
def create(name, blob):
retval = Photo()
retval.name = name
retval.img = blob
return retval
class PhotosPage(webapp2.RequestHandler):
def get(self):
# general content of the page:
html = '''<form action="/photos" method="post" enctype="multipart/form-data">
<input type="file" name="flimg"/>
<input value="new_pic" name="flname"/>
<input type="submit" value="Upload"/> </form>'''
def post(self):
n = str(self.request.get('flname'))
f = self.request.get('flimg')
p = Photo.get(n)
if p:
p.img = f
else:
p = Photo.create(n, f)
p.put()
from poster.encode import multipart_encode, MultipartParam
from google.appengine.api import urlfetch
name = 'file'
content = ... # file binary content
where = ... # gotten URL
options = {
'file': MultipartParam(
name=name,
value=content,
filename=name,
filetype='image/png',
filesize=len(content))
}
data, headers = multipart_encode(options)
pocket = "".join(data)
result = urlfetch.fetch(
url=where,
payload=pocket,
method=urlfetch.POST,
headers=headers)
requests
: requests
POST到API,方法2: import requests
name = 'file'
content = ... # file binary content
where = ... # gotten URL
# I also tried without this dict; is it necessary?
data = {
'fileName': name,
'fileSize': len(content),
'description': 'undefined',
}
result = requests.post(where, files={name: StringIO(content)}, data=data)
In addition, for the second approach I extracted the content of my request: 另外,对于第二种方法,我提取了请求的内容:
POST
https://pu.vk.com/c.../upload.php?act=ads_add&mid=...&size=m&rdsn=1&hash_time=...&hash=...&rhash=...&api=1
Content-Length: 15946
Content-Type: multipart/form-data; boundary=b4b260eace4e4a7082a99753b74cf51f
--b4b260eace4e4a7082a99753b74cf51f
Content-Disposition: form-data; name="description"
undefined
--b4b260eace4e4a7082a99753b74cf51f
Content-Disposition: form-data; name="fileSize"
15518
--b4b260eace4e4a7082a99753b74cf51f
Content-Disposition: form-data; name="fileName"
file
--b4b260eace4e4a7082a99753b74cf51f
Content-Disposition: form-data; name="file"; filename="file"
< File binary content >
--b4b260eace4e4a7082a99753b74cf51f--
Thanks to SwiftStudier , I found the origin of the problem: StringIO
and BytesIO
don't behave identical to file open
. 感谢SwiftStudier ,我找到了问题的根源:
StringIO
和BytesIO
行为与file open
不一致。 If I use just open
the code works well, but it doesn't with virtual file. 如果我只使用
open
代码,效果很好,但不能用于虚拟文件。 How it can be solved? 如何解决?
import requests
from io import BytesIO
with open('path.to/file.png', 'rb') as fin:
content = BytesIO(fin.read())
token = '...'
url = 'https://api.vk.com/method/ads.getUploadURL?access_token=' + token + '&ad_format=2'
upload_url = requests.get(url).json()['response']
post_fields = {
'access_token': token
}
data_fields = {
# This works:
# 'file': open('path.to/file.png', 'rb')
# But this does not:
'file': content
}
response = requests.post(upload_url, data=post_fields, files=data_fields)
print(response.text)
Not sure if it can help, but I'll post it anyway 不确定是否可以提供帮助,但是我还是会贴出来
I used requests
to upload an image to ads
我使用
requests
将图片上传到ads
import requests
token = '***'
url = f'https://api.vk.com/method/ads.getUploadURL?access_token={token}&ad_format=1' # I set add_format randomly just to avoid an error of this parameter was missing
upload_url = requests.get(url).json()['response']
post_fields = {
'access_token': token
}
data_fields = {
'file': open('/path/to/image.png', 'rb')
}
response = requests.post(upload_url, data=post_fields, files=data_fields)
print(response.text)
The result looks like valid photo upload, the data received can be used in further actions with ad API. 结果看起来像是有效的照片上传,接收到的数据可用于广告API的进一步操作。
After a lot of experiments and investigating of different HTTP requests content I found out the only difference between wrong and working code. 经过大量实验并研究了不同的HTTP请求内容,我发现了错误代码与有效代码之间的唯一区别。 It was about 4 bytes only: file name MUST contain the extension.
它只有大约4个字节:文件名必须包含扩展名。 Vk API even ignores
Content-Type: image/png
, but needs .png
or similar in filename. Vk API甚至会忽略
Content-Type: image/png
,但需要.png
或类似的文件名。 So, this doesn't work: 因此,这不起作用:
requests.post(upload_url, files={
'file': BytesIO('<binary file content>')
})
But this option works properly: 但是此选项正常工作:
requests.post(upload_url, files={
'file': ('file.png', BytesIO('<binary file content>'), 'image/png')
})
Just like this one, which is not available for GAE: 就像这样,GAE不可用:
requests.post(upload_url, files={
'file': open('/path/to/image.png', 'rb')
})
Both StringIO
and StringIO
are appropriate for that task. StringIO
和StringIO
均适合该任务。 As mentioned, Content-Type
does not matter, it can be just multipart/form-data
. 如前所述,
Content-Type
无关紧要,它可以只是multipart/form-data
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.