我是OAuth Arena和Google ApI的新手,但我想在这里实现的非常简单。
用户点击Google Connect按钮,我的网络服务应该能够从Google服务器获取所有用户个人资料信息:
我已经编写了代码来获取AccessToken(我还没有测试它),但假设工作正常,那么我现在应该如何让Google API为我提供用户个人资料呢?我确实在GoogleConsumer Class中看到了名为Get Contacts的静态函数,但我没有看到任何获取profiledata的选项。可能有些东西我不见了?
这是我的代码,我正在使用accessToken:
IConsumerTokenManager tokenManager =
new LocalTokenManager(consumerKey,consumerSecret);
var googleConsumer =
new WebConsumer(GoogleConsumer.ServiceDescription, tokenManager);
var tokenResult = googleConsumer.ProcessUserAuthorization();
return tokenResult.AccessToken;
现在,如何从中获取用户个人资料?
答案 0 :(得分:2)
一旦你有了Access_Token(访问类型离线;并且设置了范围/权限以便获取用户信息),你可以尝试以下方法(未经测试,如果发生任何错误,请告诉我):
string userInfo = "";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(action);
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
StreamReader sr = new StreamReader(resp.GetResponseStream());
userInfo = "https://www.googleapis.com/oauth2/v1/userinfo?access_token=" + "YOUR_ACCESSTOKEN";
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"];
info += "<br/>Verified_email : " + jsonResp.Root["verified_email"];
info += "<br/>Given_name : " + jsonResp.Root["given_name"];
info += "<br/>Family_name : " + jsonResp.Root["family_name"];
info += "<br/>Link : " + jsonResp.Root["link"];
info += "<br/>Gender : " + jsonResp.Root["gender"];
Response.Write(info);
流程:使用访问令牌请求google userinfo网址,获取响应并显示信息。
答案 1 :(得分:1)
让我知道您对使用他们的GET方法访问个人资料的Google信息有什么看法?https://developers.google.com/+/api/latest/people/get 这是我的C#示例。
string urlGoogle = "https://www.googleapis.com/plus/v1/people/me";
HttpWebRequest client = HttpWebRequest.Create(urlGoogle) as HttpWebRequest;
client.Method = "GET";
client.Headers.Add("Authorization", "Bearer " + accessToken);
using (HttpWebResponse response = (HttpWebResponse)client.GetResponse())
{
using (Stream dataStream = response.GetResponseStream())
{
using (StreamReader reader = new StreamReader(dataStream))
{
if (response.StatusCode == HttpStatusCode.OK)
{
var json = new JavaScriptSerializer();
var data = json.Deserialize<IDictionary<string, object>>(reader.ReadToEnd());
//....... here in data you have all json fields for the profile