我可以使用 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
A link for opening the file in a relevant Google editor or viewer in a browser.
基本上,如果相关用户具有访问权限,那么您将获得该文件的链接。像这样的东西
https://docs.google.com/document/d/xWau_bNrntLBDDFjwUPBDkL3_oBW3hthavaVF8Ed1mbA
我一直无法弄清楚的是如何在网站上创建一个可共享的链接,所以我认为也有一种方法可以通过 API 来实现。目前,我认为该文件必须是公开的,或者在您获得 WebView 链接之前用户必须具有访问权限。
关于c# - 我可以使用 C# 在浏览器中查看谷歌驱动器中的文件吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41226517/