从令牌C#获取用户信息 [英] Get user info from token c#
问题描述
我已经通过他们的api成功地从Google提供商那里获取了用于上述oauth身份验证的密钥令牌。
i have successfully got the key token for the above oauth authentication from Google provider through their api.
让我们考虑访问令牌 是xxxii-xxxxx-xxx-xxxxx-xxxxx,其范围为 https://www.googleapis .com / auth / userinfo.profile
let us consider "access-token" is xxxii-xxxxx-xxx-xxxxx-xxxxx which has scope of https://www.googleapis.com/auth/userinfo.profile
现在,当我使用访问令牌访问用于检索用户信息值的浏览器时,
now when i hit the browser with access token for retrieving user information values has
https://www.googleapis.com/oauth2/v1/userinfo?access_token=xxxii-xxxxx-xxx-xxxxx-xxxxx;
我得到的答复是
{
"id": "XXXXXXXXXXXXXX",
"name": "XXXXXXXXXXXXXXXX",
"given_name": "XXXXXXXXXXX",
"family_name": "X",
"picture": "XXXXXXXXXX/photo.jpg",
"locale": "en"
}
我的问题是,当我通过代码解析以上请求时,我没有通过浏览器得到响应
My problem is when i parse the above request through code i am not getting responses as i got through browser
我使用的代码
String userInfo = "https://www.googleapis.com/oauth2/v1/userinfo?access_token="+token;
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(userInfo);
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
StreamReader sr = new StreamReader(resp.GetResponseStream());
sr.Close();
JObject jsonResp = JObject.Parse(userInfo);
string info = "";
info += "<h3>" + jsonResp.Root["name"] + "</h3>";
info += "<img src='" + jsonResp.Root["picture"] + "' width='120'/><br/>";
info += "<br/>ID : " + jsonResp.Root["id"];
info += "<br/>Email : " + jsonResp.Root["email"];
Response.Write(info);
作为回应,我得到的是空引用。
in response i am getting null reference .
和我在行中得到的错误
JObject jsonResp = JObject.Parse(userInfo);
as
解析值h时遇到意外字符。第1行,位置1。
Unexpected character encountered while parsing value: h. Line 1, position 1.
堆栈跟踪:
$ b $在Newtonsoft.Json.JsonTextReader.ReadInternal()的b在Newtonsoft.Json.JsonTextReader.ReadInternal()
在Newtonsoft.Json.JsonTextReader.Read()
在Newtonsoft .Json.Linq.JObject.Load(JsonReader reader)
at牛顿.Json.Linq.JObject.Parse(String json)
在_Default.getresponse(String token)在d:\Oauth\ WebSite3\Default.aspx.cs:第99行
at Newtonsoft.Json.JsonTextReader.ParseValue(Char currentChar) at Newtonsoft.Json.JsonTextReader.ReadInternal() at Newtonsoft.Json.JsonTextReader.Read() at Newtonsoft.Json.Linq.JObject.Load(JsonReader reader) at Newtonsoft.Json.Linq.JObject.Parse(String json) at _Default.getresponse(String token) in d:\Oauth\WebSite3\Default.aspx.cs:line 99
正在等待您的宝贵建议和意见
Waiting for your valuable suggestions and comments
推荐答案
使用以下代码获取用户信息:
Use this code to get user info:
var userInfoUrl = "https://www.googleapis.com/oauth2/v1/userinfo";
var hc = new HttpClient();
hc.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authToken);
var response = hc.GetAsync(userInfoUrl).Result;
dynamic userInfo = response.Content.ReadAsAsync().Result;
return userInfo;
有一篇关于dotnetopenoauth和google api集成的好文章: http://www.dotnetopenauth.net/documentation/securityscenarios/
There is a good article about integration of dotnetopenoauth and google api: http://www.dotnetopenauth.net/documentation/securityscenarios/
这篇关于从令牌C#获取用户信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!