我对OAuth Arena和Google ApI很陌生,但我在这里想要实现的目标非常简单。
用户点击Google Connect按钮和我的网络服务应该能够从Google服务器获取所有用户个人资料信息:
我已经编写了代码来获取 AccessToken(我还没有测试它),但假设它工作正常,现在我应该如何要求 Google API 给我用户配置文件?我确实在Google消费者类中看到名为"获取联系人"的静态函数,但我没有看到任何获取个人资料数据的选项。也许我错过了什么?
这是我用来获取访问令牌的代码:
IConsumerTokenManager tokenManager =
new LocalTokenManager(consumerKey,consumerSecret);
var googleConsumer =
new WebConsumer(GoogleConsumer.ServiceDescription, tokenManager);
var tokenResult = googleConsumer.ProcessUserAuthorization();
return tokenResult.AccessToken;
现在,如何从中获取用户配置文件?
一旦你有了你的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);
流程 :请求带有访问令牌的谷歌用户信息网址,获取响应并显示信息。
让我知道您对使用他们的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