Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Ismail Mayat 4511 posts 10090 karma points MVP 2x admin c-trib
    Feb 19, 2015 @ 16:45
    Ismail Mayat
    0

    Instagram username to id

    Anders,

    For the instragram service the get user requires a user id which is a long, I have usernames is there something in the api to get that id for a given user, I can see search _instagramService.Users.Search that takes query parameter and I am assuming i can just pass username in there? If so then I cannot see how from the return object i can get the actual data for the user?

    Cheers

    Ismail

  • Ismail Mayat 4511 posts 10090 karma points MVP 2x admin c-trib
    Feb 19, 2015 @ 17:37
    Ismail Mayat
    100

    Ok some progress with this, may be a better way to do it but i have the following working

            /// <summary>
            /// instagram only does stuff with ids not user names therefore we have to get ids from username
            /// </summary>
            /// <param name="talentSocial"></param>
            /// <returns></returns>
            private long GetInstagramIdFromUserName(string instagramUserName)
            {
                long userId=0;
                var searchResult = _instagramService.Users.Search(instagramUserName).Response.Body;
    
                var userData = JObject.Parse(searchResult);
    
                if (userData != null && searchResult.Contains("id"))
                {
                    userId = userData["data"][0]["id"].Value<long>();
                }
    
                return userId;
            }

    Regards

    Ismail

  • Anders Bjerner 487 posts 2989 karma points MVP 7x admin c-trib
    Feb 19, 2015 @ 22:07
    Anders Bjerner
    1

    Yes, you can search for users matching a specific username. Should look something like:

    InstagramUserSummary user = service.Users.Search(username).Body.Data.FirstOrDefault(x => x.Username == username);
    
    if (user != null) {
    
        InstagramRecentMediaResponse response = service.Users.GetRecentMedia(user.Id);
    
        foreach (InstagramMedia media in response.Body.Data) {
    
            Content.Text += "<img src=\"" + media.Thumbnail + "\" alt=\"\" />";
    
        }
    
    }
    

    In my experience the user search can be a little weird, since the results may have a different order depending on the count/limit specified. So you can set the count to a high value. I think the current maximum is 20 or 30, but the API won't throw an error if you specify something higher.

    InstagramUserSummary user = service.Users.Search(new InstagramUserSearchOptions {
        Query = username,
        Count = 50
    }).Body.Data.FirstOrDefault(x => x.Username == username);
    
  • Ismail Mayat 4511 posts 10090 karma points MVP 2x admin c-trib
    Feb 20, 2015 @ 09:34
    Ismail Mayat
    0

    Anders,

    I need get user stats like followers and the recent media so i guess that would involve 2 calls?

    var instagraUser = _instagramService.Users.Service.Users.GetInfo(userId).Body.Data;
    
                    if (instagraUser != null)
                    {
                        socialStat.InstragramFollowerCount = instagraUser.Counts.FollowedBy;
                        socialStat.InstragramPostCount = instagraUser.Counts.Media;
                    }
    
                    var recentMedia = _instagramService.Users.GetRecentMedia(userId, 5).Images;
    
                    if (recentMedia.Any())
                    {
                        socialStat.RecentInstagramImages = recentMedia;
                    }

    Cheers

     

    Ismail

  • Ismail Mayat 4511 posts 10090 karma points MVP 2x admin c-trib
    Feb 20, 2015 @ 10:07
    Ismail Mayat
    0

    Anders,

    I tried your code snippet i get nothing back, i have reverted to what i had before, i have also updated dll to your latest so will now try the youtube stuff.  Many thanks once again for all your help.

    Regards

     

    Ismail

  • Anders Bjerner 487 posts 2989 karma points MVP 7x admin c-trib
    Feb 20, 2015 @ 10:47
    Anders Bjerner
    1

    The snippet worked when searching for "abjerner" (and probably all usernames I have tested with as far as I remember), but as I wrote, the user search can be a little weird, so it might not work properly for all usernames (eg. if other users have higher relevance). So I don't think I'll be able to help you any further there.

    I haven't tested your code for the user stats, but it seems about right. It requires one call to get information about the user, and one call to get the recent media.

  • Ismail Mayat 4511 posts 10090 karma points MVP 2x admin c-trib
    Feb 20, 2015 @ 11:18
    Ismail Mayat
    0

    Anders,

    No problem i have reverted to old code which is working.

    Regards

    Ismail

  • Anders Bjerner 487 posts 2989 karma points MVP 7x admin c-trib
    Feb 20, 2015 @ 11:27
    Anders Bjerner
    1

    Are you sure that works as intended? From what I can tell, it will search for a given username, and then return the ID of the first user in the results - which may not be a user with the specified username. Eg. if searching for "abjerner", another user with the username "abjerner2" might be the first user in the results, thus giving the wrong ID.

    Would it be okay for you to post the username you're searching for here? Then I can do some tests to see why it wasn't working for you.

  • Ismail Mayat 4511 posts 10090 karma points MVP 2x admin c-trib
    Feb 20, 2015 @ 12:36
    Ismail Mayat
    0

    Anders,

    Sure I was using ScottHermanFitness and I see your point about more than one hmm.

    Cheers

    Ismail

  • Anders Bjerner 487 posts 2989 karma points MVP 7x admin c-trib
    Feb 20, 2015 @ 12:57
    Anders Bjerner
    1

    I think this might be a case-related issue since Instagram probably returns usernames in lowercase. So .FirstOrDefault(x => x.Username == username); should be updated to ignore case.

  • Ismail Mayat 4511 posts 10090 karma points MVP 2x admin c-trib
    Feb 20, 2015 @ 14:52
    Ismail Mayat
    0

    Anders,

    Lowercase done the trick, i think i owe you big time lol!

Please Sign in or register to post replies

Write your reply to:

Draft