public async Task GetMediaListAsync_Returns_MediaList()
        {
            // Arrange
            var credentials         = MockInstagramCredentials();
            var options             = Options.Create(credentials);
            var logger              = Mock.Of <ILogger <InstagramApi> >();
            var mockFactory         = MockHttpClientFactory_For_GetMediaList();
            var instagramHttpClient = new InstagramHttpClient(options, mockFactory.Object, logger);
            var oAuthResponse       = new OAuthResponse
            {
                AccessToken = "123",
                User        = new UserInfo
                {
                    Id       = "123",
                    Username = "******"
                }
            };

            // Act
            var api      = new InstagramApi(options, logger, instagramHttpClient);
            var response = await api.GetMediaListAsync(oAuthResponse).ConfigureAwait(false);

            // Assert
            Assert.NotNull(api);
            Assert.NotNull(response);
            Assert.NotNull(response.Paging);
            Assert.NotNull(response.Data);
            Assert.Equal("Carousel", response.Data[0].Caption);
            Assert.Equal("CAROUSEL_ALBUM", response.Data[0].MediaType);
            Assert.Equal("18081199675165936", response.Data[0].Id);
            Assert.Equal("producer_journey", response.Data[0].Username);
            Assert.Equal(25, response.Data.Count); // unless you page through its 25 items per request
        }
        public async Task Authenticate_Handles_OAuthExceptions()
        {
            // Arrange
            var credentials         = MockInstagramCredentials();
            var options             = Options.Create(credentials);
            var logger              = Mock.Of <ILogger <InstagramApi> >();
            var mockFactory         = MockHttpClientFactory_For_Authenticate_Exception("OAuthException.json", HttpStatusCode.BadRequest);
            var instagramHttpClient = new InstagramHttpClient(options, mockFactory.Object, logger);

            // Act
            var api = new InstagramApi(options, logger, instagramHttpClient);
            var ex  = await Assert.ThrowsAsync <InstagramOAuthException>(() => api.AuthenticateAsync("", "")).ConfigureAwait(false);

            const string expectedMessage = "Error validating access token: Session has expired on Friday, 13-Mar-20 22:00:00 PDT. The current time is Saturday, 14-Mar-20 04:25:10 PDT.";
            var          actualMessage   = ex.Message;

            const string expectedType = "OAuthException";
            var          actualType   = ex.ErrorType;

            const int expectedCode = 190;
            var       actualCode   = ex.ErrorCode;

            const string expectedTraceId = "AzyAsv5wakY_WKcdKis3N32";
            var          actualTraceId   = ex.FbTraceId;

            // Assert
            Assert.NotNull(api);
            Assert.NotNull(ex);
            Assert.Equal(expectedMessage, actualMessage);
            Assert.Equal(expectedType, actualType);
            Assert.Equal(expectedCode, actualCode);
            Assert.Equal(expectedTraceId, actualTraceId);
        }
        public void Authorize_Returns_Expected_Uri()
        {
            // Arrange
            var credentials         = MockInstagramCredentials();
            var options             = Options.Create(credentials);
            var logger              = Mock.Of <ILogger <InstagramApi> >();
            var mockFactory         = MockHttpClientFactory();
            var instagramHttpClient = new InstagramHttpClient(options, mockFactory.Object, logger);

            // Act
            const string state = "";
            var          api   = new InstagramApi(options, logger, instagramHttpClient);
            var          url   = api.Authorize(state);

            // Assert
            Assert.NotNull(api);
            Assert.NotNull(url);

            var uri = new Uri(url);

            Assert.NotNull(uri);

            Assert.Equal("api.instagram.com", uri.Authority);
            Assert.Equal("https", uri.Scheme);
            Assert.Equal("api.instagram.com", uri.Host);
            Assert.Equal($"?client_id={credentials.ClientId}&redirect_uri={credentials.RedirectUrl}&scope=user_profile,user_media&response_type=code&state={state}", uri.Query);
            Assert.Equal($"/oauth/authorize?client_id={credentials.ClientId}&redirect_uri={credentials.RedirectUrl}&scope=user_profile,user_media&response_type=code&state={state}", uri.PathAndQuery);
        }
        public void Can_Create_InstanceOf_InstagramApi()
        {
            // Arrange
            var credentials         = MockInstagramCredentials();
            var options             = Options.Create(credentials);
            var logger              = Mock.Of <ILogger <InstagramApi> >();
            var mockFactory         = MockHttpClientFactory();
            var instagramHttpClient = new InstagramHttpClient(options, mockFactory.Object, logger);

            // Act
            var api = new InstagramApi(options, logger, instagramHttpClient);

            // Assert
            Assert.NotNull(api);
        }
