【问题标题】:Google contact api v3 authentication谷歌联系人 api v3 身份验证
【发布时间】:2012-05-06 02:55:39
【问题描述】:

我尝试使用新的谷歌联系人 API。 我的任务很简单 - 从静态(我的个人)域帐户中检索联系人。 我在 API 控制台注册我的应用程序并获取 ClientId,ClientSecret 所以我尝试通过 .net(google SDK) 验证我的应用程序

 RequestSettings settings = new RequestSettings(appName,login,password);
 ContactsRequest cr = new ContactsRequest(settings);
 Feed<Contact> contacts = cr.GetContacts();
 foreach (Contact entry in contacts.Entries)
 {
      ....
 }

这段代码运行良好,但谷歌表示我们应该在生产场景中使用 OAuth2 身份验证。 我在RequestSettings 尝试不同的参数,但在其他变体中我得到 401(拒绝访问)。 所以我的问题是在安装的桌面应用程序中通过 google API v3 进行身份验证而不使用其他帐户凭据的正确方法是什么。

【问题讨论】:

  • ...Google 说我们应该使用 OAuth2,你应该在这行之后对你的请求进行身份验证 RequestSettings settings = new RequestSettings(appName); // 在此处添加授权令牌 ContactsRequest cr = new ContactsRequest(settings);但他们对如何做到这一点一无所知,无论如何我都希望找到使用 OAuth 2.0 的解决方案,您可以在此处阅读 developers.google.com/accounts/docs/OAuth2Login

标签: .net authentication oauth-2.0 google-contacts-api


【解决方案1】:

在开始工作之前,您应该获得授权令牌。为此,您应该创建链接,用户应该打开并访问您的应用程序。 比您应该使用稍后获得的代码女巫请求令牌。 这种机制在https://developers.google.com/accounts/docs/OAuth2Login 中描述 像这样:

        private const string GetTokenUrl = "https://accounts.google.com/o/oauth2/token";    
        private new bool Auth(bool needUserCredentionals = true)
        {
            var dic = new Dictionary<string, string>();
            dic.Add("grant_type", "authorization_code");
            dic.Add("code", ResponseCode);
            dic.Add("client_id", ApplicationId);
            dic.Add("client_secret", ApplicationSecret);
            dic.Add("redirect_uri", HttpUtility.UrlEncode(AppRedirectUrl));
            var str = String.Join("&", dic.Select(item => item.Key + "=" + item.Value).ToArray());
            var client = new WebClient();
            client.Headers.Add("Content-type", "application/x-www-form-urlencoded");
            string s;
            try { s = client.UploadString(GetTokenUrl, str); }
            catch (WebException) { return false; }
            catch (Exception) { return false; }
            AuthResponse response;
            try { response = JsonConvert.DeserializeObject<AuthResponse>(s); }
            catch (Exception) { return false; }
            Token = response.access_token;
            SessionTime = DateTime.Now.Ticks + response.expires_in;
            if (needUserCredentionals)
                if (!GetUserInfo()) return false;
            return true;
        }

        public class AuthResponse
        {
            public string access_token { get; set; }
            public string token_type { get; set; }
            public long expires_in { get; set; }
        }

ResponseCode 这是用户从“访问授权页面”重定向后应捕获的代码 但是这个方法我猜是api 2......也许我错了,谁知道

【讨论】:

  • 对不起 AuthResponse 是一个公共类 AuthResponse { public string access_token { get;放; } 公共字符串 token_type { 获取;放; } 公共字符串 expires_in { 获取;放; } }
  • 和 JsonConvert.DeserializeObject 它是 Newtonsoft.Json dll 的一部分,您可以从 code plex 获取它
  • GetTokenUrl_gApi.Token 是什么?
  • 我进行了重构,因此代码已更改。在以前的版本中 GetTokenUrl = 它是 google auth api 的终点 _gApi.Token = 这是你从 google 获得的一个令牌,这意味着你有发出 api 请求的密钥,总而言之,关于令牌你可以阅读一些关于 oAuth 规则的内容...
猜你喜欢
  • 1970-01-01
  • 2015-01-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-08
  • 1970-01-01
  • 2012-07-23
  • 2020-09-26
相关资源
最近更新 更多