Hello I had a problem loading the image onto the user's wall. At first I thought that I wasn’t correctly sending POST easily, but as it turned out the problem is that I don’t get the server URI where I need to upload images. Here is the code.

WebRequest webRequest = WebRequest.Create("https://api.vk.com/method/photos.getWallUploadServer?user_id="+userId+"&access_token="+token); WebResponse webResponse = webRequest.GetResponse(); Stream stream = webResponse.GetResponseStream (); //тут я получаю URI stream.Write(arr, 1 , arr.Length); string jsonR = Encoding.UTF8.GetString(arr); var jso = (JObject)JsonConvert.DeserializeObject(jsonR); string url = jso["response"]["upload_url"].ToString(); //POST запрос var myWebClient = new WebClient(); byte[] responseArray = myWebClient.UploadFile(url, filePath); string jsonResponse = Encoding.UTF8.GetString(responseArray); var json = (JObject)JsonConvert.DeserializeObject(jsonResponse); string photo = json["response"]["photo"].ToString(); string server = json["response"]["server"].ToString(); string hash = json["response"]["hash"].ToString(); //загрузка изображения WebRequest.Create("https://api.vk.com/method/photos.saveWallPhoto.xml?aceess_token="+ token+"&server="+server+"&photo="+photo+"&hash="+hash); xmlDocument.Load (stream); string photoLoad = xmlDocument.SelectSingleNode("responce/id").InnerText; string URI = "https://api.vk.com/method/wall.post.xml?"; URI +="access_token="+ token; URI += "&owner_id="+userId; URI += "&attachments=photo"+userId+"_"+photoLoad; URI += ",note"+userId+"_"+stext; URI += "&message="+stext; WebRequest.Create(URI); 

    1 answer 1

    Method for uploading photos and comments on the wall:

     using Newtonsoft.Json; using Newtonsoft.Json.Linq; // ... static string Upload(string userid, string token, string imagePath, string text) { var c = new WebClient(); // var u = "https://api.vk.com/method/photos.getWallUploadServer?user_id=" + userid + "&access_token=" + token; var r = c.DownloadString(u); var j = JsonConvert.DeserializeObject(r) as JObject; // var u2 = j["response"]["upload_url"].ToString(); var r2 = Encoding.UTF8.GetString(c.UploadFile(u2, "POST", imagePath)); var j2 = JsonConvert.DeserializeObject(r2) as JObject; // var u3 = "https://api.vk.com/method/photos.saveWallPhoto?access_token=" + token + "&server=" + j2["server"] + "&photo=" + j2["photo"] + "&hash=" + j2["hash"]; var r3 = c.DownloadString(u3); var j3 = JsonConvert.DeserializeObject(r3) as JObject; // var u4 = "https://api.vk.com/method/wall.post?access_token=" + token + "&owner_id" + j3["response"][0]["owner_id"] + "&message=" + text + "&attachments=" + j3["response"][0]["id"]; return c.DownloadString(u4); } 

    To compile the code you need to connect the assembly Newtonsoft.Json.dll
    And access_token should be obtained for Photos .

    • I apologize, but I just copied it wrong here ... I have a request from the server, no spaces: " api.vk.com/method/photos.getWallUploadServer?user_id= " + userId + "& access_token =" + token And rights to Photos, I get the authorization - user197132
    • "Token for Photos, and how to get it?" - just like you got access_token , only when you refer to https://oauth.vk.com/authorize you must specify / add 4 to the value of scope. - Stack
    • var auth = new OAuth2Authenticator (clientId: "", scope: "wall, photos, notes, pages", authorizeUrl: new System.Uri (" oauth.vk.com/authorize" ), redirectUrl: new System.Uri (" oauth .vk.com / blank.html " )); When logging in, I’ll get everything, post it just post it on the user's wall, but when I send a request, for some reason I don’t get an answer, or I don’t get the right message ... but I checked the request myself by letter and right - user197132
    • var httpClient = new HttpClient (); var httpRequest = httpClient.GetStringAsync (" api.vk.com/method/photos.getWallUploadServer?user_id="+ userId +" & access_token = "+ token); var result = httpRequest.Result; var js2 = (JObject) JsonConvert.DeserializeObject (result); string url = js2 ["response"] ["upload_url"]. ToString (); Do I extract the result correctly? - user197132
    • Thanks, I’m already receiving the URL, but for some reason I’m not already recording the attributes of the photo already loaded by the request. var myWebClient = new WebClient (); byte [] responseArray = myWebClient.UploadFile (url, filePath); string jsonResponse = Encoding.UTF8.GetString (responseArray); var json = (JObject) JsonConvert.DeserializeObject (jsonResponse); string photo = json ["response"] ["photo"]. ToString (); string server = json ["response"] ["server"]. ToString (); string hash = json ["response"] ["hash"]. ToString (); On parsing it wafts - user197132