5

別のユーザーに代わってサービス アカウントを使用して DriveService を作成しようとしました。

ここにあるGoogleドキュメントからこのコードをコピーしましたhttps://developers.google.com/drive/delegation

    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(),
        };
        var auth = new OAuth2Authenticator<AssertionFlowClient>(provider, AssertionFlowClient.GetState);
        return new DriveService(auth);
    }

しかし、プロジェクトをビルドしようとすると、次の警告が表示されます。

警告 4 'Google.Apis.Authentication.OAuth2.DotNetOpenAuth.AssertionFlowClient' は廃止されました: 'AssertionFlowClient はサポートされなくなり、1.7.0-beta で削除される予定です。新しい Google.Api.Auth NuGet パッケージの ServiceAccountCredential の使用を検討してください。

また、次のエラーが表示されます。

エラー 11 引数 1: 'Google.Apis.Authentication.OAuth2.OAuth2Authenticator' から 'Google.Apis.Services.BaseClientService.Initializer' に変換できません

次に、 ServiceAccountCredential をグーグルで検索し、最終的にこのコードになりました(このページから派生: https://code.google.com/p/google-api-dotnet-client/wiki/OAuth2#Service_Accounts )

static DriveService BuildService() {
            X509Certificate2 certificate = new X509Certificate2(SERVICE_ACCOUNT_PKCS12_FILE_PATH, "notasecret", X509KeyStorageFlags.Exportable);

        ServiceAccountCredential credential = new ServiceAccountCredential(
            new ServiceAccountCredential.Initializer(SERVICE_ACCOUNT_EMAIL)
            {
                User = "someone@mydomain.mygbiz.com", 
                Scopes = new[] { DriveService.Scope.DriveFile }
            }.FromCertificate(certificate));

        var service = new DriveService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName = "Drive API Sample",
        });

        return service;
    }

このコードをビルドしようとすると問題ないように見えますが、実行すると次のエラーが発生します。

タイプ 'System.Security.Cryptography.CryptographicException' の初回例外が mscorlib.dll で発生しました

追加情報: Det går inte att hitta det begärda objektet. (翻訳: 要求されたオブジェクトが見つかりません)

この例外のハンドラがあれば、プログラムは安全に続行できます。

エラーは次の行で発生します。

X509Certificate2 certificate = new X509Certificate2(SERVICE_ACCOUNT_PKCS12_FILE_PATH, "notasecret", X509KeyStorageFlags.Exportable);

誰にもアイデアはありますか?

2013年10月31日更新私はこのコードを試しました:

{
        Console.WriteLine("Drive API - Service Account");
        Console.WriteLine("==========================");

        String serviceAccountEmail = "<some email>@developer.gserviceaccount.com";

        var certificate = new X509Certificate2(@"key.p12", "notasecret", X509KeyStorageFlags.Exportable);

        ServiceAccountCredential credential = new ServiceAccountCredential(
           new ServiceAccountCredential.Initializer(serviceAccountEmail)
           {
               User = "<someuser>@<mydomain>.mygbiz.com",
               Scopes = new[] { DriveService.Scope.Drive }
           }.FromCertificate(certificate));

        // Create the service.
        var service = new DriveService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName = "DrvMgr",
        });

        Console.WriteLine("Executing listing");
        FileList UserFiles = service.Files.List().Execute();

次のエラー メッセージが表示されます。

タイプ 'Google.Apis.Auth.OAuth2.Responses.TokenResponseException' の未処理の例外が Google.Apis.dll で発生しました

追加情報: エラー:"access_denied"、説明:""、Uri:""

4

1 に答える 1