我可以使用C#在浏览器中的Google驱动器中查看文件吗,我已经尝试过使用Google API。但总是要求输入登录信息。
如何使用C#在浏览器中查看文件?
我在这里尝试过
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 = "xxxxxxxxxxx@gmail.com";
//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);
}
}
拜托,有人对此有任何想法吗?
最佳答案
当您使用Permissions: create与用户共享文件时,可以选择向他们发送通知。这告诉他们文件已与他们共享。
现在,文件已与它们共享。File.get返回文件resource注意,请确保您添加fields = *,否则您将不会从file.get中获得很多元数据。
webViewLink一个链接,用于在相关的Google编辑器或浏览器的查看器中打开文件。
基本上,如果有问题的用户有权访问,那么您将获得此链接回到文件。像这样
https://docs.google.com/document/d/xWau_bNrntLBDDFjwUPBDkL3_oBW3hthavaVF8Ed1mbA
我还无法弄清楚如何在网站上创建一个可共享的链接,因此我认为也可以通过API来实现。目前,我认为该文件必须是公开的,或者用户必须具有访问权限,然后您才能获取Webview链接。