API Drive con Visual Basic .NET
Publicado por Sebastian (3 intervenciones) el 23/10/2017 18:03:40
Necesito ayuda para manipular la API Drive de Google, toda la documentación está escrita en c# por lo tanto no me sirve.
He "traducido" el código a vb pero sigue sin funcionar, estoy usando la versión 2, y usando windows forms.
Les adjunto el código.
Gracias por su ayuda :D
He "traducido" el código a vb pero sigue sin funcionar, estoy usando la versión 2, y usando windows forms.
Les adjunto el código.
Gracias por su ayuda :D
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
Imports Google.Apis.Auth.OAuth2
Imports Google.Apis.Drive.v2
Imports Google.Apis.Drive.v2.Data
Imports Google.Apis.Services
Imports Google.Apis.Util.Store
Imports System
Imports System.Collections.Generic
Imports System.IO
Imports System.Linq
Imports System.Text
Imports System.Threading
Imports System.Threading.Tasks
Imports Google
Imports Google.Apis.Auth
Imports Google.Apis.Download
Public Class Form1
Private Service As DriveService = New DriveService
Private Sub CreateService()
Dim ClientId = "My id client"
Dim ClientSecret = "My secret client"
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 = "PruebaAPIDrivee"})
End Sub
Private Sub UploadFile(FilePath As String)
Me.Cursor = Cursors.WaitCursor
If Service.ApplicationName <> "PruebaAPIDrivee" Then CreateService()
Dim TheFile As New Apis.Drive.v2.Data.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(url As String, 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
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim openFileDialog1 As New OpenFileDialog()
openFileDialog1.InitialDirectory = "c:\"
openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
openFileDialog1.FilterIndex = 2
openFileDialog1.RestoreDirectory = True
If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
TextBox1.Text = openFileDialog1.FileName()
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
UploadFile(TextBox1.Text)
End Sub
Valora esta pregunta
0