2008-09-17 39 views
6

Tôi đã nghiên cứu API xác thực của Google (AuthSub) ... Câu hỏi của tôi là, làm cách nào để nhận thông tin tài khoản của người dùng (ít nhất là địa chỉ Gmail của họ) sau xác thực đã trôi qua?API xác thực của Google: Cách lấy địa chỉ gmail của người dùng

Vì hiện tại, tất cả những gì tôi nhận được từ quá trình xác thực là mã thông báo cấp cho tôi quyền truy cập vào dịch vụ Google mà tôi đã chỉ định trong phạm vi, nhưng không có cách nào dễ dàng để có được id đăng nhập của người dùng (địa chỉ Gmail) như tôi có thể nói ...


Nếu có, dịch vụ Google nào cho phép tôi truy cập thông tin của người dùng?

+0

Tôi cũng muốn biết điều đó! Aargh :-) –

+0

hướng dẫn và tập lệnh tuyệt vời để xác thực người dùng từ gmail và truy cập dữ liệu cơ bản http://www.9lessons.info/2011/07/login-with-google-account.html –

Trả lời

2

Sử dụng dịch vụ GDA của Google AppEngine, bạn có thể yêu cầu người dùng cấp cho bạn quyền truy cập vào Gmail, Lịch, Picasa, v.v. của họ Kiểm tra xem nó here.

+1

Không tìm thấy trang: P @Joe Skora –

4

API xác thực của Google là hệ thống dựa trên mã thông báo để xác thực người dùng hợp lệ. Nó không hiển thị bất kỳ giao diện nào khác cho phép nhận thông tin chủ tài khoản trở lại người ủy quyền.

2

Bạn có thể lấy một số dữ liệu qua số OpenID API, với phần mở rộng rìu. Nếu bạn đang xác thực với các phương pháp khác, tốt nhất tôi thấy là gọi https://www-opensocial.googleusercontent.com/api/people/@me/@self và nó sẽ giúp bạn có được tên, email và hình ảnh. Đảm bảo có http://www-opensocial.googleusercontent.com/api trong phạm vi khi xác thực.

+0

Cách này không trả lại email – Burjua

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; 
     } 
    } 
Các vấn đề liên quan