0

I am trying to get pictures from Instagram to my Windows Forms Aplication. When I get data of my profile I cannot access link where picture is located.

This is my current code:

private void button1_Click(object sender, EventArgs e)
{
    var picFilePath = @"C:\PictureFromInsta.jpg";
    WebClient Client = new WebClient();
    Client.DownloadFile("https://api.instagram.com/v1/users/7030608823/media/recent/?access_token=7030608823.1677ed0.f5877671841d4751af1de0c307b55d04", @"C:\jsonLink.json");

    var json = File.ReadAllText(@"C:\jsonLink.json");
    var jsonConv = JsonConvert.DeserializeObject(json);
    JObject jsonArray = new JObject(jsonConv);
    foreach(var item in jsonArray)
    {
        if(item.Value.Contains("https://www.instagram.com"))
        {
            string link = Convert.ToString(item);
            Client.DownloadFile(link, picFilePath);

            WebBrowser webBrowser1 = new WebBrowser();
            webBrowser1.Navigate(link);
        }
    }
    PictureBox p = new PictureBox();
    p.ImageLocation = picFilePath;
}

And this is json with user data:

{
    "pagination": {},
    "data": [{
        "id": "1705085128132010442_7030608823",
        "user": {
            "id": "7030608823",
            "full_name": "Timotej Gregoric",
            "profile_picture": "https://instagram.famd3-1.fna.fbcdn.net/vp/3230896e49952035c4a21d078561d30f/5B1DB27A/t51.2885-19/11906329_960233084022564_1448528159_a.jpg",
            "username": "timi.g200"
        },
        "images": {
            "thumbnail": {
                "width": 150,
                "height": 150,
                "url": "https://scontent.cdninstagram.com/vp/7dd31adb2a9d022aa75eb8bdcf1d98da/5B197D11/t51.2885-15/s150x150/e35/27578551_408458386276378_4933350606149517312_n.jpg"
            },
            "low_resolution": {
                "width": 320,
                "height": 320,
                "url": "https://scontent.cdninstagram.com/vp/54fa9b153901d4887c5cf3ea0a1e1f11/5B152956/t51.2885-15/s320x320/e35/27578551_408458386276378_4933350606149517312_n.jpg"
            },
            "standard_resolution": {
                "width": 480,
                "height": 480,
                "url": "https://scontent.cdninstagram.com/vp/db9c668781db83a9366b05e91bd24ef0/5B265274/t51.2885-15/e35/27578551_408458386276378_4933350606149517312_n.jpg"
            }
        },
        "created_time": "1517482008",
        "caption": null,
        "user_has_liked": false,
        "likes": {
            "count": 0
        },
        "tags": [],
        "filter": "Normal",
        "comments": {
            "count": 0
        },
        "type": "image",
        "link": "https://www.instagram.com/p/BeprePeHCHK/",
        "location": null,
        "attribution": null,
        "users_in_photo": []
    }],
    "meta": {
        "code": 200
    }
}

I need to get "link": "https://www.instagram.com/p/BeprePeHCHK/".

gpgekko
  • 3,506
  • 3
  • 32
  • 35
Gandalf
  • 9
  • 5

3 Answers3

0

Ok so I did it in a bit differnet way but it works perfectly:

    private void Form1_Load(object sender, EventArgs e)
    {
        var nextPageUrl = "";

        WebRequest webRequest = null;
        if (webRequest == null && string.IsNullOrEmpty(nextPageUrl))
            webRequest = HttpWebRequest.Create(String.Format("https://api.instagram.com/v1/users/self/media/recent/?access_token=7030608823.1677ed0.f5877671841d4751af1de0c307b55d04"));
        else
            webRequest = HttpWebRequest.Create(nextPageUrl);

        var responseStream = webRequest.GetResponse().GetResponseStream();
        Encoding encode = System.Text.Encoding.Default;


        using (StreamReader reader = new StreamReader(responseStream, encode))
        {
            JToken token = JObject.Parse(reader.ReadToEnd());
            var pagination = token.SelectToken("pagination");

            if (pagination != null && pagination.SelectToken("next_url") != null)
            {
                nextPageUrl = pagination.SelectToken("next_url").ToString();
            }
            else
            {
                nextPageUrl = null;
            }

            var images = token.SelectToken("data").ToArray();
            int i = 0;
            foreach (var image in images)
            {
                if (i < 10)
                {
                    i++;
                    var imageUrl = image.SelectToken("images").SelectToken("standard_resolution").SelectToken("url").ToString();

                    WebClient client = new WebClient();
                    Directory.CreateDirectory(@"C:\instaPics\");
                    client.DownloadFile(imageUrl, @"C:\instaPics\instaPic" + i + ".jpg");

That is how to save last 10 photos from instagram to my PC.

Gandalf
  • 9
  • 5
-1

Get the value of the item, do not convert the item itself to a string

string link = item.Value
jalsh
  • 801
  • 6
  • 18
  • Still not working: JObject jsonObj = new JObject(jsonConv); In this line I get: An exception has been encountered. This may be caused by an extension. – Gandalf Feb 02 '18 at 06:19
-1

I need to get "link": "https://www.instagram.com/p/BeprePeHCHK/"

I have edit my answer it will now return the URL you are looking for, however this URL is doesn't contain a valid image so it will not correctly display on the PictureBox, you will have to choose another URL that contains a valid Image.

here is correct version.

        private void button1_Click(object sender, EventArgs e)
    {
        var picFilePath = @"C:\PictureFromInsta.jpg";
        WebClient Client = new WebClient();
        Client.DownloadFile("https://api.instagram.com/v1/users/7030608823/media/recent/?access_token=7030608823.1677ed0.f5877671841d4751af1de0c307b55d04", @"C:\jsonLink.json");

        string json = File.ReadAllText(@"C:\jsonLink.json");
        JObject obj = JObject.Parse(json);
        var valueArray = obj["data"][0]["link"].Value<string>();

        if (valueArray.ToString().Contains("https://www.instagram.com"))
        {
            string link = valueArray.ToString();
            Client.DownloadFile(link, picFilePath);

            WebBrowser webBrowser1 = new WebBrowser();
            webBrowser1.Navigate(link);
        }
        PictureBox p = new PictureBox();
        p.ImageLocation = picFilePath;
    }
besh
  • 14
  • 1
  • 2
  • Still not working: JObject jsonObj = new JObject(jsonConv); In this line I get: An exception has been encountered. This may be caused by an extension. – Gandalf Feb 02 '18 at 06:17
  • i have edited my answer it will now show correct URL – besh Feb 03 '18 at 15:55