На данный момент у меня есть:
- Создал API Google Диска и служебный аккаунт.
- Создал общий диск Google и назначил свой служебный аккаунт «менеджером».
Делегирование всего домена Google
Это позволит успешно получить службу Google Drive, но я думаю, что проблема может быть в олицетворении/делегировании.
Код: Выделить всё
private DriveService GetDriveService()
{
// Load the Service Account JSON key file
string rootDirectory = Environment.CurrentDirectory;
string keyFilePath = rootDirectory + "\\mykey.json";
ServiceAccountCredential? credential;
using (var stream = new FileStream(keyFilePath, FileMode.Open, FileAccess.Read))
{
credential = GoogleCredential.FromStream(stream)
.CreateScoped(DriveService.Scope.Drive)
.CreateWithUser("me@domain.com") // Specifying the user to impersonate
.UnderlyingCredential as ServiceAccountCredential;
}
// Specify the scopes required
var scopes = new[] { DriveService.Scope.DriveFile };
// Create a delegated credential
if (credential == null) throw new ArgumentNullException(nameof(credential));
var delegatedCredential = new ServiceAccountCredential(new ServiceAccountCredential.Initializer(credential.Id)
{
Key = credential.Key,
Scopes = scopes
});
// Create the Drive service
var driveService = new DriveService(new BaseClientService.Initializer
{
HttpClientInitializer = delegatedCredential,
ApplicationName = "ServerTools"
});
return driveService;
}
Код: Выделить всё
public List GetFileList()
{
var request = _driveService.Files.List();
request.Fields = "nextPageToken, files(id, name)";
var files = request.Execute().Files;
return files.Select(x => x.Name).ToList();
}
Код: Выделить всё
private List? GetFolders()
{
FilesResource.ListRequest listRequest = _driveService.Files.List();
listRequest.Q = "mimeType='application/vnd.google-apps.folder'";
listRequest.Fields = "nextPageToken, files(id, name)";
// List files.
IList files = listRequest.Execute().Files;
return files.Select(x => x.Name).ToList();
}
Подробнее здесь: https://stackoverflow.com/questions/791 ... ce-account
Мобильная версия