通过Google API获取用户信息
是否可以通过 Google API 从用户的个人资料中获取信息?如果可以的话我应该使用哪个API?
我对此类信息感兴趣:
- 用户个人资料的网址(例如 https://profiles.google.com/115063121183536852887< /a>);
- 性别(性别);
- 个人资料照片。
从用户的个人资料中获取其他信息也很酷。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
将其添加到范围 - https://www.googleapis.com/auth/userinfo.profile
并在授权后完成后,从 - https://www.googleapis.com/oauth2/v1/userinfo? alt=json
它包含大量内容 - 包括姓名、公共个人资料网址、性别、照片等。
Add this to the scope - https://www.googleapis.com/auth/userinfo.profile
And after authorization is done, get the information from - https://www.googleapis.com/oauth2/v1/userinfo?alt=json
It has loads of stuff - including name, public profile url, gender, photo etc.
范围 - https://www.googleapis.com/auth/userinfo.profile
获取 < a href="https://www.googleapis.com/oauth2/v1/userinfo?alt=json&access_token=youraccess_token" rel="noreferrer">https://www.googleapis.com/oauth2/v1/userinfo?alt=json&access_token=youraccess_token
您将得到 json:
致 Tahir Yasin:
这是一个 php 示例。
您可以使用 json_decode 函数来获取 userInfo 数组。
scope - https://www.googleapis.com/auth/userinfo.profile
get https://www.googleapis.com/oauth2/v1/userinfo?alt=json&access_token=youraccess_token
you will get json:
To Tahir Yasin:
This is a php example.
You can use json_decode function to get userInfo array.
这是一个糟糕的文档/已经发生了变化。我会参考此 https://developers.google.com/oauthplayground 以获取最新端点。
自
2021
起,userinfo
的正确端点为https://www.googleapis.com/oauth2/v1/userinfo
因此,一旦您获得
access_token
您可以重要:获取
openid 电子邮件配置文件
范围
所需的所有信息。This is poorly document / there have been changes. I would reference this https://developers.google.com/oauthplayground for up to date endpoints.
As of
2021
the correct endpoint foruserinfo
ishttps://www.googleapis.com/oauth2/v1/userinfo
So once you get the
access_token
you can doImportant: To get all the information you need
scope
ofopenid email profile
.此范围 https://www.googleapis.com/auth/userinfo.profile 现已弃用。请查看https://developers.google.com/+/api/auth-migration#timetable。
您将用于获取个人资料信息的新范围是:profile 或 https://www.googleapis.com/auth/plus。登录
,端点为 - https://www.googleapis.com/plus/v1/people/< /a>{userId} - userId 可以只是“我”对于当前登录的用户。
This scope https://www.googleapis.com/auth/userinfo.profile has been deprecated now. Please look at https://developers.google.com/+/api/auth-migration#timetable.
New scope you will be using to get profile info is: profile or https://www.googleapis.com/auth/plus.login
and the endpoint is - https://www.googleapis.com/plus/v1/people/{userId} - userId can be just 'me' for currently logged in user.
我正在使用
PHP
并通过使用 google-api-php-client假设以下代码用于将用户重定向到 Google 身份验证页面:
假设将有效的身份验证代码返回到
redirect_url
,则将生成以下内容来自身份验证代码的令牌以及提供基本信息个人资料信息:但是,不会返回位置。 新 YouTube 帐户没有 YouTube 特定用户名
I'm using
PHP
and solved this by using version 1.1.4 of google-api-php-clientAssuming the following code is used to redirect a user to the Google authentication page:
Assuming a valid authentication code is returned to the
redirect_url
, the following will generate a token from the authentication code as well as provide basic profile information:However, location is not returned. New YouTube accounts don't have YouTube specific usernames
我正在使用 Google API for .Net,但毫无疑问,您可以使用其他版本的 API 以相同的方式获取此信息。
正如user872858提到的,范围userinfo.profile已被弃用(Google 文章)。
为了获取用户个人资料信息,我使用以下代码(重写了 google 示例的部分):
然后,您可以使用 userProfile 访问几乎任何内容。
更新:要使此代码正常工作,您必须在谷歌登录按钮上使用适当的范围。例如我的按钮:
I am using Google API for .Net, but no doubt you can find the same way to obtain this information using other version of API.
As user872858 mentioned, scope userinfo.profile has been deprecated (google article) .
To obtain user profile info I use following code (re-written part from google's example):
Than, you can access almost anything using userProfile.
UPDATE: To get this code working you have to use appropriate scopes on google sign in button. For example my button:
如果您使用Oath2,那么客户端将提供id_token
,然后您可以使用此网址在服务器上验证它
https://oauth2.googleapis.com/tokeninfo?id_token=your_id_token
将解决诸如幸运符之类的问题
if you use Oath2 then client will give id_token
then you can validate it on server with this url
https://oauth2.googleapis.com/tokeninfo?id_token=your_id_token
will solve problem like lucky charm
需要运行 3 个步骤。
非常有趣的是,这种最简单的用法并没有在任何地方得到明确的描述。 我相信存在危险,您应该注意响应中的
verified_email
参数。因为如果我没记错,它可能会产生虚假电子邮件来注册您的申请。 (这只是我的解释,很有可能我是错的!)我发现 facebook 的 OAuth 机制描述得非常清楚。
There are 3 steps that needs to be run.
It is very interesting that this simplest usage is not clearly described anywhere. And i believe there is a danger, you should pay attention to the
verified_email
parameter coming in the response. Because if I am not wrong it may yield fake emails to register your application. (This is just my interpretation, has a fair chance that I may be wrong!)I find facebook's OAuth mechanics much much clearly described.
如果您只想获取 Web 应用程序访问者的 Google 用户 ID、姓名和图片 - 这是我 2020 年的纯 PHP 服务端解决方案,不使用任何外部库 -
如果您阅读 将 OAuth 2.0 用于 Web 服务器应用程序 Google 指南(请注意,Google喜欢更改指向其自己文档的链接),那么您只需执行 2 个步骤:
返回的令牌之一称为“id_token”,包含访问者的用户 ID、姓名和照片。
这是我编写的网页游戏的 PHP 代码。最初我使用的是 Javascript SDK,但后来我注意到仅使用客户端 SDK 时,虚假的用户数据可能会传递到我的网页游戏(尤其是用户 ID,这对我的游戏很重要),所以我转而使用服务器端的 PHP:
您可以使用 PHP 库通过验证 JWT 签名来添加额外的安全性。就我的目的而言,这是不必要的,因为我相信谷歌不会通过发送虚假的访问者数据来背叛我的小网页游戏。
此外,如果您想获取访问者的更多个人数据,那么您需要第三步:
或者您可以代表用户获取更多权限 - 请参阅 Google API 的 OAuth 2.0 范围 文档。
最后,我的代码中使用的 APP_ID 和 APP_SECRET 常量 - 您可以从 Google API 控制台获取它:
If you only want to fetch the Google user id, name and picture for a visitor of your web app - here is my pure PHP service side solution for the year 2020 with no external libraries used -
If you read the Using OAuth 2.0 for Web Server Applications guide by Google (and beware, Google likes to change links to its own documentation), then you have to perform only 2 steps:
One of the returned tokens is called "id_token" and contains the user id, name and photo of the visitor.
Here is the PHP code of a web game by me. Initially I was using Javascript SDK, but then I have noticed that fake user data could be passed to my web game, when using client side SDK only (especially the user id, which is important for my game), so I have switched to using PHP on the server side:
You could use a PHP library to add additional security by verifying the JWT signature. For my purposes it was unnecessary, because I trust that Google will not betray my little web game by sending fake visitor data.
Also, if you want to get more personal data of the visitor, then you need a third step:
Or you could get more permissions on behalf of the user - see the long list at the OAuth 2.0 Scopes for Google APIs doc.
Finally, the APP_ID and APP_SECRET constants used in my code - you get it from the Google API console:
如果您处于客户端 Web 环境中,新的 auth2 javascript API 包含急需的
getBasicProfile()
函数,该函数返回用户的姓名、电子邮件和图像 URL。https://developers.google.com/identity/sign-in/web /reference#googleusergetbasicprofile
If you're in a client-side web environment, the new auth2 javascript API contains a much-needed
getBasicProfile()
function, which returns the user's name, email, and image URL.https://developers.google.com/identity/sign-in/web/reference#googleusergetbasicprofile
获取 id 和 accessToken 的方法
这就是我使用新的 Google Identity Services API JS 代码
解释
client.requestAccessToken();
方法 弹出Google登录界面This is how I get the id and accessToken using the new Google Identity Services API
JS Code
Explanation
client.requestAccessToken();
method to popup Google sign-in screen