using System.Collections.Generic; using System.Linq; using System.Net.Http; using System.Threading.Tasks; using Flurl; namespace Sony.Filtr.YouTube.Api { public class GoogleOAuth2Api { //private const string ClientID = "496029115583-2gm32qtrt13t9om4ufuh23rlmvo4fj5b.apps.googleusercontent.com"; //private const string ClientSecret = "yIu9_L4xi11aWqT5H1uuE7jb"; //private const string ClientID = "1066394547140-bpkvl6vd6detutas90bvm98d7sgbusqa.apps.googleusercontent.com"; //private const string ClientSecret = "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"; private readonly string clientId; private readonly string secret; public GoogleOAuth2Api(string clientId, string secret) { this.clientId = clientId; this.secret = secret; } public async Task RequestRefreshedAccessTokenAsync(string refreshToken) { var url = new Url("https://accounts.google.com/o/oauth2/token"); Dictionary requestParams = new Dictionary() { { "client_id", this.clientId }, { "client_secret", this.secret }, { "refresh_token", refreshToken }, { "grant_type", "refresh_token" }, }; var client = new HttpClient(); var response = await client.PostAsync(url, new FormUrlEncodedContent(requestParams)); var accessTokenResponse = await response.Content.ReadAsAsync(); return accessTokenResponse; } public async Task GetMe(AccessTokenResponse accessToken) { var client = new HttpClient(); var url = new Url("https://www.googleapis.com/oauth2/v1/tokeninfo"); url.SetQueryParam("access_token", accessToken.access_token); var response = await client.GetAsync(url); var accessTokenInfo = await response.Content.ReadAsAsync(); return accessTokenInfo; } public async Task GetUserInfo(AccessTokenResponse accessToken) { var client = new HttpClient(); var url = new Url("https://www.googleapis.com/oauth2/v1/userinfo"); url.SetQueryParam("access_token", accessToken.access_token); var response = await client.GetAsync(url); var userInfo = await response.Content.ReadAsAsync(); return userInfo; } public class AccessTokenResponse { public string access_token { get; set; } public string token_type { get; set; } public string expires_in { get; set; } public string refresh_token { get; set; } } public class AccessTokenInfoResponse { public string audience { get; set; } public string user_id { get; set; } public string scope { get; set; } public string expires_in { get; set; } } public class UserInfoResponse { public string id { get; set; } public string name { get; set; } public string given_name { get; set; } public string family_name { get; set; } public string link { get; set; } public string picture { get; set; } public string gender { get; set; } public string locale { get; set; } } } }