Your question is:
How do I attribute authors if I don't know their real names?
I think this has wider applicability to just this case, so I'll attempt to answer with future visitors in mind. I'm also intentionally avoiding the issue of licensing but just focusing on author attribution.
The first thing I would do in this case - although not necessarily required - is contact the author if possible, and ask how they would like to be attributed. Some may be happy to give you their full name, others may prefer their nickname, or others may even be happy to say 'do whatever you like!'.
(When I post code snippets I've written, I'm always happy for it to be used in any way, but if people want to attribute me then that's awesome. I know many other SE users (not everybody though!) apply a similar attitude to their posts)
If you can't contact the authors or don't hear back from them, then - assuming you already have the implicit permission to use the code - I would attribute them based on their nickname, and include their profile URL.
This is still a suitable default method of attribution, as it unambiguously identifies the person (maybe even more so than their full name on its own, actually!). In the case of StackExchange, including the user URL is part of the attribution guidelines (point 4) when including SE content on another website, and in any case, it allows a future user of the code to see who was behind it, easily locate the context of it, and possibly even contact the user. This would be suitable for both your StackOverflow and GitHub examples.
Of course, if a user then contacted you later and asked you to change the way you're attributing them, then you should follow that.