如何使用 google Oauth2 获取用户 json 数据
How to get User json data with google Oauth2
我正在尝试发回我的令牌以请求有关用户登录的更多信息。我想取回 json。
使用下面的代码我得到一个异常"The given key was not present in the dictionary."
我做错了什么?
public void Login(Action<Mobile.Google.Account> googleLoginCompleted, Action googleLoginFailed)
{
var auth = new OAuth2Authenticator(
clientId: "myid",
clientSecret: "secret",
scope: "https://www.googleapis.com/auth/plus.login",
authorizeUrl: new Uri("https://accounts.google.com/o/oauth2/v2/auth"),
redirectUrl: new Uri("http://adults.wicareerpathways.org"),
accessTokenUrl: new Uri("https://www.googleapis.com/oauth2/v4/token"));
auth.Completed += (sender, e) =>
{
if (e.IsAuthenticated)
{
var values = e.Account.Properties;
var access_token = values["access_token"];
var googleAccount = new Mobile.Google.Account
{
Username = e.Account.Username,
Properties = e.Account.Properties
};
try
{
var request = HttpWebRequest.Create(string.Format(@"https://www.googleapis.com/oauth2/v2/userinfo?access_token=" + access_token + "&format=json", ""));
request.ContentType = "application/json";
request.Method = "GET";
var user_ID = values["user_id"];
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
System.Console.Out.WriteLine("Stautus Code is: {0}", response.StatusCode);
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
var content = reader.ReadToEnd();
GoogleAccountDetails result = Newtonsoft.Json.JsonConvert.DeserializeObject<GoogleAccountDetails>(content);
googleAccount.Username = result.id;
if (googleLoginCompleted != null)
{
googleLoginCompleted(googleAccount);
}
}
}
}
catch (Exception exx)
{
System.Console.WriteLine(exx.ToString());
}
}
与其直接解决你的问题,不如让我给你一些建议,告诉你如何自己解决这个问题。
很明显密钥不存在。第一件重要的事情是弄清楚问题到底出在哪一行。我看到两种可能性,"user_id" 和 "access_token"。不得包括这两项中的一项。两者都来自价值观。尝试打印出值,看看你能得到什么。特别是把钥匙打印出来,看看有没有打错。
看你的代码,我怀疑你的错误在第二个,var user_ID = values["user_id"];
首先,你似乎没有在任何地方使用它。其次,它与之前使用的 clientId
不同。事实上,我建议只使用 clientId
,如果这是你想要的,这将使你的请求值保持一致。
我正在尝试发回我的令牌以请求有关用户登录的更多信息。我想取回 json。
使用下面的代码我得到一个异常"The given key was not present in the dictionary."
我做错了什么?
public void Login(Action<Mobile.Google.Account> googleLoginCompleted, Action googleLoginFailed)
{
var auth = new OAuth2Authenticator(
clientId: "myid",
clientSecret: "secret",
scope: "https://www.googleapis.com/auth/plus.login",
authorizeUrl: new Uri("https://accounts.google.com/o/oauth2/v2/auth"),
redirectUrl: new Uri("http://adults.wicareerpathways.org"),
accessTokenUrl: new Uri("https://www.googleapis.com/oauth2/v4/token"));
auth.Completed += (sender, e) =>
{
if (e.IsAuthenticated)
{
var values = e.Account.Properties;
var access_token = values["access_token"];
var googleAccount = new Mobile.Google.Account
{
Username = e.Account.Username,
Properties = e.Account.Properties
};
try
{
var request = HttpWebRequest.Create(string.Format(@"https://www.googleapis.com/oauth2/v2/userinfo?access_token=" + access_token + "&format=json", ""));
request.ContentType = "application/json";
request.Method = "GET";
var user_ID = values["user_id"];
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
System.Console.Out.WriteLine("Stautus Code is: {0}", response.StatusCode);
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
var content = reader.ReadToEnd();
GoogleAccountDetails result = Newtonsoft.Json.JsonConvert.DeserializeObject<GoogleAccountDetails>(content);
googleAccount.Username = result.id;
if (googleLoginCompleted != null)
{
googleLoginCompleted(googleAccount);
}
}
}
}
catch (Exception exx)
{
System.Console.WriteLine(exx.ToString());
}
}
与其直接解决你的问题,不如让我给你一些建议,告诉你如何自己解决这个问题。
很明显密钥不存在。第一件重要的事情是弄清楚问题到底出在哪一行。我看到两种可能性,"user_id" 和 "access_token"。不得包括这两项中的一项。两者都来自价值观。尝试打印出值,看看你能得到什么。特别是把钥匙打印出来,看看有没有打错。
看你的代码,我怀疑你的错误在第二个,var user_ID = values["user_id"];
首先,你似乎没有在任何地方使用它。其次,它与之前使用的 clientId
不同。事实上,我建议只使用 clientId
,如果这是你想要的,这将使你的请求值保持一致。