주어진 메일 주소로 C 드라이브를 사용하여 Google 드라이브에 파일을 업로드하려면 어떻게해야합니까?C 드라이브로 파일 업로드
3
A
답변
2
@ NicoRiff의 참고 자료 외에도 Uploading Files 설명서를 확인할 수 있습니다. 다음은 샘플 코드입니다.
var fileMetadata = new File()
{
Name = "My Report",
MimeType = "application/vnd.google-apps.spreadsheet"
};
FilesResource.CreateMediaUpload request;
using (var stream = new System.IO.FileStream("files/report.csv",
System.IO.FileMode.Open))
{
request = driveService.Files.Create(
fileMetadata, stream, "text/csv");
request.Fields = "id";
request.Upload();
}
var file = request.ResponseBody;
Console.WriteLine("File ID: " + file.Id);
tutorial을 확인할 수도 있습니다.
2
"메일 ID로 업로드"의 의미가 확실하지 않습니다. 사용자의 Google 드라이브에 액세스하려면 해당 비밀번호로 Google에서 액세스 토큰을 받아야합니다. 이 작업은 API을 사용하여 수행됩니다.
사용자의 동의를 얻은 후 액세스 토큰이 반환됩니다. 그리고이 액세스 토큰은 API 요청을 보내는 데 사용됩니다. 처음에는 더 약 Authorization
알아, 당신은 그럼 당신은 사용자의 동의를 잡 및 인증 획득을 위해 다음과 같은 코드를 사용할 수 있습니다, 당신의 드라이브 API를 사용하여 프로젝트를 등록하고 Developer Consol
에서 자격 증명을 취득해야 드라이브 서비스
string[] scopes = new string[] { DriveService.Scope.Drive,
DriveService.Scope.DriveFile};
var clientId = "xxxxxx"; // From https://console.developers.google.com
var clientSecret = "xxxxxxx"; // From https://console.developers.google.com
// here is where we Request the user to give us access, or use the Refresh Token that was previously stored in %AppData%
var credential = GoogleWebAuthorizationBroker.AuthorizeAsync(new ClientSecrets { ClientId = clientId,
ClientSecret = clientSecret},
scopes,
Environment.UserName,
CancellationToken.None,
new FileDataStore("MyAppsToken")).Result;
//Once consent is recieved, your token will be stored locally on the AppData directory, so that next time you wont be prompted for consent.
DriveService service = new DriveService(new BaseClientService.Initializer()
{
HttpClientInitializer = credential,
ApplicationName = "MyAppName",
});
service.HttpClient.Timeout = TimeSpan.FromMinutes(100);
//Long Operations like file uploads might timeout. 100 is just precautionary value, can be set to any reasonable value depending on what you use your service for.
다음은 드라이브에 업로드하기위한 코드입니다.
// _service: Valid, authenticated Drive service
// _uploadFile: Full path to the file to upload
// _parent: ID of the parent directory to which the file should be uploaded
public static Google.Apis.Drive.v2.Data.File uploadFile(DriveService _service, string _uploadFile, string _parent, string _descrp = "Uploaded with .NET!")
{
if (System.IO.File.Exists(_uploadFile))
{
File body = new File();
body.Title = System.IO.Path.GetFileName(_uploadFile);
body.Description = _descrp;
body.MimeType = GetMimeType(_uploadFile);
body.Parents = new List<ParentReference>() { new ParentReference() { Id = _parent } };
byte[] byteArray = System.IO.File.ReadAllBytes(_uploadFile);
System.IO.MemoryStream stream = new System.IO.MemoryStream(byteArray);
try
{
FilesResource.InsertMediaUpload request = _service.Files.Insert(body, stream, GetMimeType(_uploadFile));
request.Upload();
return request.ResponseBody;
}
catch(Exception e)
{
MessageBox.Show(e.Message,"Error Occured");
}
}
else
{
MessageBox.Show("The file does not exist.","404");
}
}
다음은 MIME 타입을 결정하기위한 작은 기능입니다 :
private static string GetMimeType(string fileName)
{
string mimeType = "application/unknown";
string ext = System.IO.Path.GetExtension(fileName).ToLower();
Microsoft.Win32.RegistryKey regKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(ext);
if (regKey != null && regKey.GetValue("Content Type") != null)
mimeType = regKey.GetValue("Content Type").ToString();
return mimeType;
}
관련 문제
- 1. 내 컴퓨터에서 Google 드라이브로 이미지 파일 업로드
- 2. appengine (python)에서 Google 드라이브로 파일 업로드
- 3. 안드로이드 앱에서 Google 드라이브로 파일 업로드
- 4. 라스베리 파이에서 google 드라이브로 파일 업로드
- 5. C++ Qt에서 외장 USB 드라이브로 파일 전송
- 6. 클라이언트에서 다른 드라이브로 파일 이동
- 7. 간단한 샘플을 Android 드라이브에서 Google 드라이브로 파일 업로드
- 8. C 드라이브로 포맷 드라이브
- 9. ftp에서 C 드라이브로 매핑
- 10. IOS 응용 프로그램에서 Google 드라이브로 이미지 업로드
- 11. Mac의 명령 줄에서 Google 드라이브로 업로드
- 12. Appengine-django에서 Google 드라이브로 문서 업로드
- 13. C# 파일 업로드 솔루션.
- 14. 메모리에서 C# 파일 업로드
- 15. 파일 업로드 C# asp.net
- 16. C# 복수 파일 업로드
- 17. 동적 파일 업로드 C#
- 18. 파일 업로드/업로드, 필터 할 문자 C#
- 19. iOS 앱에서 Dropbox 및 Google 드라이브로 동영상을 업로드 하시겠습니까?
- 20. 배치 스크립트를 통해 네트워크 드라이브로 파일 이동
- 21. 문서를 Google 드라이브로 업로드 할 때 줄 바꿈이 끊어짐
- 22. XHR을 사용하여 JS에서 Google 드라이브로 재개 할 수있는 업로드
- 23. C# 웹 사이트에 파일 업로드
- 24. C# AJAX 여러 파일 업로드?
- 25. C# 클라이언트에서 IIS로 파일 업로드
- 26. http 파일 업로드 목표 C
- 27. asp.net에 대한 파일 업로드 C#
- 28. C# ftp 서버에 파일 업로드
- 29. Gmail 스타일 파일 업로드 C#
- 30. Retrofit + Restful C# 파일 업로드
http://www.daimto.com/google-drive-api-c-upload/ – NicoRiff
데이터에 액세스하기 위해 사용자 계정에 인증을 받아야하는 이메일 주소로는 실제로 할 수 없습니다. . 그 답변과 위의 링크에있는 코드는 시작해야합니다. – DaImTo