如何通过API调用从id_token获取用户配置文件

时间:2016-10-11 06:30:42

标签: api backend google-authentication

我有一个应用程序使用的API,该应用程序向我发送了已经过身份验证的用户或Google帐户的id_token。

如何在ASP.net中向谷歌API服务器进行API调用(而不是java脚本调用)以获取用户配置文件信息,例如名称,以便我可以将用户保存在应用程序的数据库中。

我已经有一个带有客户端ID和客户端密钥的Google项目。

1 个答案:

答案 0 :(得分:0)

所以我找到了一种方法,虽然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"
}
相关问题