PDA

View Full Version : سوال: ارسال و دریافت فایل به گوگل درایو



niknam_mh
سه شنبه 10 فروردین 1400, 16:34 عصر
دوستان سلام
آیا با وی بی دات نت میشه کاری کرد که بشه به طور مستقیم از نرم افزار خودمون فایل رو درون گوگل درایو بفرستیم یا از داخل گوگل درایو دانلود کنیم؟

the king
سه شنبه 10 فروردین 1400, 21:58 عصر
دوستان سلام
آیا با وی بی دات نت میشه کاری کرد که بشه به طور مستقیم از نرم افزار خودمون فایل رو درون گوگل درایو بفرستیم یا از داخل گوگل درایو دانلود کنیم؟
گوگل در بخش developers (https://developers.google.com) اش یک Drive API داره که برای همین ارتباط با Google Drive طراحی شده.
در GitHub هم نمونه کد سرویس و ابزار و مثال استفاده از Drive API هست و هم توضیح مراحلی که کاربر برای ثبت نام و دریافت کد شناسایی باید طی کنه.
اون کد شناسایی برای ارتباط نرم افزار با API لازم میشه.
https://github.com/google/google-drive-proxy
https://github.com/Obrelix/.net-Google-Drive-API-v3-File-Handling
https://github.com/se0kjun/google-drive-sample

پرستو پارسایی
چهارشنبه 11 فروردین 1400, 11:20 صبح
Imports Google.Apis.Auth Imports Google.Apis.Download


'Dev Console:
'https://console.developers.google.com/


'Nuget command:
'Install-Package Google.Apis.Drive.v2


Private Service As DriveService = New DriveService


Private Sub CreateService()
If Not BeGreen Then
Dim ClientId = "your client ID"
Dim ClientSecret = "your client secret"
Dim MyUserCredential As UserCredential = GoogleWebAuthorizationBroker.AuthorizeAsync(New ClientSecrets() With {.ClientId = ClientId, .ClientSecret = ClientSecret}, {DriveService.Scope.Drive}, "user", CancellationToken.None).Result
Service = New DriveService(New BaseClientService.Initializer() With {.HttpClientInitializer = MyUserCredential, .ApplicationName = "Google Drive VB Dot Net"})
End If
End Sub




Private Sub UploadFile(ByVal FilePath As String)
Me.Cursor = Cursors.WaitCursor
If Service.ApplicationName <> "Google Drive VB Dot Net" Then CreateService()


Dim TheFile As New File()
TheFile.Title = "My document"
TheFile.Description = "A test document"
TheFile.MimeType = "text/plain"


Dim ByteArray As Byte() = System.IO.File.ReadAllBytes(FilePath)
Dim Stream As New System.IO.MemoryStream(ByteArray)


Dim UploadRequest As FilesResource.InsertMediaUpload = Service.Files.Insert(TheFile, Stream, TheFile.MimeType)


Me.Cursor = Cursors.Default
MsgBox("Upload Finished")
End Sub


Private Sub DownloadFile(ByVal url As String, ByVal DownloadDir As String)
Me.Cursor = Cursors.WaitCursor
If Service.ApplicationName <> "Google Drive VB Dot Net" Then CreateService()


Dim Downloader = New MediaDownloader(Service)
Downloader.ChunkSize = 256 * 1024 '256 KB


' figure out the right file type base on UploadFileName extension
Dim Filename = DownloadDir & "NewDoc.txt"
Using FileStream = New System.IO.FileStream(Filename, System.IO.FileMode.Create, System.IO.FileAccess.Write)
Dim Progress = Downloader.DownloadAsync(url, FileStream)
Threading.Thread.Sleep(1000)
Do While Progress.Status = TaskStatus.Running
Loop
If Progress.Status = TaskStatus.RanToCompletion Then
MsgBox("Downloaded!")
Else
MsgBox("Not Downloaded :(")
End If
End Using
Me.Cursor = Cursors.Default
End Sub


اگر آدرس URL برای بارگیری فایل را نمی دانید ، می توانید برای دریافت کد از این کد استفاده کنید


Dim Request = Service.Files.List() Request.Q = "mimeType != 'application/vnd.google-apps.folder' and trashed = false"
Request.MaxResults = 2
Dim Results = Request.Execute
For Each Result In Results.Items
MsgBox(Result.DownloadUrl & vbCrLf & Result.Title & vbCrLf & Result.OriginalFilename)
Next



منبع : stackoverflow.com