From 070d92cba2296b764faa73d25d34a2356ed43e3d Mon Sep 17 00:00:00 2001 From: lorenzo gremoli Date: Fri, 10 Dec 2021 14:28:28 +0100 Subject: [PATCH] Modified ChildData Changed api ULR for getting media Added method for getting childrend media --- src/Core/Instagram/ChildData.cs | 4 +- src/Core/InstagramApi.cs | 82 ++++++++++++++++++++++++++++++--- 2 files changed, 78 insertions(+), 8 deletions(-) diff --git a/src/Core/Instagram/ChildData.cs b/src/Core/Instagram/ChildData.cs index 3030c15..4b6da52 100644 --- a/src/Core/Instagram/ChildData.cs +++ b/src/Core/Instagram/ChildData.cs @@ -10,8 +10,8 @@ public class ChildData /// /// The Media's caption text. Not returnable for Media in albums. /// - [JsonPropertyName("caption")] - public string Caption { get; set; } + //[JsonPropertyName("caption")] + //public string Caption { get; set; } /// /// The Media's ID. diff --git a/src/Core/InstagramApi.cs b/src/Core/InstagramApi.cs index 1dcd1ba..3c085be 100644 --- a/src/Core/InstagramApi.cs +++ b/src/Core/InstagramApi.cs @@ -332,8 +332,8 @@ public async Task GetMediaListAsync(string url) /// /// A valid /// A valid Media Id - /// A Media object or null if an error is caught - public async Task GetMediaAsync(OAuthResponse response, string mediaId) + /// A Data object or null if an error is caught + public async Task GetMediaAsync(OAuthResponse response, string mediaId) { AssertInstagramSettings(); @@ -370,8 +370,8 @@ public async Task GetMediaAsync(OAuthResponse response, string mediaId) /// /// An Instagram User Access Token /// A valid Media Id - /// A Media object or null if an error is caught - public async Task GetMediaAsync(string accessToken, string mediaId) + /// A Data object or null if an error is caught + public async Task GetMediaAsync(string accessToken, string mediaId) { AssertInstagramSettings(); @@ -387,8 +387,78 @@ public async Task GetMediaAsync(string accessToken, string mediaId) try { - var url = $"https://graph.instagram.com/{mediaId}/media?fields=caption,id,media_type,media_url,permalink,thumbnail_url,username,timestamp,children{{id,media_type,media_url,permalink,thumbnail_url,username,timestamp}}&access_token={accessToken}"; - return await _client.GetJsonAsync(url).ConfigureAwait(false); + var url = $"https://graph.instagram.com/{mediaId}/?fields=caption,id,media_type,media_url,permalink,thumbnail_url,username,timestamp,children{{id,media_type,media_url,permalink,thumbnail_url,username,timestamp}}&access_token={accessToken}"; + return await _client.GetJsonAsync(url).ConfigureAwait(false); + } + catch (Exception ex) + { + _logger.LogError(ex, "Cannot [{me}] with the accessToken [{token}] and media id [{user}]", nameof(GetMediaAsync), accessToken, mediaId); + throw; + } + } + + /// + /// Gets a object via it's unique identifier + /// + /// A valid + /// A valid ChildMedia Id + /// A ChildData object or null if an error is caught + public async Task GetChildMediaAsync(OAuthResponse response, string mediaId) + { + AssertInstagramSettings(); + + if (response == null) + { + throw new ArgumentNullException(nameof(response)); + } + + if (string.IsNullOrWhiteSpace(response.AccessToken)) + { + throw new ArgumentNullException(nameof(response.AccessToken)); + } + + if (response.User == null) + { + throw new ArgumentNullException(nameof(response.User)); + } + + if (string.IsNullOrWhiteSpace(response.User.Id)) + { + throw new ArgumentNullException(nameof(response.User.Id)); + } + + if (string.IsNullOrWhiteSpace(mediaId)) + { + throw new ArgumentNullException(nameof(mediaId)); + } + + return await GetChildMediaAsync(response.AccessToken, mediaId).ConfigureAwait(false); + } + + /// + /// Gets a object via it's unique identifier + /// + /// An Instagram User Access Token + /// A valid ChildMedia Id + /// A ChildData object or null if an error is caught + public async Task GetChildMediaAsync(string accessToken, string mediaId) + { + AssertInstagramSettings(); + + if (string.IsNullOrWhiteSpace(accessToken)) + { + throw new ArgumentNullException(nameof(accessToken)); + } + + if (string.IsNullOrWhiteSpace(mediaId)) + { + throw new ArgumentNullException(nameof(mediaId)); + } + + try + { + var url = $"https://graph.instagram.com/{mediaId}/?fields=id,media_type,media_url,permalink,thumbnail_url,username,timestamp&access_token={accessToken}"; + return await _client.GetJsonAsync(url).ConfigureAwait(false); } catch (Exception ex) {