Google Authentication API: How to get the user's gmail address

馋奶兔 提交于 2019-11-30 04:46:38

问题


I've been studying the Google authentication API (AuthSub)... My question is, how do I get the user's account information (at least their Gmail address) after the authentication has passed?

Because currently, all I get back from the authentication process is a token granting me access to which ever Google service I have specified in the scope, but there's no easy way to even get the user's login id (Gmail address) as far as I can tell...


If so, what Google service allows me to access the user's information?


回答1:


Using the Google AppEngine GData services, you can request the user to give you access to their Google Mail, Calendar, Picasa, etc. Check it out here.




回答2:


Google Authentication API is a token based system to authenticate a valid user. It does not expose any of other interface that allows to get account holder information back to authorizer.




回答3:


You can get some of the data through the OpenID API, with the ax extension. If you are authenticating with other methods, best I found is calling https://www-opensocial.googleusercontent.com/api/people/@me/@self and it will get you name, email and picture. Be sure to have http://www-opensocial.googleusercontent.com/api in scopes when authenticating.




回答4:


    [ValidateInput(false)]
    public ActionResult Authenticate(string returnUrl)
    {
        try
        {
            logger.Info("" + returnUrl + "] LoginController : Authenticate method start  ");
            var response = openid.GetResponse();
            if (response == null)
            {
                try
                {
                    string discoveryuri = "https://www.google.com/accounts/o8/id";
                    //OpenIdRelyingParty openid = new OpenIdRelyingParty();
                    var fetch = new FetchRequest();// new 
                    var b = new UriBuilder(Request.Url) { Query = "" };
                    var req = openid.CreateRequest(discoveryuri, b.Uri, b.Uri);
                    fetch.Attributes.AddRequired(WellKnownAttributes.Contact.Email);
                    fetch.Attributes.AddRequired(WellKnownAttributes.Name.FullName);
                    req.AddExtension(fetch);
                    return req.RedirectingResponse.AsActionResult();
                }
                catch (ProtocolException ex)
                {
                    logger.ErrorFormat(" LoginController : Authenticate method has error, Exception:" + ex.ToString());
                    ViewData["Message"] = ex.Message;
                    return View("Login");
                }
            }
            else
            {
                logger.Info("" + returnUrl + "] LoginController : Authenticate method :when responce not  null  ");
                switch (response.Status)
                {
                    case AuthenticationStatus.Authenticated:
                        logger.Info("" + response.Status + "] LoginController : Authenticate method : responce status  ");
                        var fetchResponse = response.GetExtension<FetchResponse>();
                        string email = fetchResponse.GetAttributeValue(WellKnownAttributes.Contact.Email);
                        string userIPAddress = HttpContext.Request.UserHostAddress;
                        SecurityManager manager = new SecurityManager();                            
                        int userID = manager.IsValidUser(email);

                        if (userID != 0)
                        {
                            ViewBag.IsFailed = "False";
                            logger.Info("" + userID + "] LoginController : Authenticate method : user id id not null  ");
                            Session["FriendlyIdentifier"] = response.FriendlyIdentifierForDisplay;
                            Session["UserEmail"] = email;

                            FormsAuthentication.SetAuthCookie(email, false);

                            WebSession.UserEmail = email;
                            WebSession.UserID = userID;

                            UserManager userManager = new UserManager();
                            WebSession.AssignedSites = userManager.GetAssignedSites(userID);



                            if (!string.IsNullOrEmpty(returnUrl))
                            {
                                logger.Info("" + returnUrl + "] LoginController : Authenticate method : retutn url not null then return Redirect   ");
                                return Redirect(returnUrl);
                            }
                            else
                            {
                                logger.Info("" + returnUrl + "] LoginController : Authenticate method : retutn url null then return RedirectToAction   ");
                                //
                                return Redirect("/Home");
                            }
                        }
                        else
                        {
                            ViewBag.IsFailed = "True";
                            logger.Info("" + returnUrl + "] LoginController : Authenticate method :user id null   ");
                            if (!string.IsNullOrEmpty(returnUrl))
                            {
                                logger.Info("" + returnUrl + "] LoginController : Authenticate method :and return Redirect   ");
                                return Redirect(returnUrl);
                            }
                            else
                            {
                                logger.Info("" + returnUrl + "] LoginController : Authenticate method :and return RedirectToAction   ");

                                return View("Index");

                            }
                        }

                    case AuthenticationStatus.Canceled:
                        logger.Info("" + response.Status + "] LoginController : Authenticate method : AuthenticationStatus.Canceled  and return view  ");
                        ViewData["Message"] = "Canceled at provider";
                        return View("Login");
                    case AuthenticationStatus.Failed:
                        logger.Info("" + response.Status + "] LoginController : Authenticate method : AuthenticationStatus.Failed and return view   ");
                        logger.Error(response.Exception.Message);
                        ViewData["Message"] = response.Exception.Message;
                        return View("Login");
                }

            }
            logger.Info("" + returnUrl + "] LoginController : Authenticate method end and return  EmptyResult");
            return new EmptyResult();
        }
        catch (Exception ex)
        {
            logger.Error(" LoginController : Authenticate method ", ex);
            throw;
        }
    }


来源:https://stackoverflow.com/questions/83476/google-authentication-api-how-to-get-the-users-gmail-address

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!