【问题标题】:How to get user profile from id_token through API call如何通过 API 调用从 id_token 获取用户配置文件
【发布时间】:2016-10-11 06:30:41
【问题描述】:
我有一个应用程序使用的 API,应用程序向我发送一个已通过身份验证的用户或 Google 帐户的 id_token。
如何在 ASP.net 中向 google API 服务器进行 API 调用(不是 java 脚本调用)以获取用户的个人资料信息,例如姓名,以便我可以将用户保存在应用程序的数据库中。
我已经有一个带有客户端 ID 和客户端密码的谷歌项目。
【问题讨论】:
标签:
api
backend
google-authentication
【解决方案1】:
所以我找到了一种通过 asp.net API 获取我想要的用户信息的方法。
using (var client = new System.Net.WebClient())
{
var uri = new Uri("https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=" + ACCESSTOKENFROMLOGGININ + "&access_type=offline");
var response = client.DownloadString(uri);
}
you response will have these fields
{
"issued_to": "",
"audience": "",
"user_id": "",
"scope": "",
"expires_in": 756,
"email": "",
"verified_email": true,
"access_type": "online"
}
Then you could make another call to get more personal information
var userInfoUri = new Uri("https://www.googleapis.com/oauth2/v2/userinfo");
var userInfoClient = new System.Net.WebClient();
client.Headers.Add("Authorization", "Bearer " + YOUACCESSToken);
and you will receive an object like this
{
"id": "",
"name": "",
"given_name": "",
"family_name": "",
"link": "",
"picture": "",`enter code here`
"locale": "en"
}