Google Contact api v3 身份验证

发布于 2024-12-22 05:35:30 字数 580 浏览 6 评论 0原文

我尝试使用新的谷歌联系人 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)
 {
      ....
 }

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

I try use new google contacts API.
My task is simple - retrieve contacts from static(my personal) domain account.
I register my application at APIs Console and get ClientId,ClientSecret
So I try authenticate my application through .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)
 {
      ....
 }

This code works well, but Google said that we should use OAuth2 authentication at production scenario.
I try different parameters at RequestSettings but at other variant I get 401(access denied).
So my question what's the right way to auth through google API v3 at installed desctop application without using other accounts credentials.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧城空念 2024-12-29 05:35:30

在开始工作之前,您应该获得身份验证令牌。为此,您应该创建用户应该打开并允许访问您的应用程序的链接。
您应该使用稍后获得的代码请求令牌。
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...也许我错了,谁知道呢

Before start to work you should get auth token. For do this you should create link whitch user should open and grand access to your app.
Than you should request for token with the code witch you get later.
This mechanism described at https://developers.google.com/accounts/docs/OAuth2Login
something like this:

        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 it's a code what you should catch after user redirect from "access granting page"
But this method is api 2 i guess... Maybe I'm wrong, who know

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文