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?
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;
}
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.
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.
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.
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.
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.
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
Ok some progress with this, may be a better way to do it but i have the following working
Regards
Ismail
Yes, you can search for users matching a specific username. Should look something like:
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.
Anders,
I need get user stats like followers and the recent media so i guess that would involve 2 calls?
Cheers
Ismail
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
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.
Anders,
No problem i have reverted to old code which is working.
Regards
Ismail
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.
Anders,
Sure I was using ScottHermanFitness and I see your point about more than one hmm.
Cheers
Ismail
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.Anders,
Lowercase done the trick, i think i owe you big time lol!
is working on a reply...