· 5 years ago · Jan 23, 2020, 09:50 PM
1public struct SampleUser
2{
3 public string Username { get; set; }
4 public string Password { get; set; }
5 public string Subdomain { get; set; }
6 public string ControlPlane { get; set; }
7}
8class Program
9{
10 static void Main(string[] args)
11 {
12 //Console.WriteLine("Hello World!");
13 var user = new SampleUser
14 {
15 ControlPlane = "sharefile.com",
16 Username = "user",
17 Password = "pass!",
18 Subdomain = "sd"
19 };
20
21 string oauthClientId = "secret";
22 string oauthClientSecret = "secret";
23
24 if (string.IsNullOrEmpty(oauthClientId) || string.IsNullOrEmpty(oauthClientSecret))
25 {
26 Console.WriteLine("You must provide Client ID and Client Secret");
27 return;
28 }
29
30 if (string.IsNullOrEmpty(user.Username) || string.IsNullOrEmpty(user.Password) || string.IsNullOrEmpty(user.Subdomain))
31 {
32 Console.WriteLine("You must provide a username, password and sub-domain");
33 return;
34 }
35
36 RunSample(user, oauthClientId, oauthClientSecret).Wait();
37 Console.ReadLine();
38 }
39 public static async Task RunSample(SampleUser user, string clientID, string clientSecret)
40 {
41 var sfClient = await PasswordAuthencation(user, clientID, clientSecret);
42
43 await StartSession(sfClient);
44
45 var defaultUserFolder = await LoadFolderAndChildren(sfClient);
46
47 var allSharedAlias = sfClient.Items.GetAlias("allshared");
48
49 var sharedFolders = sfClient.Items.GetChildren(allSharedAlias).Execute();
50
51 foreach (var folder in sharedFolders.Feed)
52 {
53 if (folder.Name.ToString() == "Art Files")
54 {
55 var subs = sfClient.Items.GetAlias(folder.Name);
56 try
57 {
58 var ecomSubs = sfClient.Items.GetChildren(subs).Execute();
59 }
60 catch (Exception ex)
61 {
62 Console.WriteLine(ex.ToString());
63 }
64 }
65 }
66 //Console.WriteLine(allSharedAlias.ToString());
67
68 }
69 public static async Task<Folder> LoadFolderAndChildren(ShareFileClient sfClient)
70 {
71 var folder = (Folder)await sfClient.Items.Get().Expand("Children").ExecuteAsync();
72 return folder;
73 }
74 public static async Task StartSession(ShareFileClient sfClient)
75 {
76 var session = await sfClient.Sessions.Login().Expand("Principal").ExecuteAsync();
77 Console.WriteLine("Authenicated As " + session.Principal.Email);
78 }
79 public static async Task<ShareFileClient> PasswordAuthencation(SampleUser user, string clientID, string clientSecret)
80 {
81 var configuration = Configuration.Default();
82 configuration.Logger = new DefaultLoggingProvider();
83 var sfClient = new ShareFileClient("https://secure.sf-api.com/sf/v3/");
84 var oauthService = new OAuthService(sfClient, clientID, clientSecret);
85
86 var oauthToken = await oauthService.PasswordGrantAsync(user.Username, user.Password, user.Subdomain, user.ControlPlane);
87
88 sfClient.AddOAuthCredentials(oauthToken);
89 sfClient.BaseUri = oauthToken.GetUri();
90
91 return sfClient;
92 }
93}