Question

J'ai étudié l'API d'authentification Google (AuthSub) ... Ma question est la suivante: comment puis-je obtenir les informations de compte de l'utilisateur (au moins son adresse Gmail) une fois l'authentification terminée?

Parce qu'actuellement, tout ce que je récupère du processus d'authentification, c'est un jeton qui m'autorise l'accès au service Google que j'ai spécifié dans le champ d'application, mais il n'existe pas de moyen simple d'obtenir même l'identifiant de connexion de l'utilisateur (adresse Gmail). comme je peux le dire ...

Si oui, quel service Google me permet d'accéder aux informations de l'utilisateur?

Était-ce utile?

La solution

À l'aide des services Google AppEngine GData, vous pouvez demander à l'utilisateur de vous donner accès à leurs comptes Google Mail, Agenda, Picasa, etc. Consultez-le ici .

Autres conseils

L'API d'authentification Google est un système basé sur un jeton permettant d'authentifier un utilisateur valide. Il n’expose aucune autre interface permettant d’obtenir les informations du titulaire du compte à l’autorisateur.

Vous pouvez obtenir certaines des données via l’ API OpenID , avec l'extension de la hache. Si vous vous authentifiez à l'aide d'autres méthodes, le mieux que j'ai trouvé est d'appeler https://www-opensocial.googleusercontent.com/api/people/@me/@self et vous obtiendrez le nom, l'adresse électronique et image. Assurez-vous d'avoir http://www-opensocial.googleusercontent.com/api dans les portées lors de l'authentification.

    [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;
        }
    }
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top