Tôi đang sử dụng Google API cho .Net, nhưng không nghi ngờ gì bạn có thể tìm cách tương tự để nhận thông tin này bằng cách sử dụng phiên bản API khác. Là user872858 được đề cập, phạm vi userinfo.profile đã không được chấp nhận (google article).
Để có được thông tin lý lịch thành viên tôi sử dụng đoạn mã sau (viết lại một phần từ google's example):
IAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow(
new GoogleAuthorizationCodeFlow.Initializer
{
ClientSecrets = Secrets,
Scopes = new[] { PlusService.Scope.PlusLogin,"https://www.googleapis.com/auth/plus.profile.emails.read" }
});
TokenResponse _token = flow.ExchangeCodeForTokenAsync("", code, "postmessage",
CancellationToken.None).Result;
// Create an authorization state from the returned token.
context.Session["authState"] = _token;
// Get tokeninfo for the access token if you want to verify.
Oauth2Service service = new Oauth2Service(
new Google.Apis.Services.BaseClientService.Initializer());
Oauth2Service.TokeninfoRequest request = service.Tokeninfo();
request.AccessToken = _token.AccessToken;
Tokeninfo info = request.Execute();
if (info.VerifiedEmail.HasValue && info.VerifiedEmail.Value)
{
flow = new GoogleAuthorizationCodeFlow(
new GoogleAuthorizationCodeFlow.Initializer
{
ClientSecrets = Secrets,
Scopes = new[] { PlusService.Scope.PlusLogin }
});
UserCredential credential = new UserCredential(flow,
"me", _token);
_token = credential.Token;
_ps = new PlusService(
new Google.Apis.Services.BaseClientService.Initializer()
{
ApplicationName = "Your app name",
HttpClientInitializer = credential
});
Person userProfile = _ps.People.Get("me").Execute();
}
Thân, bạn có thể truy cập vào hầu hết mọi thứ bằng UserProfile.
CẬP NHẬT: Để mã này hoạt động, bạn phải sử dụng phạm vi phù hợp trên nút đăng nhập google. Ví dụ: nút của tôi:
<button class="g-signin"
data-scope="https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/plus.profile.emails.read"
data-clientid="646361778467-nb2uipj05c4adlk0vo66k96bv8inqles.apps.googleusercontent.com"
data-accesstype="offline"
data-redirecturi="postmessage"
data-theme="dark"
data-callback="onSignInCallback"
data-cookiepolicy="single_host_origin"
data-width="iconOnly">
</button>
Tôi đã sử dụng các url ở trên nhưng không thể nhận hồ sơ của người dùng. Chỉ nhận được '{'. Plz u có thể đăng một số mã hoặc liên kết. Cảm ơn trước. – Panache
u có thể xây dựng cách lấy hồ sơ người dùng bằng cách sử dụng các url ở trên ... plz .. – Panache
Uhm, chỉ cho phép yêu cầu với OAuth 2. URL đó trả về dữ liệu cho người dùng hiện đã đăng nhập. Nó sẽ cung cấp cho bạn một lỗi nếu bạn thực hiện yêu cầu mà không gửi tiêu đề OAuth. –