Beispiel #5
0
        public override void AwakeFromNib()
        {
            base.AwakeFromNib();
            var scopes = new[]
            {
                "https://www.googleapis.com/auth/userinfo.email",
                "https://www.googleapis.com/auth/userinfo.profile"
            };

            api = new InstagramApi("instagram",
                                   ClientId,
                                   ClientSecret)
            {
                Scopes = scopes,
            };
        }
        public async Task <bool> Reload()
        {
            var result = await InstagramApi.GetProfile(username, true);

            if (!result.IsSuccess)
            {
                return(false);
            }
            var profile = result.GetResult();

            is_private  = profile.is_private;
            is_verified = profile.is_verified;
            full_name   = profile.full_name;
            id          = profile.id;
            followers   = profile.followers;
            following   = profile.following;
            posts       = profile.posts;
            return(true);
        }
        public async Task Authenticate_Returns_OAuthResponse()
        {
            // Arrange
            var credentials         = MockInstagramCredentials();
            var options             = Options.Create(credentials);
            var logger              = Mock.Of <ILogger <InstagramApi> >();
            var mockFactory         = MockHttpClientFactory_For_Authenticate();
            var instagramHttpClient = new InstagramHttpClient(options, mockFactory.Object, logger);

            // Act
            var api      = new InstagramApi(options, logger, instagramHttpClient);
            var response = await api.AuthenticateAsync("", "").ConfigureAwait(false);

            // Assert
            Assert.NotNull(api);
            Assert.NotNull(response);
            Assert.Equal("123", response.AccessToken);
            Assert.Equal("123", response.User.Id);
            Assert.Equal("BUSINESS", response.User.AccountType);
            Assert.Equal(116, response.User.MediaCount);
            Assert.Equal("solrevdev", response.User.Username);
        }
        public void Authorize_Without_ClientId_In_InstagramSettings_ThrowsException()
        {
            // Arrange
            var credentials = MockInstagramCredentials();

            credentials.ClientId = null;

            var options             = Options.Create(credentials);
            var logger              = Mock.Of <ILogger <InstagramApi> >();
            var mockFactory         = MockHttpClientFactory();
            var instagramHttpClient = new InstagramHttpClient(options, mockFactory.Object, logger);

            // Act
            var api = new InstagramApi(options, logger, instagramHttpClient);
            var ex  = Assert.Throws <ArgumentNullException>(() => api.Authorize(""));

            // Assert
            const string expected = "The ClientId is either null or empty please check the InstagramCredentials section in your appsettings.json (Parameter 'ClientId')";
            var          actual   = ex.Message;

            Assert.NotNull(api);
            Assert.Equal(expected, actual);
        }
        public async Task Authenticate_Handles_IGExceptions()
        {
            // Arrange
            var credentials         = MockInstagramCredentials();
            var options             = Options.Create(credentials);
            var logger              = Mock.Of <ILogger <InstagramApi> >();
            var mockFactory         = MockHttpClientFactory_For_Authenticate_Exception("IGApiException.json", HttpStatusCode.BadRequest);
            var instagramHttpClient = new InstagramHttpClient(options, mockFactory.Object, logger);

            // Act
            var api = new InstagramApi(options, logger, instagramHttpClient);
            var ex  = await Assert.ThrowsAsync <InstagramApiException>(() => api.AuthenticateAsync("", "")).ConfigureAwait(false);

            const string expectedMessage = "Unsupported get request. Object with ID '3518610791' does not exist, cannot be loaded due to missing permissions, or does not support this operation";
            var          actualMessage   = ex.Message;

            const string expectedType = "IGApiException";
            var          actualType   = ex.ErrorType;

            const int expectedCode = 100;
            var       actualCode   = ex.ErrorCode;

            const int expectedSubCode = 33;
            var       actualSubCode   = ex.ErrorSubcode;

            const string expectedTraceId = "AZtb-9k2P_mHdfRi-sN4MNH";
            var          actualTraceId   = ex.FbTraceId;

            // Assert
            Assert.NotNull(api);
            Assert.NotNull(ex);
            Assert.Equal(expectedMessage, actualMessage);
            Assert.Equal(expectedType, actualType);
            Assert.Equal(expectedCode, actualCode);
            Assert.Equal(expectedSubCode, actualSubCode);
            Assert.Equal(expectedTraceId, actualTraceId);
        }
 public IndexModel(InstagramApi api, ILogger <IndexModel> logger)
 {
     _api    = api;
     _logger = logger;
 }
