我可以使用C#在浏览器中查看谷歌驱动器中的文件,我已经尝试过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);
}
}
拜托,有人对此有任何想法吗?
答案 0 :(得分:0)
当您与使用Permissions: create的用户共享文件时,可以选择向他们发送通知。这告诉他们该文件是与他们共享的。
现在文件与它们共享File.get返回一个文件resource注意确保你添加fields = *或者你不会从file.get获得大量的元数据。
webViewLink
用于在浏览器中的相关Google编辑器或查看器中打开文件的链接。
基本上,如果有问题的用户有权访问,那么您将获得此链接返回该文件。像这样的东西
https://docs.google.com/document/d/xWau_bNrntLBDDFjwUPBDkL3_oBW3hthavaVF8Ed1mbA
我无法弄清楚如何在网站上创建一个可共享的链接,所以我假设有一种方法可以通过API进行。目前我认为该文件必须是公共的,或者用户必须具有访问权限才能获得webview链接。