【问题标题】:Create domain shared contact in Gsuite using Contacts API in C#使用 C# 中的联系人 API 在 Gsuite 中创建域共享联系人
【发布时间】:2019-06-04 00:38:56
【问题描述】:

我正在尝试通过 C# 中的联系人 API 在 GSuite 中创建域共享联系人,但无法弄清楚如何将 atom XML 条目发布到此处提到的 Feed URL:https://developers.google.com/admin-sdk/domain-shared-contacts/#Creating

我已尝试遵循此处提到的旧 GData 方式https://developers.google.com/gdata/client-cs,但我收到“执行身份验证请求返回意外结果:404”错误。

static void Main(string[] args)
        {

            Console.WriteLine("Hello !! ");
            //Get Auth
            OAuth2Parameters p = ContactsAuth();

            //Create a domain shared contact
            try
            {
                RequestSettings settings = new RequestSettings("GSuiteAdminApp", p);
                ContactsRequest cr = new ContactsRequest(settings);

                ContactEntry cn = new ContactEntry();

                Name n = new Name();
                n.GivenName = "Ice";
                n.FamilyName = "Cold001";
                n.FullName = "Ice Cold001";

                EMail e = new EMail();
                e.Rel = "http://schemas.google.com/g/2005#work";
                e.Primary = true;
                e.Address = "ice.cold001@xyz.com";

                cn.Name = n;
                cn.Emails.Add(e);

            }
            catch (Exception e44)
            {
                Console.WriteLine(e44.Message);
            }
        }




//Auth for Contacts API
        public static OAuth2Parameters ContactsAuth()
        {
            string clientId = "xxxxxxxxxxxxxx.apps.googleusercontent.com";
            string clientSecret = "xxxxxxxxxxxxx";

            string[] scopes = new string[] { "https://www.google.com/m8/feeds/" };

            try
            {
                UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets
                {
                    ClientId = clientId,
                    ClientSecret = clientSecret
                }, scopes, "super-admin@mydomain.com", CancellationToken.None, new FileDataStore("C:\\Temp\\A\\SharedContactsOauth")).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;
                return parameters;
            }
            catch (Exception ex33)
            {
                Console.WriteLine(ex33.Message);
                return null;
            }
        }

这会给出“请求失败”错误。

【问题讨论】:

  • 您是否考虑过使用 People api。它应该为您提供相同的访问权限,并且您不必使用不再受支持的 gdata 库。
  • 感谢琳达调查。我检查了 People API,但没有任何地方提到使用 API 创建“域共享联系人”。

标签: c# google-api google-contacts-api


【解决方案1】:

我终于能够通过串接来自几个不同来源的代码 sn-ps 和我自己的一些修改来解决这个问题。 Linda Lawton 的 https://www.daimto.com/google-contacts-with-c/ 用于使用旧 GData API 的 OAuth2 部分。 Google 关于 Contacts API v3.0 https://developers.google.com/contacts/v3/ 的文档,介绍了使用 .NET 客户端库处理联系人的机制,以及他们关于“域共享联系人”的(有点粗略的)文档,尤其是关于为新联系人使用正确的 FeedUri 和 Atom 条目@987654323 @。

基本上归结为 - 使用 GSuite 超级管理员帐户使用 OAuth2.0 授权联系人 API,然后使用 GData Contacts .NET 客户端库创建新联系人 通过在方法中提供您的 Gsuite 域,您就完成了。

这是我现在为我工作的完整代码:

using System;
using System.Threading;
using Google.Contacts;
using Google.GData.Contacts;
using Google.GData.Client;
using Google.GData.Extensions;
using Google.Apis.Auth;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Util.Store;

namespace SharedContactsAPI
{
    class Program
    {
        static void Main(string[] args)
        {

            Console.WriteLine("Hello !! ");

            //Get Auth
            OAuth2Parameters p = ContactsAuth();

            ////Create a domain shared contact
            try
            {
                RequestSettings settings = new RequestSettings("GSuiteAdminApp", p);
                ContactsRequest contactreq = new ContactsRequest(settings);

                Console.WriteLine("Attempting to create a Domain Shared Contact in GSuite");
                Console.WriteLine(" ");
                CreateContact(contactreq);

            }
            catch (Exception e44)
            {
                Console.WriteLine(e44.Message);
            }
        }

        //Create Shared Contact
        public static Contact CreateContacttest(ContactsRequest cr)
        {
            Contact newEntry = new Contact();
            // Set the contact's name.
            newEntry.Name = new Name()
            {
                FullName = "Ice Cold005",
                GivenName = "Ice",
                FamilyName = "Cold005"
            };
            newEntry.Content = "Notes";
            // Set the contact's e-mail addresses.
            newEntry.Emails.Add(new EMail()
            {
                Primary = true,
                Rel = ContactsRelationships.IsWork,
                Address = "ice.cold005@xyz.com"
            });

            //Insert the contact
            Uri feedUri = new Uri(ContactsQuery.CreateContactsUri("test.com"));
            Contact createdEntry = cr.Insert(feedUri, newEntry);
            Console.WriteLine("New Contact created successfully with ContactID = " + createdEntry.Id);
            return createdEntry;
        }

        //Auth for Contacts API
        public static OAuth2Parameters ContactsAuthtest()
        {
            string clientId = "xxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com";
            string clientSecret = "xxxxxxxxxxxxxxxxxxxxxxxxxx";

            string[] scopes = new string[] { "https://www.google.com/m8/feeds/contacts/" };

            try
            {
                UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets
                {
                    ClientId = clientId,
                    ClientSecret = clientSecret
                }, scopes, "super-admin@test.com", CancellationToken.None, new FileDataStore("C:\\Temp\\A\\SharedContactsOauth")).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;
                return parameters;
            }
            catch (Exception ex33)
            {
                Console.WriteLine(ex33.Message);
                return null;
            }
        }

    }
}

【讨论】:

    猜你喜欢
    • 2013-06-10
    • 1970-01-01
    • 1970-01-01
    • 2021-12-08
    • 2012-07-07
    • 1970-01-01
    • 1970-01-01
    • 2015-10-09
    • 2017-07-03
    相关资源
    最近更新 更多