private void Start() { if (PlayerPrefs.HasKey(PlayerPrefsConstants.EmailPref) && !string.IsNullOrWhiteSpace(PlayerPrefs.GetString(PlayerPrefsConstants.EmailPref))) { SetUserLoggedInUI(); } else { SetUserLoggedOutUI(); } _googleAuthorization = new GoogleAuthorization(_webClientId); _authApiService = new AuthApiService(); _signInButton.onClick.AddListener(OnSignIn); _signOutButton.onClick.AddListener(OnSignOut); _settingsButton.onClick.AddListener(OnSettings); _resetButton.onClick.AddListener(OnReset); _okButton.onClick.AddListener(OnOk); }
} // End of the GetFacebookUser method /// <summary> /// Get a google user /// </summary> public async static Task<GoogleUser> GetGoogleUser(Domain domain, string code) { // Create variables HttpRequestMessage request = null; HttpResponseMessage response = null; GoogleAuthorization google_authorization = null; GoogleUser google_user = null; // Get a static http client HttpClient client = DefaultHttpClient.Get(); // Create a dictionary with data Dictionary<string, string> input = new Dictionary<string, string>(10); input.Add("code", code); input.Add("client_id", domain.google_app_id); input.Add("client_secret", domain.google_app_secret); input.Add("redirect_uri", domain.web_address + "/customer/google_login_callback"); input.Add("grant_type", "authorization_code"); // Use form data content using (FormUrlEncodedContent content = new FormUrlEncodedContent(input)) { // Create a request message request = new HttpRequestMessage(HttpMethod.Post, "https://accounts.google.com/o/oauth2/token"); request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); request.Headers.AcceptEncoding.Add(new StringWithQualityHeaderValue("Gzip")); request.Headers.AcceptEncoding.Add(new StringWithQualityHeaderValue("Deflate")); request.Content = content; // Get a response response = await client.SendAsync(request); // Make sure that the response is successful if (response.IsSuccessStatusCode == true) { // Get google authorization google_authorization = JsonConvert.DeserializeObject<GoogleAuthorization>(await response.Content.ReadAsStringAsync()); } else { // Get error information string data = await response.Content.ReadAsStringAsync(); } } // Make sure that google authorization not is null if(google_authorization == null) { return null; } // Create a request message request = new HttpRequestMessage(HttpMethod.Get, "https://www.googleapis.com/plus/v1/people/me?key=" + domain.google_app_id); request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", google_authorization.access_token); request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); request.Headers.AcceptEncoding.Add(new StringWithQualityHeaderValue("Gzip")); request.Headers.AcceptEncoding.Add(new StringWithQualityHeaderValue("Deflate")); // Get a response response = await client.SendAsync(request); // Make sure that the response is successful if (response.IsSuccessStatusCode == true) { // Get a google user google_user = JsonConvert.DeserializeObject<GoogleUser>(await response.Content.ReadAsStringAsync()); } else { // Get error information string data = await response.Content.ReadAsStringAsync(); } // Return a google user return google_user; } // End of the GetGoogleUser method