我正在尝试从Instagram获取图片到我的Windows窗体应用程序。当我获取我的个人资料的数据时,我无法访问图片所在的链接。
这是我目前的代码:
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;
}
这是json的用户数据:
{
"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
}
}
我需要"link": "https://www.instagram.com/p/BeprePeHCHK/"
。
答案 0 :(得分:0)
好的,所以我以不同的方式做到了,但效果很好:
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");
这就是如何将最后10张照片从Instagram保存到我的电脑上。
答案 1 :(得分:-1)
获取项目的值,不要将项目本身转换为字符串
string link = item.Value
答案 2 :(得分:-1)
我需要得到&#34;链接&#34;:&#34; https://www.instagram.com/p/BeprePeHCHK/&#34;
我已经编辑了我的答案,现在它将返回您要查找的网址,但是此网址不包含有效图片,因此无法在PictureBox上正确显示,您必须选择其他网址包含有效的图像。
这是正确的版本。
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;
}