如何使用C#將文件上傳到帶有給定郵件地址的谷歌驅動器?使用C#將文件上傳到Google Drive#
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
獲取您的證書,那麼你可以用下面的代碼recieving用戶的同意,並獲得認證驅動器服務
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 Drive
- 2. 將大文件上傳到Google Drive
- 3. 將文件從blobstore上傳到Google Drive
- 4. 將文件上傳到Google Drive
- 5. 將多個文件上傳到Google Drive?
- 6. 如何將文件上傳到Google Drive
- 7. 上傳到Google Drive使用C#
- 8. 使用Google Drive API上傳文件
- 9. 使用拖放將文件上傳到Google Drive iFrame
- 10. 使用嵌入式瀏覽器將文件上傳到Google Drive#
- 11. 如何使用Python腳本將文件上傳到Google Drive?
- 12. 使用Raspbian和Raspberry Pi將運動文件上傳到Google Drive
- 13. 使用PyDrive將大文件上傳到Google Drive
- 14. 使用HTML5將文件上傳到Google Drive API
- 15. 確保將文件上傳到Google Drive C#
- 16. Google Drive API上傳到父文件夾
- 17. Google Drive V2 Java API - 將文件上傳到特定文件夾
- 18. 將文件上傳到Google Drive中的指定文件夾
- 19. 將圖像上傳到Google Drive公用文件夾
- 20. 使用Google Drive API將文件上傳到Google雲端硬盤時出現OutOfMemoryException
- 21. 如何使用HTTP API將文件上傳到Google Drive時指定文件名?
- 22. 使用python將文件上傳到特定的Google Drive文件夾
- 23. 使用Ruby RestClient上傳到Google Drive API
- 24. 如何使用python代碼將android上的文件上傳到Google Drive
- 25. 將圖片上傳到Google Drive for OCR
- 26. 將多個縮略圖圖片上傳到Google Drive API文件?
- 27. 如何將文件上傳到Google Drive或Onedrive的HTML5 Web App?
- 28. 將文件上傳到Google Drive與iOS的問題
- 29. Google Drive將文件上傳到Python中的[email protected]
- 30. 將上傳到Google Drive的文件鏈接插入數據庫?
http://www.daimto.com/google-drive-api-c-upload/ – NicoRiff
您無法真正使用電子郵件地址執行此操作,您必須通過用戶帳戶進行身份驗證才能訪問其數據。除此之外,答案中的代碼和上面的鏈接應該讓你開始。 – DaImTo