Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Sandor Apati committed Mar 21, 2023
0 parents commit 736ac05
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.idea
credentials.json
obj
bin
32 changes: 32 additions & 0 deletions GoogleSharedDrives.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<AssemblyName>GoogleSharedDrives</AssemblyName>
<RootNamespace>GoogleSharedDrives</RootNamespace>
<PackageId>GoogleSharedDrives</PackageId>
<Authors>GoogleSharedDrives</Authors>
<Company>GoogleSharedDrives</Company>
<Product>GoogleSharedDrives</Product>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Google.Apis.Admin.Directory.directory_v1" Version="1.60.0.2964" />
<PackageReference Include="Google.Apis.Drive.v3" Version="1.60.0.2986" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.1" />
<PackageReference Include="ObjectDumper.NET" Version="3.5.6" />
</ItemGroup>

<ItemGroup>
<None Include="appsettings*.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="credentials.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
86 changes: 86 additions & 0 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
using Google.Apis.Auth.OAuth2;
using Google.Apis.Admin.Directory.directory_v1;
using Google.Apis.Admin.Directory.directory_v1.Data;
using Google.Apis.Drive.v3;
using Google.Apis.Drive.v3.Data;
using Google.Apis.Services;
using Newtonsoft.Json;

namespace GoogleSharedDrives
{
class Program
{
static readonly string ApplicationName = "GoogleSharedDrives";

static async Task Main(string[] args)
{
var settings = Settings.LoadSettings();

ServiceAccountCredential credential;
// Load client secrets.
using (var stream =
new FileStream("credentials.json", FileMode.Open, FileAccess.Read))
{
credential = ServiceAccountCredential.FromServiceAccountData(stream);
}

var cred = new ServiceAccountCredential(new ServiceAccountCredential.Initializer(settings.ServiceAccountId!)
{
User = settings.AdminEmail!,
Scopes = new[] { DriveService.ScopeConstants.Drive },
Key = credential.Key
});

var service = new DriveService(new BaseClientService.Initializer
{
HttpClientInitializer = cred,
ApplicationName = ApplicationName
});

string? pageToken = null;

do
{
var request = service.Drives.List();
request.UseDomainAdminAccess = true;
request.Fields = "nextPageToken, drives(id, name)";
request.PageSize = 100;
if (pageToken is not null)
{
request.PageToken = pageToken;
}
var result = await request.ExecuteAsync();
foreach (var drive in result.Drives)
{
// Console.WriteLine("## {0} ({1})", drive.Name, drive.Id);

var req = service.Permissions.List(drive.Id);
req.SupportsAllDrives = true;
req.UseDomainAdminAccess = true;
req.Fields = "*";
req.PageSize = 100;


var found = false;

var res = await req.ExecuteAsync();
foreach (var perm in res.Permissions)
{
if (perm.Type == "user" && perm.Role == "organizer")
{
Console.WriteLine("{0}\t{1}\t{2}", drive.Name, drive.Id, perm.EmailAddress);
found = true;
}
}

if (!found)
{
Console.WriteLine("{0}\t{1}\t{2}", drive.Name, drive.Id, "-");
}
}

pageToken = result.NextPageToken;
} while (pageToken != null);
}
}
}
28 changes: 28 additions & 0 deletions Settings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Microsoft.Extensions.Configuration;

namespace GoogleSharedDrives
{
public class Settings
{
public string? ServiceAccountId { get; set; }
public string? AdminEmail { get; set; }
public string? CustomerId { get; set; }
public string? Domain { get; set; }

public static Settings LoadSettings()
{
// Load settings
IConfiguration config = new ConfigurationBuilder()
// appsettings.json is required
.AddJsonFile("appsettings.json", optional: false)
// appsettings.Development.json" is optional, values override appsettings.json
.AddJsonFile($"appsettings.Development.json", optional: true)
// User secrets are optional, values override both JSON files
.AddUserSecrets<Program>()
.Build();

return config.GetRequiredSection("Settings").Get<Settings>() ??
throw new Exception("Could not load app settings. See README for configuration instructions.");
}
}
}
15 changes: 15 additions & 0 deletions appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"Logging": {
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
},
"Settings": {
"ServiceAccountId": "",
"AdminEmail": "",
"CustomerId": "",
"Domain": ""
}
}

0 comments on commit 736ac05

Please sign in to comment.