1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
|
using System;
using System.Collections.Generic;
using Google.Contacts;
using Google.GData.Contacts;
using Google.GData.Client;
using Google.GData.Extensions;
namespace ProfilesDemoConsoleApplication {
//////////////////////////////////////////////////////////////////////
/// <summary>hold batch processing results
/// </summary>
//////////////////////////////////////////////////////////////////////
public class BatchResult {
public int Success { get; set; }
public int Error { get; set; }
public List<Contact> ErrorEntries { get; set; }
}
//////////////////////////////////////////////////////////////////////
/// <summary>used to unshare domain users contact information
/// </summary>
//////////////////////////////////////////////////////////////////////
public class ProfilesManager {
private String domain;
private ContactsRequest cr;
private List<Contact> profiles;
/// <summary>
/// constructs a new ProfilesManager and authenticate using 2-Legged OAuth
/// </summary>
/// <param name="consumerKey">Domain's consumer key</param>
/// <param name="consumerSecret">Domain's consumer secret</param>
/// <param name="adminEmail">Domain administrator's email</param>
public ProfilesManager(String consumerKey, String consumerSecret, String adminEmail) {
String admin = adminEmail.Substring(0, adminEmail.IndexOf('@'));
this.domain = adminEmail.Substring(adminEmail.IndexOf('@') + 1);
RequestSettings settings =
new RequestSettings("GoogleInc-UnshareProfilesSample-1", consumerKey,
consumerSecret, admin, this.domain);
settings.AutoPaging = true;
this.cr = new ContactsRequest(settings);
this.BatchSize = 100;
}
/// <summary>
/// get or set the batch processing size
/// </summary>
/// <returns></returns>
public int BatchSize { get; set; }
/// <summary>
/// returns the list of profiles for the domain
/// </summary>
/// <returns></returns>
public List<Contact> Profiles {
get {
if (this.profiles == null) {
this.GetAllProfiles();
}
return this.profiles;
}
}
/// <summary>
/// retrieve all profiles for the domain
/// </summary>
public void GetAllProfiles() {
ContactsQuery query =
new ContactsQuery("https://www.google.com/m8/feeds/profiles/domain/" + this.domain + "/full");
Feed<Contact> f = cr.Get<Contact>(query);
this.profiles = new List<Contact>(f.Entries);
}
/// <summary>
/// Unshare all profiles for the domain
/// </summary>
public BatchResult UnshareProfiles() {
BatchResult result = new BatchResult() {
ErrorEntries = new List<Contact>(),
};
int index = 0;
if (this.profiles == null) {
this.GetAllProfiles();
}
while (index < this.Profiles.Count) {
List<Contact> requestFeed = new List<Contact>();
for (int i = 0; i < this.BatchSize && index < this.Profiles.Count; ++i, ++index) {
Contact entry = this.Profiles[index];
entry.ContactEntry.Status = new Status(false);
entry.BatchData = new GDataBatchEntryData(GDataBatchOperationType.update);
requestFeed.Add(entry);
}
Feed<Contact> responseFeed =
cr.Batch(requestFeed,
new Uri("https://www.google.com/m8/feeds/profiles/domain/" + this.domain + "/full/batch"),
GDataBatchOperationType.Default);
// Check the status of each operation.
foreach (Contact entry in responseFeed.Entries) {
if (entry.BatchData.Status.Code == 200) {
++result.Success;
} else {
++result.Error;
result.ErrorEntries.Add(entry);
}
}
}
return result;
}
/// <summary>
/// Runs the methods above to demonstrate usage of the .NET
/// client library.
/// </summary>
static void Main(string[] args) {
if (args.Length != 3) {
Console.WriteLine("Usage: unshare_profiles <consumerKey> <consumerSecret> <adminEmail>");
} else {
String consumerKey = args[0];
String consumerSecret = args[1];
String adminEmail = args[2];
ProfilesManager manager = new ProfilesManager(consumerKey, consumerSecret, adminEmail);
BatchResult result = manager.UnshareProfiles();
Console.WriteLine("Success: " + result.Success + " - Error: " + result.Error);
foreach (Contact entry in result.ErrorEntries) {
Console.WriteLine(" > Failed to update " + entry.Id +
": (" + entry.BatchData.Status.Code + ") "
+ entry.BatchData.Status.Reason);
}
}
}
}
}
|