-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Sandor Apati
committed
Mar 21, 2023
0 parents
commit 736ac05
Showing
5 changed files
with
165 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.idea | ||
credentials.json | ||
obj | ||
bin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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."); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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": "" | ||
} | ||
} |