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

4

6

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?

Fortunato answered 17/9, 2008 at 13:55 Comment(2)
I also want to know that! Aargh :-)Dyanne
great tutorial and script to get user's authentication from gmail and accessing basic data 9lessons.info/2011/07/login-with-google-account.htmlSchach
T
2

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.

Tempo answered 17/9, 2008 at 14:13 Comment(1)
Page not found :P @Joe SkoraStephenstephenie
T
4

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.

Thereupon answered 17/9, 2008 at 14:6 Comment(0)
T
2

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.

Tempo answered 17/9, 2008 at 14:13 Comment(1)
Page not found :P @Joe SkoraStephenstephenie
P
2

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.

Plasm answered 18/8, 2011 at 23:35 Comment(1)
This way doesn't return the emailCongo
Q
0
    [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;
        }
    }
Quiteria answered 11/2, 2013 at 19:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.