private async Task GetGooglePlusUserData(string accessToken)
        {
            try
            {
                HttpClient client     = new HttpClient();
                var        urlProfile = "https://www.googleapis.com/oauth2/v1/userinfo?access_token=" + accessToken;

                client.CancelPendingRequests();
                HttpResponseMessage output = await client.GetAsync(urlProfile);

                if (output.IsSuccessStatusCode)
                {
                    string outputData = await output.Content.ReadAsStringAsync();

                    GoogleUserData newUserData = JsonConvert.DeserializeObject <GoogleUserData>(outputData);

                    if (newUserData != null)
                    {
                        string file      = Utils.GetDataStoreFolder() + newUserData.id;
                        string jsonFile  = file + ".json";
                        string imageFile = file + ".png";

                        if (File.Exists(jsonFile))
                        {
                            string         json             = File.ReadAllText(jsonFile);
                            GoogleUserData existingUserData = JsonConvert.DeserializeObject <GoogleUserData>(json);
                            if (newUserData.picture != null &&
                                (!File.Exists(imageFile) ||
                                 existingUserData.picture != null && existingUserData.picture != newUserData.picture))
                            {
                                Utils.GetImage(newUserData.picture, imageFile);
                            }
                        }
                        else
                        {
                            Utils.GetImage(newUserData.picture, imageFile);
                        }

                        // overwrites existing file
                        File.WriteAllText(jsonFile, outputData);

                        _userData = newUserData;
                    }
                }
            }
            catch (Exception)
            {
                // return some rubbish for now.
                _userData = new GoogleUserData()
                {
                    id = "1234567890", name = "Unknown"
                };
            }
        }
示例#2
0
        public Account CreateAccount(GoogleUserData googleData)
        {
            var newAccountId = IdGenerationHelper.GetNewId((accountId) => dataAccess.GetAccountById(accountId) == null);

            var account = new Account
            {
                AccountId   = newAccountId,
                GoogleId    = googleData.Id,
                Name        = googleData.Name,
                Email       = googleData.Email,
                CreatedDate = DateTime.UtcNow
            };

            dataAccess.AddNewAccount(account);
            Log.Information($"Added new account. id={account.AccountId}");
            return(account);
        }