Ради интереса решил попробовать. Вот так удалось загрузить фото в свой альбом:
; Выполнение запросов к API ВКонтакте: https://vk.com/dev/api_requests
; Загрузка файлов: https://vk.com/dev/upload_files
accessToken := "cfcc912ff7d7d2d83e4bb99f89b81ecd1b5061d5c48cde42a7eca6" ; указать свой access_token
albumId := 102453589 ; album_id
filePath := "D:\Downloads\Иконки\test.png"
JS := CreateScriptObj()
url := GetUrl(accessToken, albumId, JS)
SendFile(filePath, url, JS, server, photos_list, aid, hash)
MsgBox, % SaveFile(accessToken, server, photos_list, aid, hash)
GetUrl(accessToken, albumId, JS) {
queryUrl := "https://api.vk.com/method/photos.getUploadServer?album_id=" . albumId . "&access_token=" . accessToken . "&v=5.95."
whr := ComObjCreate("WinHttp.WinHttpRequest.5.1")
whr.Open("GET", queryUrl, false)
whr.Send()
if (whr.Status != 200)
throw Exception(whr.responseText)
Return JS.("JSON.parse('" . whr.ResponseText . "').response.upload_url")
}
SendFile(filePath, url, JS, ByRef server, ByRef photos_list, ByRef aid, ByRef hash) {
CreateFormData(postData, hdr_ContentType, {file: [filePath]})
whr := ComObjCreate("WinHttp.WinHttpRequest.5.1")
whr.Open("POST", url, false)
whr.SetRequestHeader("Content-Type", hdr_ContentType)
whr.Send(postData)
if (whr.Status != 200)
throw Exception(whr.responseText)
else {
responseText := whr.responseText
for k, v in ["server", "photos_list", "aid", "hash"]
%v% := StrReplace(JS.("JSON.parse('" . StrReplace(responseText, "\""", "|") . "')." . v), "|", """")
}
}
SaveFile(accessToken, server, photos_list, aid, hash) {
queryUrl := "https://api.vk.com/method/photos.save?server=" . server
. "&photos_list=" . URIEncode(photos_list)
. "&aid=" . aid
. "&hash=" . hash
. "&album_id=" . aid
. "&access_token=" . accessToken . "&v=5.95."
whr := ComObjCreate("WinHttp.WinHttpRequest.5.1")
whr.Open("GET", queryUrl, false)
whr.Send()
Return % whr.responseText
}
CreateScriptObj() {
static doc
doc := ComObjCreate("htmlfile")
doc.write("<meta http-equiv='X-UA-Compatible' content='IE=9'>")
Return ObjBindMethod(doc.parentWindow, "eval")
}
URIEncode(str, encoding := "UTF-8") {
VarSetCapacity(var, StrPut(str, encoding))
StrPut(str, &var, encoding)
While code := NumGet(Var, A_Index - 1, "UChar") {
bool := (code > 0x7F || code < 0x30 || code = 0x3D)
UrlStr .= bool ? "%" . Format("{:02X}", code) : Chr(code)
}
Return UrlStr
}
CreateFormData(ByRef retData, ByRef retHeader, objParam) {
new CreateFormData(retData, retHeader, objParam)
}
class CreateFormData
{
__New(ByRef retData, ByRef retHeader, objParam) {
CRLF := "`r`n", i, k, v, str, pvData
; Create a random Boundary
Boundary := this.RandomBoundary()
BoundaryLine := "------------------------------" . Boundary
this.Len := 0 ; GMEM_ZEROINIT|GMEM_FIXED = 0x40
this.Ptr := DllCall("GlobalAlloc", UInt, 0x40, UInt, 1, Ptr)
; Loop input paramters
for k, v in objParam
{
if IsObject(v) {
for i, FileName in v
{
str := BoundaryLine . CRLF
. "Content-Disposition: form-data; name=""" . k . """; filename=""" . FileName . """" . CRLF
. "Content-Type: " . this.MimeType(FileName) . CRLF . CRLF
this.StrPutUTF8( str )
this.LoadFromFile( Filename )
this.StrPutUTF8( CRLF )
}
}
else {
str := BoundaryLine . CRLF
. "Content-Disposition: form-data; name=""" . k """" . CRLF . CRLF
. v . CRLF
this.StrPutUTF8( str )
}
}
this.StrPutUTF8( BoundaryLine . "--" . CRLF )
; Create a bytearray and copy data in to it.
retData := ComObjArray(0x11, this.Len) ; Create SAFEARRAY = VT_ARRAY|VT_UI1
pvData := NumGet( ComObjValue( retData ) + 8 + A_PtrSize )
DllCall("RtlMoveMemory", Ptr, pvData, Ptr, this.Ptr, Ptr, this.Len)
this.Ptr := DllCall("GlobalFree", Ptr, this.Ptr, Ptr)
retHeader := "multipart/form-data; boundary=----------------------------" . Boundary
}
StrPutUTF8( str ) {
ReqSz := StrPut( str, "utf-8" ) - 1
this.Len += ReqSz ; GMEM_ZEROINIT|GMEM_MOVEABLE = 0x42
this.Ptr := DllCall("GlobalReAlloc", Ptr, this.Ptr, UInt, this.len + 1, UInt, 0x42)
StrPut(str, this.Ptr + this.len - ReqSz, ReqSz, "utf-8")
}
LoadFromFile( Filename ) {
objFile := FileOpen( FileName, "r" )
this.Len += objFile.Length ; GMEM_ZEROINIT|GMEM_MOVEABLE = 0x42
this.Ptr := DllCall("GlobalReAlloc", Ptr, this.Ptr, UInt, this.len, UInt, 0x42)
objFile.RawRead( this.Ptr + this.Len - objFile.length, objFile.length )
objFile.Close()
}
RandomBoundary() {
str := "0|1|2|3|4|5|6|7|8|9|a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z"
Sort, str, D| Random
str := StrReplace(str, "|")
Return SubStr(str, 1, 12)
}
MimeType(FileName) {
n := FileOpen(FileName, "r").ReadUInt()
Return (n = 0x474E5089) ? "image/png"
: (n = 0x38464947) ? "image/gif"
: (n&0xFFFF = 0x4D42 ) ? "image/bmp"
: (n&0xFFFF = 0xD8FF ) ? "image/jpeg"
: (n&0xFFFF = 0x4949 ) ? "image/tiff"
: (n&0xFFFF = 0x4D4D ) ? "image/tiff"
: "application/octet-stream"
}
}