Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated InstagramApi.cs #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Core/Instagram/ChildData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ public class ChildData
/// <summary>
/// The Media's caption text. Not returnable for Media in albums.
/// </summary>
[JsonPropertyName("caption")]
public string Caption { get; set; }
//[JsonPropertyName("caption")]
//public string Caption { get; set; }

/// <summary>
/// The Media's ID.
Expand Down
82 changes: 76 additions & 6 deletions src/Core/InstagramApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -332,8 +332,8 @@ public async Task<Media> GetMediaListAsync(string url)
/// </summary>
/// <param name="response">A valid <see cref="OAuthResponse"/></param>
/// <param name="mediaId">A valid Media Id</param>
/// <returns>A Media object or null if an error is caught</returns>
public async Task<Media> GetMediaAsync(OAuthResponse response, string mediaId)
/// <returns>A Data object or null if an error is caught</returns>
public async Task<Data> GetMediaAsync(OAuthResponse response, string mediaId)
{
AssertInstagramSettings();

Expand Down Expand Up @@ -370,8 +370,8 @@ public async Task<Media> GetMediaAsync(OAuthResponse response, string mediaId)
/// </summary>
/// <param name="accessToken">An Instagram User Access Token</param>
/// <param name="mediaId">A valid Media Id</param>
/// <returns>A Media object or null if an error is caught</returns>
public async Task<Media> GetMediaAsync(string accessToken, string mediaId)
/// <returns>A Data object or null if an error is caught</returns>
public async Task<Data> GetMediaAsync(string accessToken, string mediaId)
{
AssertInstagramSettings();

Expand All @@ -387,8 +387,78 @@ public async Task<Media> 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<Media>(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<Data>(url).ConfigureAwait(false);
}
catch (Exception ex)
{
_logger.LogError(ex, "Cannot [{me}] with the accessToken [{token}] and media id [{user}]", nameof(GetMediaAsync), accessToken, mediaId);
throw;
}
}

/// <summary>
/// Gets a <see also="Media" /> object via it's unique identifier
/// </summary>
/// <param name="response">A valid <see cref="OAuthResponse"/></param>
/// <param name="mediaId">A valid ChildMedia Id</param>
/// <returns>A ChildData object or null if an error is caught</returns>
public async Task<ChildData> 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);
}

/// <summary>
/// Gets a <see also="Media" /> object via it's unique identifier
/// </summary>
/// <param name="accessToken">An Instagram User Access Token</param>
/// <param name="mediaId">A valid ChildMedia Id</param>
/// <returns>A ChildData object or null if an error is caught</returns>
public async Task<ChildData> 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<ChildData>(url).ConfigureAwait(false);
}
catch (Exception ex)
{
Expand Down