Beispiel #11
0
 public InstagramModel(ILogger <InstagramModel> logger, InstagramApi api)
 {
     _logger = logger;
     _api    = api;
 }
Beispiel #12
0
        public void Initialize(Rendering rendering)
        {
            //Get current item
            var dataSource = rendering.DataSource;

            if (dataSource.IsNullOrEmpty())
            {
                dataSource = Sitecore.Configuration.Settings.GetSetting("FeedsDataSource", "{AF7BCA0A-4C7F-4ACA-9956-E4801143775A}");
            }

            CurrentItem = Sitecore.Context.Database.GetItem(dataSource);

            if (CurrentItem != null)
            {
                // Get all social feeds items
                var socialFeeds = CurrentItem.InnerItem.GetChildren().ToList();
                FacebookApi  = new FacebookApi();
                TwitterApi   = new TwitterApi();
                InstagramApi = new InstagramApi();
                YouTubeApi   = new YouTubeApi();
                PinterestApi = new PinterestApi();
                FlickrApi    = new FlickrApi();

                if (socialFeeds.Any())
                {
                    // Get facebook feed item and bind facebook api class properties
                    var facebookFeedItem = socialFeeds.FirstOrDefault(i => i.IsOfType(FacebookFeed.TemplateId));
                    if (facebookFeedItem != null)
                    {
                        var facebookFeed = new FacebookFeed(facebookFeedItem);
                        if (facebookFeed != null)
                        {
                            if (facebookFeed.FacebookAccount.TargetItem != null)
                            {
                                var facebookAccount = new FacebookAccount(facebookFeed.FacebookAccount.TargetItem);

                                if (facebookAccount.SocialLink != null)
                                {
                                    if (!ShowSocialFeed)
                                    {
                                        ShowSocialFeed = true;
                                    }
                                    FacebookApi.ApiId = string.Join("|",
                                                                    new string[]
                                    {
                                        facebookAccount.ApiId.Value, facebookAccount.ApiKey.Value
                                    });
                                    FacebookApi.Icon = facebookAccount.SocialLink != null
                                        ? MediaManager.GetMediaUrl(
                                        new SocialMedia(facebookAccount.SocialLink.TargetItem).SocialIcon.MediaItem)
                                        : "";

                                    // new BaseFeed()

                                    FacebookApi.Priority = GetPriority(facebookFeed.BaseFeed);
                                }
                            }
                        }
                    }

                    // Get twitter feed item and bind twitter api class properties
                    var twitterFeedItem = socialFeeds.FirstOrDefault(i => i.IsOfType(TwitterFeed.TemplateId));
                    if (twitterFeedItem != null)
                    {
                        var twitterFeed = new TwitterFeed(twitterFeedItem);
                        if (twitterFeed != null)
                        {
                            if (twitterFeed.TwitterAccount.TargetItem != null)
                            {
                                var twitterAccount = new TwitterAccount(twitterFeed.TwitterAccount.TargetItem);

                                if (!ShowSocialFeed)
                                {
                                    ShowSocialFeed = true;
                                }
                                TwitterApi.HashTagsWithTokens = string.Join("|", new string[]
                                {
                                    string.Join(",",
                                                twitterFeed.Hashtags.GetItems().Select(i => new Hashtag(i).Value.Value)),
                                    twitterAccount.TwitterToken.Value,
                                    twitterAccount.TwitterTokenSecret.Value,
                                    twitterAccount.TwitterConsumerKey.Value,
                                    twitterAccount.TwitterConsumerSecret.Value
                                });

                                TwitterApi.Icon = twitterAccount.SocialLink != null
                                    ? MediaManager.GetMediaUrl(
                                    new SocialMedia(twitterAccount.SocialLink.TargetItem).SocialIcon.MediaItem)
                                    : "";

                                TwitterApi.Priority = GetPriority(twitterFeed.BaseFeed);
                            }
                        }
                    }

                    // Get instagram feed item and bind instagram api class properties
                    var instagramFeedItem = socialFeeds.FirstOrDefault(i => i.IsOfType(InstagramFeed.TemplateId));

                    if (instagramFeedItem != null)
                    {
                        var instagramFeed = new InstagramFeed(instagramFeedItem);
                        if (instagramFeed != null)
                        {
                            if (instagramFeed.InstagramAccount.TargetItem != null)
                            {
                                var instagramAccount = new InstagramAccount(instagramFeed.InstagramAccount.TargetItem);

                                if (!ShowSocialFeed)
                                {
                                    ShowSocialFeed = true;
                                }
                                InstagramApi.HashTags = string.Join(",",
                                                                    instagramFeed.Hashtags.GetItems().Select(i => new Hashtag(i).Value.Value));
                                InstagramApi.AccessToken = instagramAccount.AccessToken.Value;
                                InstagramApi.ClientId    = instagramAccount.InstagramClientId.Value;
                                InstagramApi.Icon        = instagramAccount.SocialLink != null
                                    ? MediaManager.GetMediaUrl(
                                    new SocialMedia(instagramAccount.SocialLink.TargetItem).SocialIcon.MediaItem)
                                    : "";


                                InstagramApi.Priority = GetPriority(instagramFeed.BaseFeed);
                            }
                        }
                    }


                    // Get youtube feed item and bind youtube api class properties
                    var youTubeFeedItem = socialFeeds.FirstOrDefault(i => i.IsOfType(YoutubeFeed.TemplateId));
                    if (youTubeFeedItem != null)
                    {
                        var youTubeFeed = new YoutubeFeed(youTubeFeedItem);
                        if (youTubeFeed.YouTubeAccount.TargetItem != null)
                        {
                            var youtubeAccount = new YoutubeAccount(youTubeFeed.YouTubeAccount.TargetItem);

                            if (!ShowSocialFeed)
                            {
                                ShowSocialFeed = true;
                            }
                            YouTubeApi.AccountId     = youtubeAccount.AccountId.Value;
                            YouTubeApi.AccountApiKey = youtubeAccount.AccountApiKey.Value;

                            YouTubeApi.Icon = youtubeAccount.SocialLink != null
                                ? MediaManager.GetMediaUrl(
                                new SocialMedia(youtubeAccount.SocialLink.TargetItem).SocialIcon.MediaItem)
                                : "";

                            YouTubeApi.Priority = GetPriority(youTubeFeed.BaseFeed);
                        }
                    }

                    // Get pinterest feed item and bind pinterest api class properties
                    var pinterestFeedItem = socialFeeds.FirstOrDefault(i => i.IsOfType(PinterestFeed.TemplateId));
                    if (pinterestFeedItem != null)
                    {
                        var pinterestFeed = new PinterestFeed(pinterestFeedItem);
                        if (pinterestFeed != null && pinterestFeed.PinterestAccount.TargetItem != null)
                        {
                            var pinterestAccount = new PinterestAccount(pinterestFeed.PinterestAccount.TargetItem);


                            if (!ShowSocialFeed)
                            {
                                ShowSocialFeed = true;
                            }
                            PinterestApi.AccountId = pinterestAccount.AccountId.Value;

                            PinterestApi.Icon = pinterestAccount.SocialLink != null
                                ? MediaManager.GetMediaUrl(
                                new SocialMedia(pinterestAccount.SocialLink.TargetItem).SocialIcon.MediaItem)
                                : "";

                            PinterestApi.Priority = GetPriority(pinterestFeed.BaseFeed);
                        }
                    }

                    // Get flickr feed item and bind flickr api class properties
                    var flickrFeedItem = socialFeeds.FirstOrDefault(i => i.IsOfType(FlickrFeed.TemplateId));
                    if (flickrFeedItem != null)
                    {
                        var flickrFeed = new FlickrFeed(flickrFeedItem);
                        if (flickrFeed != null)
                        {
                            if (flickrFeed.FlickrAccount.TargetItem != null)
                            {
                                var flickrAccount = new FlickrAccount(flickrFeed.FlickrAccount.TargetItem);

                                if (!ShowSocialFeed)
                                {
                                    ShowSocialFeed = true;
                                }
                                FlickrApi.AccountId = flickrAccount.AccountId.Value;
                                FlickrApi.Icon      = flickrAccount.SocialLink != null
                                    ? MediaManager.GetMediaUrl(
                                    new SocialMedia(flickrAccount.SocialLink.TargetItem).SocialIcon.MediaItem)
                                    : "";

                                FlickrApi.Priority = GetPriority(flickrFeed.BaseFeed);
                            }
                        }
                    }
                }
            }
        }