vk.api to download the image on the user wall c#



  • Hello. I have a problem with downloading the image on the user wall. At first, I thought I was wrong to send the POST request, but the problem is, I don't get the URI server where to download the images. Here's 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); 
    


  • Method for loading photos and commenting 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, the assembly should be connected. Newtonsoft.Json.dll
    A access_token to be received for https://vk.com/dev/permissions ♪


Log in to reply
 

Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2