125 lines
3.6 KiB
C#
125 lines
3.6 KiB
C#
8 years ago
|
|
||
|
|
||
|
using System;
|
||
|
using Newtonsoft.Json.Linq;
|
||
|
/// <summary>
|
||
|
/// Contains static methods that allow to extract user's information from a <see cref="JObject"/>
|
||
|
/// instance retrieved from Google after a successful authentication process.
|
||
|
/// </summary>
|
||
|
public static class GoogleHelper
|
||
|
{
|
||
|
/// <summary>
|
||
|
/// Gets the Google user ID.
|
||
|
/// </summary>
|
||
|
public static string GetId(JObject user)
|
||
|
{
|
||
|
if (user == null)
|
||
|
{
|
||
|
throw new ArgumentNullException(nameof(user));
|
||
|
}
|
||
|
|
||
|
return user.Value<string>("id");
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// Gets the user's name.
|
||
|
/// </summary>
|
||
|
public static string GetName(JObject user)
|
||
|
{
|
||
|
if (user == null)
|
||
|
{
|
||
|
throw new ArgumentNullException(nameof(user));
|
||
|
}
|
||
|
|
||
|
return user.Value<string>("displayName");
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// Gets the user's given name.
|
||
|
/// </summary>
|
||
|
public static string GetGivenName(JObject user)
|
||
|
{
|
||
|
if (user == null)
|
||
|
{
|
||
|
throw new ArgumentNullException(nameof(user));
|
||
|
}
|
||
|
|
||
|
return TryGetValue(user, "name", "givenName");
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// Gets the user's family name.
|
||
|
/// </summary>
|
||
|
public static string GetFamilyName(JObject user)
|
||
|
{
|
||
|
if (user == null)
|
||
|
{
|
||
|
throw new ArgumentNullException(nameof(user));
|
||
|
}
|
||
|
|
||
|
return TryGetValue(user, "name", "familyName");
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// Gets the user's profile link.
|
||
|
/// </summary>
|
||
|
public static string GetProfile(JObject user)
|
||
|
{
|
||
|
if (user == null)
|
||
|
{
|
||
|
throw new ArgumentNullException(nameof(user));
|
||
|
}
|
||
|
|
||
|
return user.Value<string>("url");
|
||
|
}
|
||
|
|
||
|
/// <summary>
|
||
|
/// Gets the user's email.
|
||
|
/// </summary>
|
||
|
public static string GetEmail(JObject user)
|
||
|
{
|
||
|
if (user == null)
|
||
|
{
|
||
|
throw new ArgumentNullException(nameof(user));
|
||
|
}
|
||
|
|
||
|
return TryGetFirstValue(user, "emails", "value");
|
||
|
}
|
||
|
|
||
|
// Get the given subProperty from a property.
|
||
|
private static string TryGetValue(JObject user, string propertyName, string subProperty)
|
||
|
{
|
||
|
JToken value;
|
||
|
if (user.TryGetValue(propertyName, out value))
|
||
|
{
|
||
|
var subObject = JObject.Parse(value.ToString());
|
||
|
if (subObject != null && subObject.TryGetValue(subProperty, out value))
|
||
|
{
|
||
|
return value.ToString();
|
||
|
}
|
||
|
}
|
||
|
return null;
|
||
|
}
|
||
|
|
||
|
// Get the given subProperty from a list property.
|
||
|
private static string TryGetFirstValue(JObject user, string propertyName, string subProperty)
|
||
|
{
|
||
|
JToken value;
|
||
|
if (user.TryGetValue(propertyName, out value))
|
||
|
{
|
||
|
var array = JArray.Parse(value.ToString());
|
||
|
if (array != null && array.Count > 0)
|
||
|
{
|
||
|
var subObject = JObject.Parse(array.First.ToString());
|
||
|
if (subObject != null)
|
||
|
{
|
||
|
if (subObject.TryGetValue(subProperty, out value))
|
||
|
{
|
||
|
return value.ToString();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return null;
|
||
|
}
|
||
|
}
|