I am trying to work with Google drive from Windows application or service.
I don't need Web App, MVC, so on.
System must work automatically, without any user intrusion, so I created service account and get certificate and email.
And, of course, downloaded last client library - http://code.google.com/p/google-api-dotnet-client/
I build the service object as in documentation:
private const string SERVICE_ACCOUNT_EMAIL = "xxx@developer.gserviceaccount.com";
private const string SERVICE_ACCOUNT_PKCS12_FILE_PATH = @"xxx-privatekey.p12";
/// <summary>
/// Build a Drive service object authorized with the service account.
/// </summary>
/// <returns>Drive service object.</returns>
static DriveService BuildService ( )
{
X509Certificate2 certificate = new X509Certificate2 ( SERVICE_ACCOUNT_PKCS12_FILE_PATH, "notasecret",
X509KeyStorageFlags.Exportable );
var provider = new AssertionFlowClient ( GoogleAuthenticationServer.Description, certificate )
{
ServiceAccountId = SERVICE_ACCOUNT_EMAIL,
Scope = DriveService.Scopes.Drive.GetStringValue ( ),
ServiceAccountUser = "myemail@gmail.com",
};
var auth = new OAuth2Authenticator<AssertionFlowClient> ( provider, AssertionFlowClient.GetState );
return new DriveService ( auth );
} // BuildService
and then I do this:
service = BuildService ( );
FilesResource.InsertMediaUpload request = service.Files.Insert ( body, stream, mimeType );
request.Upload ( );
File file = request.ResponseBody;
I got the exception in Upload method - Unknown object identifier (OID). I found the command, which throws this exception:
String signature = UnpaddedUrlSafeBase64Encode ( Key.SignData ( Encoding.ASCII.GetBytes ( assertion.ToString ( ) ), "SHA256" ) );
This in an Google.Apis.Authentication.OAuth2.dll, file AssertionflowClient.cs, method GenerateMessage.
But Google certificate has only SHA1 algorithm. This is evident from the certificate info window: X509Certificate2UI.DisplayCertificate ( certificate ); or from windows.
I tried use SHA1: String signature = UnpaddedUrlSafeBase64Encode ( Key.SignData ( Encoding.ASCII.GetBytes ( assertion.ToString ( ) ), "SHA1" ) );
After that this exception disappeared, but I got HTTP Error 400 (Invalid request). How I can to work with Google drive with service account?