public async Task<IHttpActionResult> RegisterExternal(RegisterExternalBindingModel model)
        {

            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            var verifiedAccessToken = await VerifyExternalAccessToken(model.Provider, model.ExternalAccessToken);
            if (verifiedAccessToken == null)
            {
                return BadRequest("Invalid Provider or External Access Token");
            }

            IdentityUser user = await _repo.FindAsync(new UserLoginInfo(model.Provider, verifiedAccessToken.user_id));

            bool hasRegistered = user != null;

            if (hasRegistered)
            {
                return BadRequest("External user is already registered");
            }

            user = new IdentityUser() { UserName = model.UserName };

            IdentityResult result = await _repo.CreateAsync(user);
            if (!result.Succeeded)
            {
                return GetErrorResult(result);
            }

            var info = new ExternalLoginInfo()
            {
                DefaultUserName = model.UserName,
                Login = new UserLoginInfo(model.Provider, verifiedAccessToken.user_id)
            };

            result = await _repo.AddLoginAsync(user.Id, info.Login);
            if (!result.Succeeded)
            {
                return GetErrorResult(result);
            }

            //generate access token response
            var accessTokenResponse = GenerateLocalAccessTokenResponse(model.UserName);

            return Ok(accessTokenResponse);
        }
        public async Task<IHttpActionResult> GoogleLoad(RegisterExternalBindingModel model)
        {
            string resultJson = string.Empty;
            string contactsTokenEndPoint = string.Format(@"https://www.google.com/m8/feeds/contacts/default/full?alt=json&access_token={0}&max-results=500&v=3.0", model.ExternalAccessToken);

            List<GMailContactModel> gmailContacts = new List<GMailContactModel>();

            try
            {
                var client = new HttpClient();
                var request = new HttpRequestMessage()
                {
                    RequestUri = new Uri(contactsTokenEndPoint),
                    Method = HttpMethod.Get
                };

                request.Headers.Add("Origin", "http://localhost:32150");
                request.Headers.Accept.Clear();
                request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("*/*"));
                request.Headers.Referrer = new Uri("http://localhost:32150/TestContacts.html");
                request.Headers.Add("user-agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.109 Safari/537.36");
                request.Headers.Add("x-client-data", "CKO2yQEIxLbJAQj9lcoB");
                request.Headers.Add("cache-control", "no-cache");
                request.Headers.Add("accept-language", "en-US,en;q=0.8");
                request.Headers.Add("accept-encoding", "gzip, deflate, sdch");

                var task = client.SendAsync(request)
                   .ContinueWith((taskwithmsg) =>
                    {
                        var response = taskwithmsg.Result;
 
                        if (response.Content.Headers.ContentEncoding.ToString().ToLower().Contains("gzip"))
                        {
                            var responseStream = new GZipStream(response.Content.ReadAsStreamAsync().Result, CompressionMode.Decompress);

                            StreamReader reader = new StreamReader(responseStream, Encoding.UTF8);

                            resultJson = reader.ReadToEnd();
                        }
                        else
                        {
                            resultJson = HandleChankedContent(response);
                        }
                    });
                task.Wait();

                var entries = Newtonsoft.Json.JsonConvert.DeserializeObject<JObject>(resultJson)["feed"]["entry"] as JArray;
                
                foreach (var entry in entries)
                {
                    Debug.WriteLine(" next entry processing " + entries.IndexOf(entry));

                    if (entry["gd$email"] == null)
                    {
                        continue;
                    }

                    var emailNode = entry["gd$email"].FirstOrDefault(ml => ml["primary"] != null && ml["primary"].Value<string>() == "true");
                    if (emailNode == null && entry["gd$email"].Any())
                    {
                        emailNode = entry["gd$email"].First();
                    }

                    if (emailNode == null || string.IsNullOrEmpty(emailNode["address"].Value<string>()))
                    {
                        continue;    
                    }
                    
                    string email = emailNode["address"].Value<string>();
                    
                    if (string.IsNullOrEmpty(email))
                    {
                        continue;
                    }

                    string name = entry["title"].HasValues ? entry["title"]["$t"].Value<string>() : "";
                    if (string.IsNullOrEmpty(name))
                    {
                        name = email;
                    }
                    string avaurl = entry["link"].HasValues ? entry["link"][0]["href"].Value<string>() : "";
                    if (!string.IsNullOrEmpty(avaurl))
                    {
                        avaurl += "&access_token=" + model.ExternalAccessToken;
                    }

                    gmailContacts.Add(new GMailContactModel()
                    {
                        email = email,
                        name = name,
                        avaurl = avaurl
                    });
                }
            }
            catch (Exception ex)
            {
                return InternalServerError(ex);
            }

            return Ok(gmailContacts);
        }