Reputation: 2229
I'm trying to connect my server written in C# to my Google Drive and therefore I use Google Service Account, I configured everything from Google API Console and have the following code
static void Main(string[] args)
{
string[] Scopes = { DriveService.Scope.Drive };
var certificate = new X509Certificate2("client_secret.p12", "notasecret", X509KeyStorageFlags.Exportable);
try
{
ServiceAccountCredential credential = new ServiceAccountCredential(
new ServiceAccountCredential.Initializer("[email protected]")
{
Scopes = Scopes
}.FromCertificate(certificate));
DriveService service = new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "Salmon Player Update"
});
FilesResource.ListRequest listRequest = service.Files.List();
listRequest.PageSize = 10;
listRequest.Fields = "nextPageToken, files(id, name)";
// List files.
IList<Google.Apis.Drive.v3.Data.File> files = listRequest.Execute()
.Files;
Console.WriteLine("Files:");
if (files != null && files.Count > 0)
{
foreach (var file in files)
{
Console.WriteLine("{0} ({1})", file.Name, file.Id);
}
}
else
{
Console.WriteLine("No files found.");
}
Console.Read();
} catch(Exception ex)
{
Console.WriteLine(ex.InnerException);
}
}
But whatever I do I'm getting only "Getting Started" file which does not exist there anymore. I have a lot of different files which I cannot access to. Any ideas what could be wrong here?
Upvotes: 1
Views: 427
Reputation: 506
A service account is its own user, so it will have its own Drive account space. If you want to access files from your personal account, you will have to either a) share those files with your service account's email address or b) use an OAuth flow and authenticate with your own user rather than the robot service account.
Upvotes: 3