首页 文章

C#应用程序 - 阅读Google通讯录

提问于
浏览
2

由于Google停止了对旧版Auth的支持,现在我们必须使用oAuth 2,我们的简单桌面应用程序无法再从我的Google帐户中读取联系人 .

很好 - 我理解这一点,但是这个新的oAuth 2非常复杂......我不是从开发人员的角度来谈论 . 从我在线阅读 . 我们现在必须让我们的客户跳过众多的箍,以便我们的简单应用程序读取存储在他们的Google邮件/联系人中的联系人 .

我的iPhone似乎只能使用我一年前输入的典型电子邮件和密码来同步联系人 . 他们如何让它发挥作用?然而,通过我简单的桌面应用程序,客户端必须在谷歌开发者网站上搜索并使用API设置等 . 我是开发人员,我很困惑! - 你能想象我的客户会经历什么......它不能复杂化 .

有没有人可以给我简单的1,2,3来让C#桌面应用程序关闭并从特定的Gmail帐户获取联系人(只读)... least 数量的摆弄(对于Gmail帐户的所有者) .

我会完成应用程序中的所有艰苦工作 - 我只是不希望客户端花费一个小时来授权和创建API并在开发人员站点中点击(他/她不是开发人员) .

2 回答

  • 5

    你在这里遇到的主要问题是联系人是旧的Gdata API . 可以将Oauth2与Gdata库一起使用,但不是pretty . 就个人而言,我喜欢破解一些东西 . 我将Current .net客户端库与旧的Gdata客户端库一起使用 .

    Nuget New client library for authentication:

    不是100%肯定这是你需要的唯一一个让我知道如果它不起作用我们可以找到它 . 你基本上需要 Google.apis.auth.oauth2google apis.util.store .

    安装包Google.Apis.Auth

    Nuget old client library for contacts:

    安装包Google.GData.Contacts

    Code

    using Google.Apis.Auth.OAuth2;
    using Google.Apis.Util.Store;
    using Google.Contacts;
    using Google.GData.Client;
    using System;
    using System.Threading;
    
    public static void auth()
    {
    
        string clientId = "xxxxxx.apps.googleusercontent.com";
        string clientSecret = "xxxxx";
    
    
        string[] scopes = new string[] { "https://www.googleapis.com/auth/contacts.readonly" };     // view your basic profile info.
        try
        {
            // Use the current Google .net client library to get the Oauth2 stuff.
            UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets { ClientId = clientId, ClientSecret = clientSecret }
                                                                                         , scopes
                                                                                         , "test"
                                                                                         , CancellationToken.None
                                                                                         , new FileDataStore("test")).Result;
    
            // Translate the Oauth permissions to something the old client libray can read
            OAuth2Parameters parameters = new OAuth2Parameters();
            parameters.AccessToken = credential.Token.AccessToken;
            parameters.RefreshToken = credential.Token.RefreshToken;
            RunContactsSample(parameters);
            Console.ReadLine();
        }
        catch (Exception ex)
        {
    
            Console.ReadLine();
    
        }
        Console.ReadLine();
    
    
    }
    
    /// <summary> 
    /// Send authorized queries to a Request-based library 
    /// </summary> 
    /// <param name="service"></param> 
    private static void RunContactsSample(OAuth2Parameters parameters)
    {
        try
        {
            RequestSettings settings = new RequestSettings("Google contacts tutorial", parameters);
            ContactsRequest cr = new ContactsRequest(settings);
            Feed<Contact> f = cr.GetContacts();
            foreach (Contact c in f.Entries)
            {
                Console.WriteLine(c.Name.FullName);
            }
        }
        catch (Exception a)
        {
            Console.WriteLine("A Google Apps error occurred.");
            Console.WriteLine();
        }
    }
    

    可以找到教程here

    Google developers console

    访问google apis的所有应用程序必须在Google developers console上注册 . 访问Google的应用程序是运行代码的注册用户,不需要执行此步骤 . 您作为开发人员必须注册它 .

    从中您可以获得上面代码中使用的客户端ID和客户端密钥 .

  • 0

    我已经做到了这一点,但它有点模糊,就像你说的,很多摆弄 .

    我认为您可以在Google开发者控制台中注册并设置项目并生成服务帐户 . 然后,客户端需要以Google应用管理员身份登录HERE,并使用开发人员控制台生成的服务帐户名称和您需要访问的API范围填写clientID字段 .

    最后,我只是以客户端的身份登录到他们的管理面板并为他们设置了它 . 没有简单的方法没有客户也与谷歌应用程序转售商协助 . 我设法把它想象成一个有大量谷歌搜索的开发人员 .

相关问题