简体   繁体   English

如何使用Google Oauth2获取用户json数据

[英]How to get User json data with google Oauth2

I'm attempting to send back my token to request further information about the user logging in. I want to get json back. 我正在尝试发回令牌,以请求有关用户登录的更多信息。我想找回json。

using the code below I get an exception "The given key was not present in the dictionary." 使用下面的代码,我得到一个异常“字典中不存在给定的键”。

What am I doing wrong? 我究竟做错了什么?

 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());
                }
            }

Rather than directly solving your problem, let me give you some advice on how to solve this yourself. 除了直接解决您的问题外,让我给您一些有关如何自己解决问题的建议。

It seems obvious that a key isn't present. 似乎显然不存在密钥。 The first important thing is to figure out which line exactly the problem is. 首先重要的是弄清楚问题出在哪一行。 I see two possibilities, "user_id" and "access_token". 我看到两种可能,“ user_id”和“ access_token”。 One of those two items must not be included. 不得包含这两项之一。 Both come from values. 两者都来自价值。 Try printing out values, and see what you come up with. 尝试打印出值,然后看看结果如何。 In particular, print the keys, and see if you have a typo. 特别是,打印键,看看是否有错字。

Looking at your code, I suspect your error is in the second, var user_ID = values["user_id"]; 查看您的代码,我怀疑您的错误是第二个错误, var user_ID = values["user_id"]; First of all, you don't seem to use it anywhere. 首先,您似乎没有在任何地方使用它。 Secondly, it is different than the clientId used earlier. 其次,它与之前使用的clientId不同。 In fact, I would suggest just using clientId , if that is what you intend, which will give you consistency in your requesting values. 实际上,我建议仅使用clientId ,如果您打算这样做,则可以使您的请求值保持一致。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM