通过Google API获取用户信息

是否可以通过Google API从用户个人资料中获取信息? 如果可能,我应该使用哪个API?

我对这样的信息很感兴趣:

  • url到用户个人资料(例如https://profiles.google.com/115063121183536852887 );
  • 性别(性别);
  • 头像照片。

从用户档案中获取其他信息也是很酷的。

将其添加到范围 – https://www.googleapis.com/auth/userinfo.profile

完成授权后,从 – https://www.googleapis.com/oauth2/v1/userinfo?alt=json获取信息;

它有大量的东西 – 包括名字,公共个人资料url,性别,照片等

范围 – https://www.googleapis.com/auth/userinfo.profile

return youraccess_token = access_token 

获取https://www.googleapis.com/oauth2/v1/userinfo?hl=zh_CN?hl=zh-CN&id=json&access_token=youraccess_token

你会得到json:

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

致Tahir Yasin:

这是一个php的例子。
你可以使用json_decode函数来获取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']; 

此范围https://www.googleapis.com/auth/userinfo.profile现在已被弃用。; 请查看https://developers.google.com/+/api/auth-migration#timetable

您将用于获取个人资料信息的新范围是:个人资料或https://www.googleapis.com/auth/plus.login

而端点是 – https://www.googleapis.com/plus/v1/people/ {userId} – userId对于当前login的用户可以是“我”。

我正在使用PHP并通过使用google-api-php-client版本1.1.4解决了这个问题

假设以下代码用于将用户redirect到Google身份validation页面:

  $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(); 

假设一个有效的validation码被返回到redirect_url ,下面将从validation码生成一个令牌,并提供基本的configuration文件信息:

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

但是,位置不返回。 新的YouTube帐户没有YouTube专用用户名

我为.Net使用Google API,但毫无疑问,您可以使用其他版本的API以相同方式获取此信息。 正如user872858提到的,范围userinfo.profile已被弃用( 谷歌文章 )。

要获取用户资料信息我使用下面的代码(从谷歌的例子重写的部分):

 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(); } 

比你可以使用userProfile访问几乎任何东西。

更新:为了使这个代码工作,你必须在谷歌loginbutton上使用适当的作用域。 例如我的button:

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

如果您处于客户端Web环境中,则新的auth2 JavaScript API将包含一个非常需要的getBasicProfile()函数,该函数将返回用户的姓名,电子邮件和图像URL。

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