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);
}
}
는, 사람이 이에 대해 어떤 생각을하세요?
* 사용자는 파일에 액세스 할 수 있지만 나머지 국가는 액세스 할 수 없습니다. Google 드라이브의 파일로 사용자를 리디렉션하면 액세스 할 수있는 명시 적 권한이 필요합니다. 즉, 공개로 설정하거나 개별적으로 액세스해야하는 각 사용자에게 액세스 권한을 부여해야합니다. 유일한 다른 옵션은 다운로드하여 서버에서 직접 응답으로 반환하는 것입니다. 이렇게하면 응용 프로그램에 대해서만 권한을 부여 할 수 있지만 추가 및 위 아래 대역폭을 사용하게됩니다. –