C# 应用程序 - 读取 Google 个联系人

C# application - Read Google Contacts

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

很好 - 我明白这一点,但是这个新的 oAuth 2 非常复杂......而且我不是从开发人员的角度谈论的。从我在网上阅读的内容。我们现在必须让我们的客户跳过许多障碍,以便我们的简单应用程序能够读取存储在他们 Google mail/Contacts.

中的联系人

我的 iPhone 似乎能够仅使用我大约一年前输入的典型电子邮件和密码就可以很好地同步联系人。他们如何让它发挥作用?然而,对于我的简单桌面应用程序,客户端必须在 Google 开发人员站点和 API 设置等中翻找。我是一名开发人员,我很困惑! - 你能想象我的客户将要经历什么吗...不可能这么复杂。

有谁能给我简单的 1、2、3 来启动 C# 桌面应用程序并从特定的 Gmail 帐户获取联系人(只读)...使用 最少 次摆弄(对于 Gmail 帐户的所有者)。

我会完成应用程序中的所有艰苦工作 - 我只是不想让客户不得不花一个小时来授权和创建 API 并在开发者网站上四处点击 (he/she 不是开发人员)。

我已经做到了,但是就像你说的那样有点模糊,有很多摆弄。

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

最后我只是作为客户端登录到他们的管理面板并为他们设置。如果客户不聘请 google 应用程序经销商提供协助,则没有简单的方法。作为一名开发人员,我通过大量谷歌搜索设法解决了这个问题。

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

Nuget 用于身份验证的新客户端库:

不是 100% 确定这是您唯一需要的,如果它不起作用,请告诉我,我们可以找到它。你基本上需要 Google.apis.auth.oauth2google apis.util.store.

Install-Package Google.Apis.Auth

联系人的 Nuget 旧客户端库:

Install-Package Google.GData.Contacts

代码

 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 开发者控制台

所有访问 google API 的应用程序都必须在 Google developers console 上注册。是注册用户访问Google的应用程序运行代码不需要做这一步。你作为开发者必须注册它。

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