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

Con*_*Wet 5 c# gdata google-contacts-api google-oauth

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

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

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

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

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

DaI*_*mTo 7

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

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

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

安装包Google.Apis.Auth

Nuget联系人的旧客户端库:

安装包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();
    }
}
Run Code Online (Sandbox Code Playgroud)

教程可以在这里找到

谷歌开发者安慰

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

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

  • 成功!!多谢您的协助,我们现在开始运作。令人困惑的部分是,通常您登录Google是因为您刚刚设置了API内容-因此,测试代码当然不是在要求凭据。提示...请先设置API,获取客户端ID等,然后退出GOOGLE ...否则将引起混乱,因为您将获得oAuth同意页面(直接进入或不选择帐户),因此您将获得访问令牌,因此只能获得自己的联系人。 (2认同)