0

Googleドライブにファイルをアップロードしようとしています。このためには、ドメイン全体の権限を有効にしたサービスアカウントがあります。 "@xyx.com"は私のドメインです。私は一般的な "[email protected]" Googleドライブを持っています。ユーザーのメールアドレスをGoogleサービスアカウントに偽装する

Googleサービスアカウントは「[email protected]」です。私はファイルを "[email protected]"にアップロードする必要があります。私は "[email protected]"をサービスアカウントに偽装しようとしました。

以下は私のコード

public static DriveService AuthenticateServiceAccount(string serviceAccountEmail, string keyFilePath) 
 
     { 
 
      // check the file exists 
 
      if (!File.Exists(keyFilePath)) 
 
      { 
 
       return null; 
 
      } 
 

 
      //Google Drive scopes Documentation: https://developers.google.com/drive/web/scopes 
 
      string[] scopes = new string[] { DriveService.Scope.Drive, // view and manage your files and documents 
 
              DriveService.Scope.DriveAppdata, // view and manage its own configuration data 
 
              DriveService.Scope.DriveFile, // view and manage files created by this app 
 
              DriveService.Scope.DriveMetadata, 
 
              DriveService.Scope.DriveMetadataReadonly, // view metadata for files 
 
              DriveService.Scope.DrivePhotosReadonly, 
 
              DriveService.Scope.DriveReadonly, // view files and documents on your drive 
 
              DriveService.Scope.DriveScripts }; // modify your app scripts  
 

 

 
      var certificate = new X509Certificate2(keyFilePath, "notasecret", X509KeyStorageFlags.Exportable); 
 
      try 
 
      { 
 
       ServiceAccountCredential credential = new ServiceAccountCredential(
 
        new ServiceAccountCredential.Initializer(serviceAccountEmail) 
 
        { 
 
         Scopes = scopes, 
 
         User = "[email protected]", 
 
        }.FromCertificate(certificate)); 
 
       DriveService service = new DriveService(new BaseClientService.Initializer() 
 
       { 
 
        HttpClientInitializer = credential, 
 
        ApplicationName = "CIM_GD_UPLOAD", 
 
       }); 
 
       return service; 
 
      } 
 
      catch (Exception ex) 
 
      { 
 
       throw ex; 
 
      } 
 
     }

私は、次のエラーを取得しています。

Error:"unauthorized_client", Description:"Client is unauthorized to retrieve access tokens using this method.", Uri:"" 

私はGoogleのAPI v3のを使用しています。

サービスアカウントにユーザーアカウントを偽装することは可能ですか? Googleドライブからファイルをアップロード/取得する正しい方法を教えてください。

答えて

0

Google Drive API Authorizationを参照するには、Google APIにアクセスできるようにOAuth 2.0でリクエストを承認する必要があります。認可プロセスの第一歩は、Google API ConsoleからOAuth 2.0資格情報を取得することです。同じプロセスがサービスアカウントを使用すると、delegate domain-wide authority to the service accountというサービスアカウントの資格情報を生成する必要があります。

あなたはこのdocumentationで述べたように試してみたいことがあります:

サービスアカウントにドメイン全体のアクセス権を委任したとユーザーアカウントを偽装したい場合は、ユーザーアカウントの電子メールアドレスを指定setServiceAccountUserGoogleCredentialの方法を使用してください。たとえば:

GoogleCredential credential = new GoogleCredential.Builder() 
    .setTransport(httpTransport) 
    .setJsonFactory(JSON_FACTORY) 
    .setServiceAccountId(emailAddress) 
    .setServiceAccountPrivateKeyFromP12File(new File("MyProject.p12")) 
    .setServiceAccountScopes(Collections.singleton(SQLAdminScopes.SQLSERVICE_ADMIN)) 
    .setServiceAccountUser("[email protected]") 
    .build(); 

は、アプリケーションでのGoogle APIを呼び出すために GoogleCredentialオブジェクトを使用してください。

関連する問題