我正在使用令牌验证器google api
https://www.googleapis.com/oauth2/v3/tokeninfo?id_token=XYZ123它给出了用户在浏览器中的电子邮件id,比如
{
"iss": "https://accounts.google.com",
"sub": "110169484474386276334",
"azp": "1008719970978-hb24n2dstb40o45d4feuo2ukqmcc6381.apps.googleusercontent.com",
"aud": "1008719970978-hb24n2dstb40o45d4feuo2ukqmcc6381.apps.googleusercontent.com",
"iat": "1433978353",
"exp": "1433981953",
"email" OAuth scopes to the application.
"email": "testuser@gmail.com",
"email_verified": "true",
"name" : "Test User",
"picture": "https://lh4.googleusercontent.com/-kYgzyAWpZzJ/ABCDEFGHI/AAAJKLMNOP/tIXL9Ir44LE/s99-c/photo.jpg",
"given_name": "Test",
"family_name": "User",
"locale": "en"
}
现在我想从后端代码调用这个URL并检索邮件id以进行身份验证
我试过这样做
function(string access_token){
var url = "https://www.googleapis.com/oauth2/v3/tokeninfo?id_token=" + access_token;
WebRequest request = WebRequest.Create(url);
request.Method = "GET";
using (WebResponse response = request.GetResponse())
{
using (Stream stream = response.GetResponseStream())
{
XmlTextReader reader = new XmlTextReader(stream);
}
}
}
但是无法从json中获取数据
替代品吗?
我只需要获得用户的电子邮件id与access_token或idToken,客户端将发送到服务器
已解决
var json = new WebClient().DownloadString(url);
使用WebClient从浏览器获取数据
反序列化后得到数据