2016-12-19 12 views
2

GoogleドライブのファイルをブラウザでC#を使用して表示できますか?Google apiで何度も試してみました。常にログインの詳細を求めています。 C#を使用してブラウザでファイルを表示するにはどうすればよいですか?Cドライブを使用してGoogleドライブのファイルを閲覧できますか?

ここで私は、

public ActionResult GetDriveFile() 
    { 
     string filePath = System.Web.HttpContext.Current.Server.MapPath("~/client_secret.json"); 
     //string filePath = System.Web.HttpContext.Current.Server.MapPath("~/MyProject-e09c7c94100a.json"); 
     string userName = "[email protected]"; 

     //GoogleCredential credential; 
     //string[] Scopes = { Google.Apis.Drive.v2.DriveService.Scope.Drive }; 

     //using (var stream = new System.IO.FileStream(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read)) 
     //{ 
     // credential = GoogleCredential.FromStream(stream).CreateScoped(Scopes); 
     //} 

     //var driveService = new Google.Apis.Drive.v2.DriveService(new BaseClientService.Initializer() 
     //{ 
     // HttpClientInitializer = credential, 
     // ApplicationName = "Test", 
     //}); 


     var driveService = AuthenticateOauth(filePath, userName); 

     List<Google.Apis.Drive.v3.Data.File> allFiles = retrieveAllFiles(driveService); 

     string fileID = allFiles.Where(x => x.Name.Contains("sample.jpg")).Select(y => y.Id).FirstOrDefault(); 

     //DownloadFile(driveService); 

     string fileLink = GetFileLink(fileID); 

     return Redirect(fileLink); 
    } 



public static Google.Apis.Drive.v3.DriveService AuthenticateOauth(string clientSecretPath, string userName) 
    { 
     try 
     { 
      if (string.IsNullOrEmpty(userName)) 
       throw new Exception("userName is required."); 
      if (!System.IO.File.Exists(clientSecretPath)) 
       throw new Exception("clientSecretJson file does not exist."); 

      // These are the scopes of permissions you need. It is best to request only what you need and not all of them 
      string[] scopes = new string[] { Google.Apis.Drive.v3.DriveService.Scope.Drive };     // View and manage the files in your Google Drive   
      UserCredential credential; 
      using (var stream = new System.IO.FileStream(clientSecretPath, System.IO.FileMode.Open, System.IO.FileAccess.Read)) 
      { 
       string credPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal); 
       credPath = System.IO.Path.Combine(credPath, ".credentials/apiName"); 

       // Requesting Authentication or loading previously stored authentication for userName 
       credential = GoogleWebAuthorizationBroker.AuthorizeAsync(GoogleClientSecrets.Load(stream).Secrets, 
                     scopes, 
                     userName, 
                     CancellationToken.None, 
                     new Google.Apis.Util.Store.FileDataStore(credPath, true)).Result; 
      } 

      // Create Drive API service. 
      return new Google.Apis.Drive.v3.DriveService(new BaseClientService.Initializer() 
      { 
       HttpClientInitializer = credential, 
       ApplicationName = "Drive Authentication Sample", 
      }); 
     } 
     catch (Exception ex) 
     { 
      Console.WriteLine("Create Oauth2 DriveService failed" + ex.Message); 
      throw new Exception("CreateOauth2DriveFailed", ex); 
     } 
    } 

をしてください試してみましたが、誰もがこれについてどんな考えを持っていますか?

+0

*あなたはファイルにアクセスできますが、それ以外の世界ではアクセスできません。 Googleドライブのファイルにユーザーをリダイレクトする場合は、そのユーザーに明示的なアクセス権が必要です。つまり、公開するか、個別にアクセスする必要のある各ユーザーにアクセスを許可する必要があります。唯一のオプションは、ダウンロードしてサーバーから直接応答として返すことです。これにより、アプリケーションの認証だけが可能になりますが、アップ/ダウンの帯域幅をさらに増やすことになります。 –

答えて

0

Permissions: createを使用しているユーザーとファイルを共有するときに、通知を送信するオプションがあります。これは、ファイルがそれらと共有されていることを伝えます。

ファイルが共有されたので、File.getはファイルresourceを返します。fields = *を追加するか、file.getから多くのメタデータを取得しないように注意してください。

webViewLink
ブラウザで関連性の高いGoogleエディタまたはビューアでファイルを開くためのリンク。

基本的に、問題のユーザーがアクセス権を持っている場合は、このリンクをファイルに戻します。私が把握することができていない何この

https://docs.google.com/document/d/xWau_bNrntLBDDFjwUPBDkL3_oBW3hthavaVF8Ed1mbA

のようなものは、私はかかわらず、それを行う方法があるとし、ウェブサイト上ですることができます共有可能なリンクを作成する方法でありますAPIも同様です。現在、私はファイルが公開されていなければならないと思っています。

+0

更新ありがとうございました –

+0

上記のリンクのように、ブラウザに特定のファイルを表示するにはGoogleのアカウントにログインする必要があります(アクセス権がある場合)。 * WebViewLink *と* WebContentLink *、* WebViewLink *を正しく取得するためにアカウントやコードで行う必要があるその他の設定については、常に* null *値を取得しています。 –

+0

その場合は、ファイルyesにアクセスする必要があります。フィールドを取得していると仮定すると、なぜそれがnullになるのかはわかりません。ただし、Googleのドライブタイプのファイルではない場合を除きます。どのタイプのファイルにアクセスしようとしていますか? – DaImTo

関連する問題