Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Imports System
Imports System.Diagnostics
Imports DotNetOpenAuth.OAuth2
Imports Google.Apis.Authentication.OAuth2
Imports Google.Apis.Authentication.OAuth2.DotNetOpenAuth
Imports Google.Apis.Drive.v2
Imports Google.Apis.Drive.v2.Data
Imports Google.Apis.Util
Imports Google.Apis.Services
Namespace GoogleDriveSamples
Class DriveCommandLineSample
Private Shared Sub Main(ByVal args() As String)
Dim CLIENT_ID As String = "YOUR_CLIENT_ID"
Dim CLIENT_SECRET As String = "YOUR_CLIENT_SECRET"
' Register the authenticator and create the service
Dim provider As var = New NativeApplicationClient(GoogleAuthenticationServer.Description, CLIENT_ID, CLIENT_SECRET)
Dim auth As var = New OAuth2Authenticator(Of NativeApplicationClient)(provider, GetAuthorization)
Dim service As var = New DriveService(New BaseClientService.Initializer)
Dim body As File = New File
body.Title = "My document"
body.Description = "A test document"
body.MimeType = "text/plain"
Dim byteArray() As Byte = System.IO.File.ReadAllBytes("document.txt")
Dim stream As System.IO.MemoryStream = New System.IO.MemoryStream(byteArray)
Dim request As FilesResource.InsertMediaUpload = service.Files.Insert(body, stream, "text/plain")
request.Upload
Dim file As File = request.ResponseBody
Console.WriteLine(("File id: " + file.Id))
Console.WriteLine("Press Enter to end this process.")
Console.ReadLine
End Sub
Private Shared Function GetAuthorization(ByVal arg As NativeApplicationClient) As IAuthorizationState
' Get the auth URL:
Dim state As IAuthorizationState = New AuthorizationState(new, [)
DriveService.Scopes.Drive.GetStringValue
state.Callback = New Uri(NativeApplicationClient.OutOfBandCallbackUrl)
Dim authUri As Uri = arg.RequestUserAuthorization(state)
' Request authorization from the user (by opening a browser window):
Process.Start(authUri.ToString)
Console.Write(" Authorization Code: ")
Dim authCode As String = Console.ReadLine
Console.WriteLine
' Retrieve the access token by using the authorization code:
Return arg.ProcessUserAuthorization(authCode, state)
End Function
End Class
End Namespace
Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.
Have a better answer? Share it in a comment.