[英]Can i view a file in google drive in browser using C#
Can i view a file in google drive in browser using C#, i have tried somethings with google api. 我可以使用C#在浏览器中的Google驱动器中查看文件吗,我已经尝试过使用Google API。 but always it's asking for login details. 但总是要求输入登录信息。 How can i view file in browser using c#? 如何使用C#在浏览器中查看文件?
here i have tried, 我在这里尝试过
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);
}
}
Please, anyone have any idea regarding this? 拜托,有人对此有任何想法吗?
When you share a file with a user using Permissions: create there is an option to send them a notification. 当您使用权限与用户共享文件时:创建时 ,可以选择向他们发送通知。 This tells them that the file is shared with them. 这告诉他们文件已与他们共享。
Now that the file is shared with them File.get returns a file resource note make sure you add fields= * or you wont get much of the meta data back from file.get. 现在,文件已与它们共享,File.get将返回文件资源注释,请确保您添加fields = *,否则您将不会从file.get中获得很多元数据。
webViewLink webViewLink
A link for opening the file in a relevant Google editor or viewer in a browser. 用于在相关的Google编辑器或浏览器中打开文件的链接。
Basically if the user in question has access then you will get this link back to the file. 基本上,如果有问题的用户有权访问,那么您将获得此链接回到文件。 Something like this 像这样
https://docs.google.com/document/d/xWau_bNrntLBDDFjwUPBDkL3_oBW3hthavaVF8Ed1mbA https://docs.google.com/document/d/xWau_bNrntLBDDFjwUPBDkL3_oBW3hthavaVF8Ed1mbA
What I haven't been able to figure out is how to create a sharable link you can on the website so I assume there is a way of doing it though the API as well. 我还无法弄清楚如何在网站上创建一个可共享的链接,因此我认为也可以通过API来实现。 Currently I think the file has to either be public or the user must have access before you get a webview link back. 目前,我认为该文件必须是公开的,或者用户必须具有访问权限,然后您才能获取Webview链接。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.