Google.GData.Contacts向Gapps添加联系人

时间:2016-02-25 09:35:09

标签: c# oauth-2.0 google-contacts

我正在尝试为Gapps联系人添加联系人。我试过下面的代码。它成功添加了与Google帐户的联系,没有任何错误。但它没有显示帐户中添加的联系人。这是我添加联系人的代码

`

                 credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                new ClientSecrets
                {   
                    ClientId = clientId,
                    ClientSecret = clientSecret
                },
                Scopes = new string[] { "http://www.google.com/m8/feeds/" },

                "user",
                CancellationToken.None,
                new FileDataStore(credPath, true)).Result;

            var token = credential.Token.AccessToken;


            string RedirectURI = "urn:ietf:wg:oauth:2.0:oob";
            OAuth2Parameters parameters = new OAuth2Parameters
            {
                ClientId = clientId,
                ClientSecret = clientSecret,
                // Note: AccessToken is valid only for 60 minutes
                AccessToken = token,
                RefreshToken = credential.Token.RefreshToken,
                RedirectUri = RedirectURI
            };

            RequestSettings settings = new RequestSettings(
                "AppName", parameters);
            ContactsRequest cr = new ContactsRequest(settings);


                Contact newContact = new Contact();

                newContact.Name = new Name()
                {
                    FullName = "Elizabeth Bennet",
                    GivenName = "Elizabeth",
                    FamilyName = "Bennet",
                };
                newContact.Content = "Notes";
                // Set the contact's e-mail addresses.
                newContact.Emails.Add(new EMail()
                {
                    Primary = true,
                    Rel = ContactsRelationships.IsHome,
                    Address = "something@gmail.com"
                });
                newContact.Emails.Add(new EMail()
                {
                    Rel = ContactsRelationships.IsWork,
                    Address = "something@gmail.com"
                });

                newContact.Location = "Pune";
                Uri feedUri = new Uri(ContactsQuery.CreateContactsUri("default"));


                Contact createdContact = cr.Insert(feedUri, newContact); // Is Successfull 

            }`

当我获取联系人时,它会在结果中添加联系人。请为此问题提出解决方案。

string str = createdContact.Id;

                Feed<Contact> f = cr.GetContacts();
                foreach (Contact c in f.Entries)
                {
                    Console.WriteLine(c.Name.FullName);
                }

感谢; 雷努卡。

1 个答案:

答案 0 :(得分:0)

您可以通过发送GET request来检索所有联系人,但在此之前,您需要使用OAuth 2.0请求访问权限。您的应用程序需要范围信息。 请注意您使用Google Contacts API发送的每个请求,指定版本号,使用GData-Version HTTP标头。 Google Contacts API的版本 1 2 已正式已弃用。我们建议您使用版本3. API的版本3 为联系人姓名和结构化邮政地址字段引入新的结构化数据格式 有关联系人条目的更多详细信息,请点击以下链接:https://developers.google.com/google-apps/contacts/v3/#contact_entry