2011-08-20 37 views

Trả lời

84

Thêm phần này vào phạm vi - https://www.googleapis.com/auth/userinfo.profile

Và sau khi uỷ quyền được thực hiện, lấy thông tin từ - https://www.googleapis.com/oauth2/v1/userinfo?alt=json

Nó có vô số thứ - bao gồm tên, địa chỉ hồ sơ công khai giới tính, hình ảnh, vv

+0

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

+0

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

+0

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. –

75

phạm vi - https://www.googleapis.com/auth/userinfo.profile

return youraccess_token = access_token 

được https://www.googleapis.com/oauth2/v1/userinfo?alt=json&access_token=youraccess_token

bạn sẽ nhận được json:

{ 
"id": "xx", 
"name": "xx", 
"given_name": "xx", 
"family_name": "xx", 
"link": "xx", 
"picture": "xx", 
"gender": "xx", 
"locale": "xx" 
} 

Để Tahir Yasin:

Đây là một ví dụ php.
Bạn có thể sử dụng hàm json_decode để lấy mảng userInfo.

$q = 'https://www.googleapis.com/oauth2/v1/userinfo?access_token=xxx'; 
$json = file_get_contents($q); 
$userInfoArray = json_decode($json,true); 
$googleEmail = $userInfoArray['email']; 
$googleFirstName = $userInfoArray['given_name']; 
$googleLastName = $userInfoArray['family_name']; 
+0

Cách nhận thêm thông tin về người dùng? –

+1

làm cách nào để tôi có thể sử dụng phản hồi của họ? –

+3

nó chỉ cung cấp id –

25

Phạm vi này https://www.googleapis.com/auth/userinfo.profile hiện không được chấp nhận. Vui lòng xem https://developers.google.com/+/api/auth-migration#timetable.

New phạm vi bạn sẽ được sử dụng để nhận được thông tin hồ sơ là: Hồ sơ hoặc https://www.googleapis.com/auth/plus.login

và điểm cuối là - https://www.googleapis.com/plus/v1/people/ {userId} - userId có thể chỉ là 'tôi' cho hiện đang đăng nhập người dùng.

+0

Điều này là một sự bình an quan trọng của thông tin hướng tới việc có bằng chứng tương lai tích hợp. biết thêm thông tin về phạm vi không dùng nữa https://developers.google.com/+/web/api/rest/oauth –

4

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> 
16

Tôi đang sử dụng PHP và giải quyết vấn đề này bằng cách sử dụng phiên bản 1.1.4 google-api-php-client

Giả sử mã sau đây được sử dụng để chuyển hướng người dùng đến trang xác thực của Google:

$client = new Google_Client(); 
$client->setAuthConfigFile('/path/to/config/file/here'); 
$client->setRedirectUri('https://redirect/url/here'); 
$client->setAccessType('offline'); //optional 
$client->setScopes(['profile']); //or email 
$auth_url = $client->createAuthUrl(); 
header('Location: ' . filter_var($auth_url, FILTER_SANITIZE_URL)); 
exit(); 

Giả sử một mã xác thực hợp lệ được trả lại cho redirect_url, sau đây sẽ tạo ra một mã thông báo từ xác thực mã cũng như cung cấp thông tin hồ sơ cơ bản:

//assuming a successful authentication code is return 
$authentication_code = 'code-returned-by-google'; 
$client = new Google_Client(); 
//.... configure $client object code goes here 
$client->authenticate($authentication_code); 
$token_data = $client->getAccessToken(); 

//get user email address 
$google_oauth =new Google_Service_Oauth2($client); 
$google_account_email = $google_oauth->userinfo->get()->email; 
//$google_oauth->userinfo->get()->familyName; 
//$google_oauth->userinfo->get()->givenName; 
//$google_oauth->userinfo->get()->name; 
//$google_oauth->userinfo->get()->gender; 
//$google_oauth->userinfo->get()->picture; //profile picture 

Tuy nhiên, vị trí không được trả lại. New YouTube accounts don't have YouTube specific usernames

+0

cách nhận Vị trí? – SoftSan

+0

tôi không thể nhận thông tin về giới tính (tôi đã giữ thông tin giới tính công khai) bằng cách sử dụng phạm vi này. tôi đã thử sân chơi oauth developers.google.com/oauthplayground cho việc này. tôi muốn làm điều này bằng cách sử dụng REST API trên phía máy chủ. Bạn có thể giúp tôi về điều này? –

+0

Không thể nhận giới tính. Và trên một số tài khoản, không có gì ngoại trừ email được trả lại. Ý tưởng? –

1

Nếu bạn đang ở trong môi trường web phía máy khách, API javascript auth2 mới chứa hàm getBasicProfile() rất cần thiết, trả về tên, email và URL hình ảnh của người dùng.

https://developers.google.com/identity/sign-in/web/reference#googleusergetbasicprofile

+0

Nhưng URL API thực tế là gì? Tôi đã xem tài liệu, tôi không thể tìm thấy URL API thực tế. Google dường như đang đẩy chúng tôi đến SDK của họ, nhưng không phải ai cũng muốn sử dụng SDK. – Supertecnoboff

Các vấn đề liên quan