forked from robertj/GDataCmdLet
-
Notifications
You must be signed in to change notification settings - Fork 1
/
GDataDgc.cs
2167 lines (1746 loc) · 87.5 KB
/
GDataDgc.cs
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Diagnostics;
using System.Management.Automation;
using System.ComponentModel;
using System.Collections.Generic;
using Google.Contacts;
using Google.GData.Contacts;
using Google.GData.Client;
using Google.GData.Extensions;
using Google.GData.Apps;
using Google.GData.Extensions.Apps;
using Google.GData.Calendar;
using Google.GData.Apps.GoogleMailSettings;
using System.Net;
using System.Text;
using System.Linq;
using System.IO;
using System.Web;
using System.Xml;
using System.Xml.Linq;
using Google.AccessControl;
using Google.GData.AccessControl;
using System.Security;
using System.Runtime.InteropServices;
namespace Microsoft.PowerShell.GData
{
public class Dgc
{
#region XmlReturn
public class XmlReturn
{
public string Name { get; set; }
public string Value { get; set; }
}
#endregion XmlReturn
#region ParseXML
public class ParseXML
{
public IEnumerable<XmlReturn> ListFormat;
public ParseXML(string XMLString)
{
XElement Elem = XElement.Parse(XMLString);
XNamespace Ns = "http://schemas.google.com/apps/2006";
ListFormat = from c in Elem.Elements(Ns + "property")
select new XmlReturn
{
Name = c.Attribute("name").Value.ToString(),
Value = c.Attribute("value").Value.ToString()
};
}
}
#endregion ParseXML
#region ConvertToUnsecureString
public class ConvertToUnsecureString
{
public string PlainString;
public ConvertToUnsecureString(SecureString securePassword)
{
if (securePassword == null)
{
throw new ArgumentNullException("securePassword");
}
IntPtr _unmanagedString = IntPtr.Zero;
_unmanagedString = Marshal.SecureStringToGlobalAllocUnicode(securePassword);
PlainString = Marshal.PtrToStringUni(_unmanagedString);
}
}
#endregion ConvertToUnsecureString
#region GoogleAppService
public class GoogleAppService
{
#region GetDomain
public string GetDomain(string adminUser)
{
char[] _delimiterChars = { '@' };
string[] _temp = adminUser.Split(_delimiterChars);
var _domain = _temp[1];
return _domain;
}
#endregion GetDomain
#region GetDomainFromAppService
public string GetDomainFromAppService(AppsService userService)
{
string _domain = userService.Domain;
return _domain;
}
#endregion GetDomainFromAppService
#region CreateGroupEntrys
public GDataTypes.GDataGroupEntrys CreateGroupEntrys(AppsExtendedFeed groupEntrys)
{
var _gDataGroupEntrys = new GDataTypes.GDataGroupEntrys();
foreach (AppsExtendedEntry _groupEntry in groupEntrys.Entries)
{
var _gDataGroupEntry = new GDataTypes.GDataGroupEntry();
_gDataGroupEntry.SelfUri = _groupEntry.SelfUri.ToString();
foreach (var _setting in _groupEntry.Properties)
{
if (_setting.Name == "groupId")
{
_gDataGroupEntry.GroupId = _setting.Value;
}
else if (_setting.Name == "groupName")
{
_gDataGroupEntry.GroupName = _setting.Value;
}
else if (_setting.Name == "description")
{
_gDataGroupEntry.Description = _setting.Value;
}
else if (_setting.Name == "emailPermission")
{
_gDataGroupEntry.EmailPermission = _setting.Value;
}
}
_gDataGroupEntrys.Add(_gDataGroupEntry);
}
return _gDataGroupEntrys;
}
#endregion CreateGroupEntrys
#region CreateGroupEntry
public GDataTypes.GDataGroupEntry CreateGroupEntry(AppsExtendedEntry groupEntry)
{
var _gDataGroupEntry = new GDataTypes.GDataGroupEntry();
_gDataGroupEntry.SelfUri = groupEntry.SelfUri.ToString();
foreach (var _setting in groupEntry.Properties)
{
if (_setting.Name == "groupId")
{
_gDataGroupEntry.GroupId = _setting.Value;
}
else if (_setting.Name == "groupName")
{
_gDataGroupEntry.GroupName = _setting.Value;
}
else if (_setting.Name == "description")
{
_gDataGroupEntry.Description = _setting.Value;
}
else if (_setting.Name == "emailPermission")
{
_gDataGroupEntry.EmailPermission = _setting.Value;
}
}
return _gDataGroupEntry;
}
#endregion CreateGroupEntry
#region CreateGroupMemberEntrys
public GDataTypes.GDataGroupMemberEntrys CreateGroupMemberEntrys(AppsExtendedFeed groupMemberEntrys)
{
var _gDataGroupMemberEntrys = new GDataTypes.GDataGroupMemberEntrys();
foreach (AppsExtendedEntry _groupMemberEntry in groupMemberEntrys.Entries)
{
var _gDataGroupMemberEntry = new GDataTypes.GDataGroupMemberEntry();
foreach (var _setting in _groupMemberEntry.Properties)
{
if (_setting.Name == "memberId")
{
_gDataGroupMemberEntry.MemberId = _setting.Value;
}
else if (_setting.Name == "memberType")
{
_gDataGroupMemberEntry.MemberType = _setting.Value;
}
else if (_setting.Name == "directMember")
{
_gDataGroupMemberEntry.DirectMember = _setting.Value;
}
}
_gDataGroupMemberEntrys.Add(_gDataGroupMemberEntry);
}
return _gDataGroupMemberEntrys;
}
#endregion CreateGroupMemberEntrys
#region CreateGroupMemberEntry
public GDataTypes.GDataGroupMemberEntry CreateGroupMemberEntry(AppsExtendedEntry groupMemberEntry)
{
var _gDataGroupMemberEntry = new GDataTypes.GDataGroupMemberEntry();
foreach (var _setting in groupMemberEntry.Properties)
{
if (_setting.Name == "memberId")
{
_gDataGroupMemberEntry.MemberId = _setting.Value;
}
else if (_setting.Name == "memberType")
{
_gDataGroupMemberEntry.MemberType = _setting.Value;
}
else if (_setting.Name == "directMember")
{
_gDataGroupMemberEntry.DirectMember = _setting.Value;
}
}
return _gDataGroupMemberEntry;
}
#endregion CreateGroupMemberEntry
#region CreateGroupOwnerEntrys
public GDataTypes.GDataGroupOwnerEntrys CreateGroupOwnerEntrys(AppsExtendedFeed groupOwnerEntrys)
{
var _gDataGroupOwnerEntrys = new GDataTypes.GDataGroupOwnerEntrys();
foreach (AppsExtendedEntry _groupOwnerEntry in groupOwnerEntrys.Entries)
{
var _gDataGroupOwnerEntry = new GDataTypes.GDataGroupOwnerEntry();
foreach (var _setting in _groupOwnerEntry.Properties)
{
if (_setting.Name == "email")
{
_gDataGroupOwnerEntry.MemberId = _setting.Value;
}
else if (_setting.Name == "type")
{
_gDataGroupOwnerEntry.MemberType = _setting.Value;
}
}
_gDataGroupOwnerEntrys.Add(_gDataGroupOwnerEntry);
}
return _gDataGroupOwnerEntrys;
}
#endregion CreateGroupOwnerEntrys
#region CreateGroupOwnerEntry
public GDataTypes.GDataGroupOwnerEntry CreateGroupOwnerEntry(AppsExtendedEntry groupOwnerEntry)
{
var _gDataGroupOwnerEntry = new GDataTypes.GDataGroupOwnerEntry();
foreach (var _setting in groupOwnerEntry.Properties)
{
if (_setting.Name == "email")
{
_gDataGroupOwnerEntry.MemberId = _setting.Value;
}
else if (_setting.Name == "type")
{
_gDataGroupOwnerEntry.MemberType = _setting.Value;
}
}
return _gDataGroupOwnerEntry;
}
#endregion CreateGroupOwnerEntry
#region CreateSenderAddressEntry
public GDataTypes.GDataSenderAddress CreateSenderAddressEntry(GoogleMailSettingsEntry mailSettingsEntry)
{
var _gDataMailSettingsEntry = new GDataTypes.GDataSenderAddress();
foreach (var _setting in mailSettingsEntry.Properties)
{
if(_setting.Name == "address")
{
_gDataMailSettingsEntry.Address = _setting.Value;
}
else if (_setting.Name == "replyTo")
{
_gDataMailSettingsEntry.ReplyTo = _setting.Value;
}
else if (_setting.Name == "name")
{
_gDataMailSettingsEntry.Name = _setting.Value;
}
else if (_setting.Name == "makeDefault")
{
_gDataMailSettingsEntry.Default = _setting.Value;
}
}
return _gDataMailSettingsEntry;
}
#endregion CreateSenderAddressEntry
#region CreateCreateIMapEntry
public GDataTypes.GDataImap CreateIMapEntry(GoogleMailSettingsEntry mailSettingsEntry)
{
var _gDataImapEntry = new GDataTypes.GDataImap();
foreach (var _setting in mailSettingsEntry.Properties)
{
if (_setting.Name == "enable")
{
_gDataImapEntry.Enabled = _setting.Value;
}
}
return _gDataImapEntry;
}
#endregion CreateCreateIMapEntry
#region CreateCreatePop3Entry
public GDataTypes.GDataPop3 CreatePop3Entry(GoogleMailSettingsEntry mailSettingsEntry)
{
var _pop3Entry = new GDataTypes.GDataPop3();
foreach (var _setting in mailSettingsEntry.Properties)
{
if (_setting.Name == "enable")
{
_pop3Entry.Enabled = _setting.Value;
}
else if (_setting.Name == "action")
{
_pop3Entry.Action = _setting.Value;
}
else if (_setting.Name == "enableFor")
{
_pop3Entry.EnabledFor = _setting.Value;
}
}
return _pop3Entry;
}
#endregion CreateCreatePop3Entry
#region AppendUserAliasEntry
public GDataTypes.GDataUserAliasEntry AppendUserAliasEntry(string _Xml, GDataTypes.GDataUserAliasEntry userAliasEntry)
{
var _paresdXml = new GDataTypes.ParseXML(_Xml);
var _userSingelAliasEntry = new GDataTypes.GDataAliasEntry();
foreach (var _entry in _paresdXml.ListFormat)
{
var _aliasEntry = new GDataTypes.GDataAliasEntry();
foreach (var _attribute in _entry.at)
{
if (_attribute.Value == "aliasEmail" || _attribute.Value == "userEmail")
{
if (_attribute.Value == "aliasEmail")
{
_userSingelAliasEntry.aliasEmail = _attribute.NextAttribute.Value;
}
if (_attribute.Value == "userEmail")
{
_userSingelAliasEntry.UserName = _attribute.NextAttribute.Value;
}
if (_userSingelAliasEntry.UserName != null && _userSingelAliasEntry.aliasEmail != null)
{
userAliasEntry.Add(_userSingelAliasEntry);
}
}
}
foreach (var _subEntry in _entry.sub)
{
foreach (var _attribute in _subEntry.at)
{
if (_attribute.Value == "aliasEmail" || _attribute.Value == "userEmail")
{
if (_attribute.Value == "aliasEmail")
{
_aliasEntry.aliasEmail = _attribute.NextAttribute.Value;
}
if (_attribute.Value == "userEmail")
{
_aliasEntry.UserName = _attribute.NextAttribute.Value;
}
if (_aliasEntry.UserName != null && _aliasEntry.aliasEmail != null)
{
userAliasEntry.Add(_aliasEntry);
}
}
}
}
}
return userAliasEntry;
}
#endregion AppendUserAliasEntry
#region CreateUserAliasEntry
public GDataTypes.GDataUserAliasEntry CreateUserAliasEntry(string xml)
{
var _paresdXml = new GDataTypes.ParseXML(xml);
var _userAliasEntry = new GDataTypes.GDataUserAliasEntry();
var _userSingelAliasEntry = new GDataTypes.GDataAliasEntry();
foreach (var _entry in _paresdXml.ListFormat)
{
var _aliasEntry = new GDataTypes.GDataAliasEntry();
foreach (var _attribute in _entry.at)
{
if (_attribute.Value == "aliasEmail" || _attribute.Value == "userEmail")
{
if (_attribute.Value == "aliasEmail")
{
_userSingelAliasEntry.aliasEmail = _attribute.NextAttribute.Value;
}
if (_attribute.Value == "userEmail")
{
_userSingelAliasEntry.UserName = _attribute.NextAttribute.Value;
}
if (_userSingelAliasEntry.UserName != null && _userSingelAliasEntry.aliasEmail != null)
{
_userAliasEntry.Add(_userSingelAliasEntry);
}
}
}
foreach (var _subEntry in _entry.sub)
{
foreach (var _attribute in _subEntry.at)
{
if (_attribute.Value == "aliasEmail" || _attribute.Value == "userEmail")
{
if (_attribute.Value == "aliasEmail")
{
_aliasEntry.aliasEmail = _attribute.NextAttribute.Value;
}
if (_attribute.Value == "userEmail")
{
_aliasEntry.UserName = _attribute.NextAttribute.Value;
}
if (_aliasEntry.UserName != null && _aliasEntry.aliasEmail != null)
{
_userAliasEntry.Add(_aliasEntry);
}
}
}
}
}
return _userAliasEntry;
}
#endregion CreateAliasEntry
#region CreateUserEntry
public GDataTypes.GDataUserEntry CreateUserEntry(UserEntry userEntry)
{
var _gDataUserEntry = new GDataTypes.GDataUserEntry();
_gDataUserEntry.UserName = userEntry.Login.UserName;
_gDataUserEntry.Admin = userEntry.Login.Admin;
_gDataUserEntry.susspended = userEntry.Login.Suspended;
_gDataUserEntry.AgreedToTerms = userEntry.Login.AgreedToTerms;
_gDataUserEntry.ChangePasswordAtNextLogin = userEntry.Login.ChangePasswordAtNextLogin;
_gDataUserEntry.GivenName = userEntry.Name.GivenName;
_gDataUserEntry.FamilyName = userEntry.Name.FamilyName;
_gDataUserEntry.Limit = userEntry.Quota.Limit;
_gDataUserEntry.SelfUri = userEntry.SelfUri.ToString();
return _gDataUserEntry;
}
#endregion CreateUserEntry
#region CreateUserEntrys
public GDataTypes.GDataUserEntrys CreateUserEntrys(UserFeed _userFeed)
{
var _gDataUserEntrys = new GDataTypes.GDataUserEntrys();
foreach (UserEntry _userEntry in _userFeed.Entries)
{
var _gDataUserEntry = new GDataTypes.GDataUserEntry();
_gDataUserEntry.UserName = _userEntry.Login.UserName;
_gDataUserEntry.Admin = _userEntry.Login.Admin;
_gDataUserEntry.susspended = _userEntry.Login.Suspended;
_gDataUserEntry.AgreedToTerms = _userEntry.Login.AgreedToTerms;
_gDataUserEntry.ChangePasswordAtNextLogin = _userEntry.Login.ChangePasswordAtNextLogin;
_gDataUserEntry.GivenName = _userEntry.Name.GivenName;
_gDataUserEntry.FamilyName = _userEntry.Name.FamilyName;
_gDataUserEntry.Limit = _userEntry.Quota.Limit;
_gDataUserEntry.SelfUri = _userEntry.SelfUri.ToString();
_gDataUserEntrys.Add(_gDataUserEntry);
}
return _gDataUserEntrys;
}
#endregion CreateUserEntrys
#region GetCustomerId
public string GetCustomerId(AppsService CustIdService)
{
var Token = CustIdService.Groups.QueryClientLoginToken();
var uri = new Uri("https://apps-apis.google.com/a/feeds/customer/2.0/customerId");
WebRequest WebRequest = WebRequest.Create(uri);
WebRequest.Method = "GET";
WebRequest.ContentType = "application/atom+xml; charset=UTF-8";
WebRequest.Headers.Add("Authorization: GoogleLogin auth=" + Token);
WebResponse WebResponse = WebRequest.GetResponse();
if (WebResponse == null)
{
throw new Exception("WebResponse is null");
}
StreamReader SR = new StreamReader(WebResponse.GetResponseStream());
var _Result = SR.ReadToEnd().Trim();
var Xml = new ParseXML(_Result);
foreach (var x in Xml.ListFormat)
{
if (x.Name == "customerId")
{
CustomerId = x.Value;
}
}
return CustomerId;
}
private string CustomerId;
#endregion
#region CreateUserAlias
public string CreateUserAlias(string ID, AppsService userService, string userAlias)
{
var _domain = userService.Domain.ToString();
char[] _delimiterChars = { '@' };
string[] _temp = userAlias.Split(_delimiterChars);
var _aliasDomain = _temp[1];
var _token = userService.Groups.QueryClientLoginToken();
var uri = new Uri("https://apps-apis.google.com/a/feeds/alias/2.0/" + _aliasDomain);
WebRequest _webRequest = WebRequest.Create(uri);
_webRequest.Method = "POST";
_webRequest.ContentType = "application/atom+xml; charset=UTF-8";
_webRequest.Headers.Add("Authorization: GoogleLogin auth=" + _token);
string _post = "<atom:entry xmlns:atom='http://www.w3.org/2005/Atom' xmlns:apps='http://schemas.google.com/apps/2006'><apps:property name=\"aliasEmail\" value=\"" + userAlias + "\" /><apps:property name=\"userEmail\" value=\"" + ID + "@" + _domain + "\" /></atom:entry>";
byte[] _bytes = Encoding.UTF8.GetBytes(_post);
Stream _OS = null;
_webRequest.ContentLength = _bytes.Length;
_OS = _webRequest.GetRequestStream();
_OS.Write(_bytes, 0, _bytes.Length);
WebResponse _webResponse = _webRequest.GetResponse();
if (_webResponse == null)
{
throw new Exception("WebResponse is null");
}
StreamReader SR = new StreamReader(_webResponse.GetResponseStream());
var _result = SR.ReadToEnd().Trim();
return _result;
}
#endregion CreateUserAlias
#region CreateOrganizationUnit
public string CreateOrganizationUnit(AppsService OrgUnitService, string OrgUnitName, string OrgUnitDescription, string OrgUnitParentOrgUnitPath, bool OrgUnitBlockInheritance)
{
return null;
}
#endregion CreateOrganizationUnit
#region RetriveNextPage
private string nextPage;
public string RetriveNextPage(string xml)
{
var _parseXml = new GDataTypes.ParseXML(xml.ToString());
nextPage = "";
foreach (var _elements in _parseXml.ListFormat)
{
foreach (var _attribute in _elements.at)
{
if (_attribute.Value == "next")
{
nextPage = _attribute.NextAttribute.NextAttribute.Value;
}
}
}
return nextPage;
}
#endregion RetriveNextPage
#region RemoveUserAlias
public string RemoveUserAlias(AppsService userService, string userAlias)
{
var _domain = userService.Domain.ToString();
char[] _delimiterChars = { '@' };
string[] _temp = userAlias.Split(_delimiterChars);
var _aliasDomain = _temp[1];
var _token = userService.Groups.QueryClientLoginToken();
var _uri = new Uri("https://apps-apis.google.com/a/feeds/alias/2.0/" + _domain + "/" + userAlias);
WebRequest _webRequest = WebRequest.Create(_uri);
_webRequest.Method = "DELETE";
_webRequest.ContentType = "application/atom+xml; charset=UTF-8";
_webRequest.Headers.Add("Authorization: GoogleLogin auth=" + _token);
string _post = "";
byte[] _bytes = Encoding.UTF8.GetBytes(_post);
Stream _OS = null;
_webRequest.ContentLength = _bytes.Length;
_OS = _webRequest.GetRequestStream();
_OS.Write(_bytes, 0, _bytes.Length);
WebResponse _webResponse = _webRequest.GetResponse();
if (_webResponse == null)
{
throw new Exception("WebResponse is null");
}
StreamReader _SR = new StreamReader(_webResponse.GetResponseStream());
var _result = _SR.ReadToEnd().Trim();
return _result;
}
#endregion RemoveUserAlias
#region RetriveUserAlias
public string RetriveUserAlias(string id, AppsService userService)
{
var _domain = userService.Domain.ToString();
char[] _delimiterChars = { '@' };
var _token = userService.Groups.QueryClientLoginToken();
var _uri = new Uri("https://apps-apis.google.com/a/feeds/alias/2.0/" + _domain + "?userEmail=" + id + "@" + _domain);
WebRequest _webRequest = WebRequest.Create(_uri);
_webRequest.Method = "GET";
_webRequest.ContentType = "application/atom+xml; charset=UTF-8";
_webRequest.Headers.Add("Authorization: GoogleLogin auth=" + _token);
WebResponse _webResponse = _webRequest.GetResponse();
if (_webResponse == null)
{
throw new Exception("WebResponse is null");
}
StreamReader _SR = new StreamReader(_webResponse.GetResponseStream());
var _result = _SR.ReadToEnd().Trim();
return _result;
}
#endregion RetriveUserAlias
#region RetriveAllUserAlias
public string RetriveAllUserAlias(AppsService userService, string nextPage)
{
var _domain = userService.Domain.ToString();
char[] _delimiterChars = { '@' };
var _token = userService.Groups.QueryClientLoginToken();
if (nextPage == "")
{
nextPage = "https://apps-apis.google.com/a/feeds/alias/2.0/" + _domain + "?start=alias@" + _domain;
}
var _uri = new Uri(nextPage);
WebRequest _webRequest = WebRequest.Create(_uri);
_webRequest.Method = "GET";
_webRequest.ContentType = "application/atom+xml; charset=UTF-8";
_webRequest.Headers.Add("Authorization: GoogleLogin auth=" + _token);
WebResponse _webResponse = _webRequest.GetResponse();
if (_webResponse == null)
{
throw new Exception("WebResponse is null");
}
StreamReader _SR = new StreamReader(_webResponse.GetResponseStream());
var _result = _SR.ReadToEnd().Trim();
return _result;
}
#endregion RetriveAllUserAlias
#region RetrieveAllOUs
public string RetrieveAllOUs(AppsService UserService)
{
var _token = UserService.Groups.QueryClientLoginToken();
var OUService = new Dgc.GoogleAppService();
var _custId = OUService.GetCustomerId(UserService);
var uri = new Uri("https://apps-apis.google.com/a/feeds/orgunit/2.0/" + _custId + "?get=all");
WebRequest WebRequest = WebRequest.Create(uri);
WebRequest.Method = "GET";
WebRequest.ContentType = "application/atom+xml; charset=UTF-8";
WebRequest.Headers.Add("Authorization: GoogleLogin auth=" + _token);
WebResponse WebResponse = WebRequest.GetResponse();
if (WebResponse == null)
{
throw new Exception("WebResponse is null");
}
StreamReader SR = new StreamReader(WebResponse.GetResponseStream());
var _Result = SR.ReadToEnd().Trim();
return _Result;
}
#endregion RetrieveAllOUs
#region RetrieveOU
public string RetrieveOU(AppsService UserService, string OuPath)
{
var Token = UserService.Groups.QueryClientLoginToken();
var OUService = new Dgc.GoogleAppService();
var CustId = OUService.GetCustomerId(UserService);
var uri = new Uri("https://apps-apis.google.com/a/feeds/orgunit/2.0/" + CustId + "/"+OuPath);
WebRequest WebRequest = WebRequest.Create(uri);
WebRequest.Method = "GET";
WebRequest.ContentType = "application/atom+xml; charset=UTF-8";
WebRequest.Headers.Add("Authorization: GoogleLogin auth=" + Token);
WebResponse WebResponse = WebRequest.GetResponse();
if (WebResponse == null)
{
throw new Exception("WebResponse is null");
}
StreamReader SR = new StreamReader(WebResponse.GetResponseStream());
var _Result = SR.ReadToEnd().Trim();
return _Result;
}
#endregion RetrievOU
}
#endregion GoogleAppService
#region GoogleContactsService
public class GoogleContactsService
{
#region GetDomain
public string GetDomain(ContactsService ContactService)
{
char[] delimiterChars = { '@' };
string[] temp = ContactService.Credentials.Username.ToString().Split(delimiterChars);
var Domain = temp[1];
return Domain;
}
#endregion GetDomain
#region SetContactTitle
private string editUri;
private string oldTitle;
public string SetContactTitle(string token, string selfUri, string title)
{
var _xml = GetContact(token, selfUri);
var _paredXml = new GDataTypes.ParseXML(_xml);
if(_xml.Contains("<title type='text'></title>"))
{
}
foreach (var _entry in _paredXml.ListFormat)
{
if (_entry.name == "{http://www.w3.org/2005/Atom}link")
{
foreach (var _attribute in _entry.at)
{
if (_attribute.Value == "edit")
{
editUri = _attribute.NextAttribute.NextAttribute.Value;
}
}
}
if (_entry.name == "{http://www.w3.org/2005/Atom}title")
{
if (_entry.value != null)
{
oldTitle = "<title type='text'>" + _entry.value + "</title>";
}
else
{
oldTitle = "<title type='text'></title>";
}
}
}
var _uri = new Uri(editUri);
var _newXml = _xml.Replace(oldTitle, "<title type='text'>" + title + "</title>");
WebRequest _webRequest = WebRequest.Create(_uri);
_webRequest.ContentType = "application/atom+xml; charset=UTF-8";
_webRequest.Method = "PUT";
_webRequest.Headers.Add("Authorization: GoogleLogin auth=" + token);
byte[] _bytes = Encoding.UTF8.GetBytes(_newXml);
Stream _OS = null;
_webRequest.ContentLength = _bytes.Length;
_OS = _webRequest.GetRequestStream();
_OS.Write(_bytes, 0, _bytes.Length);
_OS.Close();
WebResponse _webResponse = _webRequest.GetResponse();
if (_webResponse == null)
{
throw new Exception("WebResponse is null");
}
StreamReader _SR = new StreamReader(_webResponse.GetResponseStream());
var _Result = _SR.ReadToEnd().Trim();
return _Result;
}
#endregion SetContact
#region GetContact
public string GetContact(string token, string selfUri)
{
var _uri = new Uri(selfUri);
WebRequest _webRequest = WebRequest.Create(_uri);
_webRequest.ContentType = "application/atom+xml; charset=UTF-8";
_webRequest.Method = "Get";
_webRequest.Headers.Add("Authorization: GoogleLogin auth=" + token);
WebResponse _webResponse = _webRequest.GetResponse();
if (_webResponse == null)
{
throw new Exception("WebResponse is null");
}
StreamReader _SR = new StreamReader(_webResponse.GetResponseStream());
var _Result = _SR.ReadToEnd().Trim();
return _Result;
}
#endregion GetContact
#region CreateContactEntry
public GDataTypes.GDataContactEntry CreateContactEntry(ContactEntry contactEntry)
{
var _gDataContactEntry = new GDataTypes.GDataContactEntry();
_gDataContactEntry.Name = contactEntry.Title.Text;
foreach (var _emailEntry in contactEntry.Emails)
{
if (_emailEntry.Work == true)
{
_gDataContactEntry.Email = _emailEntry.Address;
}
}
foreach (var _phoneNumberEntry in contactEntry.Phonenumbers)
{
if (_phoneNumberEntry.Rel == ContactsRelationships.IsWork)
{
_gDataContactEntry.PhoneNumber = _phoneNumberEntry.Value;
}
if (_phoneNumberEntry.Rel == ContactsRelationships.IsHome)
{
_gDataContactEntry.HomePhoneNumber = _phoneNumberEntry.Value;
}
if (_phoneNumberEntry.Rel == ContactsRelationships.IsMobile)
{
_gDataContactEntry.MobilePhoneNumber = _phoneNumberEntry.Value;
}
if (_phoneNumberEntry.Rel == ContactsRelationships.IsOther)
{
_gDataContactEntry.OtherPhoneNumber = _phoneNumberEntry.Value;
}
}
foreach (var _postaAddressEntry in contactEntry.PostalAddresses)
{
if (_postaAddressEntry.Rel == ContactsRelationships.IsWork)
{
_gDataContactEntry.PostalAddress = _postaAddressEntry.FormattedAddress;
}
if (_postaAddressEntry.Rel == ContactsRelationships.IsHome)
{
_gDataContactEntry.HomeAddress = _postaAddressEntry.FormattedAddress;
}
}
_gDataContactEntry.SelfUri = contactEntry.SelfUri.ToString();
return _gDataContactEntry;
}
#endregion CreateContactEntry
#region CreateContactModifidEntry
public GDataTypes.GDataContactEntry CreateContactModifidEntry(ContactEntry contactEntry, string name)
{
var _gDataContactEntry = new GDataTypes.GDataContactEntry();
_gDataContactEntry.Name = name;
foreach (var _emailEntry in contactEntry.Emails)
{
if (_emailEntry.Work == true)
{
_gDataContactEntry.Email = _emailEntry.Address;
}
}
foreach (var _phoneNumberEntry in contactEntry.Phonenumbers)
{
if (_phoneNumberEntry.Rel == ContactsRelationships.IsWork)
{
_gDataContactEntry.PhoneNumber = _phoneNumberEntry.Value;
}
if (_phoneNumberEntry.Rel == ContactsRelationships.IsHome)
{
_gDataContactEntry.HomePhoneNumber = _phoneNumberEntry.Value;
}
if (_phoneNumberEntry.Rel == ContactsRelationships.IsMobile)
{
_gDataContactEntry.MobilePhoneNumber = _phoneNumberEntry.Value;
}
if (_phoneNumberEntry.Rel == ContactsRelationships.IsOther)
{
_gDataContactEntry.OtherPhoneNumber = _phoneNumberEntry.Value;
}
}
foreach (var _postaAddressEntry in contactEntry.PostalAddresses)
{
if (_postaAddressEntry.Rel == ContactsRelationships.IsWork)
{
_gDataContactEntry.PostalAddress = _postaAddressEntry.FormattedAddress;
}
if (_postaAddressEntry.Rel == ContactsRelationships.IsHome)
{
_gDataContactEntry.HomeAddress = _postaAddressEntry.FormattedAddress;
}
}
_gDataContactEntry.SelfUri = contactEntry.SelfUri.ToString();
return _gDataContactEntry;
}
#endregion CreateContactModifidEntry