Unable to use ISteamUser().GetFriendList()

Topics: Bugs
Jan 4, 2014 at 2:19 AM
I'm trying to get the friend list of a user, but I'm not quite sure how to do it. Here's the code I'm using to view Robin Walker's profile, from the Steam Web API documentation ( https://developer.valvesoftware.com/wiki/Steam_Web_API#GetFriendList_.28v0001.29 ):
var steamIdentity = SteamIdentity.FromSteamID(76561197960435530);
var resolver = SteamWebAPI.General().ISteamUser().GetFriendList(steamIdentity, RelationshipType.Friend);
var friendListResponse = resolver.GetResponse();
At this point, I should be able to get Robin's friend list, since the above call should be functionally equivalent to this call:
http://api.steampowered.com/ISteamUser/GetFriendList/v0001/?key=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX&steamid=76561197960435530&relationship=friend
However, friendListResponse.Data is null, which makes accessing friendListResponse.Data.Friends impossible.

In addition, GetFriendList().GetResponse() doesn't appear to work in other use cases. I'm guessing if you try to access a non-public profile... when I tried using GetResponse() in conjunction with a different SteamID, I get a System.AggregateException ("Response status code does not indicate success: 500 (Internal Server Error)"). If you need that specific SteamID to test with, please let me know.
Jan 4, 2014 at 4:35 AM
Edited Jan 4, 2014 at 6:37 AM
So after some digging, I found that the reason with friendListResponse.Data returning null was because of a bug with its response object which caused GetResponse() to not de-serialize it correctly.

As for the System.AggregateException exception, I have added a change to the GetResponse() method. It will now catch Internal Server Errors (500) without throwing an exception. Details of the error and error code are now in the response object and the Success property will be set to false.

The 1.0.9 update should fix your problems. Thanks for the find!
Marked as answer by helloworld65 on 1/3/2014 at 8:56 PM
Jan 4, 2014 at 6:31 AM
I'm now able to pull the list of friends info, exactly as you said -- thank you for fixing that so quickly!

However, I seem to be having problems when I try to serialize the list of friends to a JSON string. First, I had to tell JSON.net to ignore any errors it encountered when trying to serialize data returned by successful GetResponse() calls, due to various NotImplementedException errors I still see being thrown in the source code. Here's how I did that:
private static void SaveAsJsonFiles<T>(IEnumerable<T> objects, Func<T, string> getFileName)
{
    // Ignore NotImplementedExceptions thrown by successful GetResponse() calls.
    var settings = new JsonSerializerSettings();
    settings.Error += (o, args) =>
    {
        if (args.ErrorContext.Error is NotImplementedException)
            args.ErrorContext.Handled = true;
        else if (args.ErrorContext.Error.InnerException is NotImplementedException)
            args.ErrorContext.Handled = true;
    };

    var saveFolder = new DirectoryInfo(GlobalVariables.SaveFolderPath);
    saveFolder.Create();

    foreach (var obj in objects)
    {
        string json = JsonConvert.SerializeObject(obj, Formatting.Indented, settings);
        string newFilePath = Path.Combine(GlobalVariables.SaveFolderPath, String.Concat(getFileName(obj), ".json"));
        var newFile = new FileInfo(newFilePath);

        File.WriteAllText(newFile.FullName, json);
    }
}
Once I'm able to serialize objects without throwing exceptions, the List<PortableSteam.Interfaces.General.ISteamUser.Friend> data that I collected from each successful GetFriendList() request comes back looking like this:
"Friends": [
  {
    "steamid": null,
    "relationship": null,
    "friend_since": null
  },
  {
    "steamid": null,
    "relationship": null,
    "friend_since": null
  },
  {
    "steamid": null,
    "relationship": null,
    "friend_since": null
  },
  // ... etc ...
],
All of the values in each Friend object, which I confirmed actually had values prior to serialization, are now being rendered as null. Any idea what might be causing this behavior?
Jan 4, 2014 at 6:43 AM
Yes, all three fields in Friend use custom converters and I just haven't implemented the WriteJson portion of those converters for them yet. I will try to push out an update as soon as possible. In the mean time you could explore the GetResponseString method. The array of Friends is wrapped in friendslist token.
Jan 4, 2014 at 3:06 PM
Perfect -- tyvm!
Jan 4, 2014 at 6:25 PM
I just released an update patch on NuGet for re-serializing the object responses. WriteJson should be working now. Check it out.
Marked as answer by helloworld65 on 1/4/2014 at 10:25 AM
Jan 4, 2014 at 8:48 PM
Works like a charm -- thanks!
Apr 12, 2014 at 2:54 AM
Hey... sorry for the delayed response -- kid + rarely-used account + busy work.

Thank you so much for the reply and fix... I'll give it a shot the next time I have a bit of downtime!