Google Drive API v3 VB.NET上传和下载文件到ServiceAccount

时间:2017-04-28 16:58:13

标签: vb.net google-drive-api google-api-dotnet-client c#-to-vb.net

我一直在尝试关注https://developers.google.com/drive/v3/web/quickstart/dotnet和Google API文档,以及在互联网上搜索,但确实没有一个简单的示例(尤其是针对v3)

我有一个VB.NET GUI,其中包含一个列表视图,其中包含文件夹中所有纯文本文件的名称。单击一个将在文本框中显示其内容。您也可以输入一个空白文本框并保存。我希望允许多个用户将其文本文件上传到Google云端硬盘,并能够下载存储在那里的所有文本文件。

我没有太多问题将代码从C#转换为VB.NET,我认为我可以通过Google验证服务帐户(或者至少我没有收到错误),但是上传只显示我的回答=没什么。任何帮助表示赞赏。

我通过Google创建了服务帐户,并具有以下条件:

Dim service = AuthenticateServiceAccount("xxxxx@xxxxx.iam.gserviceaccount.com", "C:\Users\xxxxx\Documents\Visual Studio 2015\Projects\MyProject\accountkey-0c1aa839896b.json")
If drive.UploadFile(service, "C:\Users\xxxxx\Documents\Visual Studio 2015\Projects\MyProject\file.txt") Is Nothing Then
    MsgBox("File not uploaded")
Else
    MsgBox("File uploaded")
End If

身份验证:

Public Function AuthenticateServiceAccount(ByVal serviceAccountEmail As String, ByVal serviceAccountCredentialFilePath As String) As DriveService
        Dim scopes As String() = {DriveService.Scope.Drive, DriveService.Scope.DriveAppdata, DriveService.Scope.DriveReadonly, DriveService.Scope.DriveFile, DriveService.Scope.DriveMetadataReadonly, DriveService.Scope.DriveReadonly, DriveService.Scope.DriveScripts}

    Try
        If (String.IsNullOrEmpty(serviceAccountCredentialFilePath)) Then
                Throw New Exception("Path to the service account credentials file is required.")
        End If
        If Not IO.File.Exists(serviceAccountCredentialFilePath) Then
            Throw New Exception("The service account credentials file does not exist at: " + serviceAccountCredentialFilePath)
        End If
        If (String.IsNullOrEmpty(serviceAccountEmail)) Then
            Throw New Exception("ServiceAccountEmail is required.")
        End If

        If (Path.GetExtension(serviceAccountCredentialFilePath).ToLower() = ".json") Then
            Dim credential As GoogleCredential
            Dim sstream As New FileStream(serviceAccountCredentialFilePath, FileMode.Open, FileAccess.Read)
            credential = GoogleCredential.FromStream(sstream)
            credential.CreateScoped(scopes)

            'Create the  Analytics service.
            Return New DriveService(New BaseClientService.Initializer() With {
            .HttpClientInitializer = credential,
            .ApplicationName = "Drive Service Account Authentication Sample"
            })
        Else
            Throw New Exception("Unsupported Service accounts credentials.")
        End If

    Catch e As Exception
        MsgBox("Create service account DriveService failed" + e.Message)
        Throw New Exception("CreateServiceAccountDriveFailed", e)
    End Try

End Function

上传文件:

Public Function UploadFile(service As DriveService, FilePath As String) As Google.Apis.Drive.v3.Data.File
    If (System.IO.File.Exists(FilePath)) Then
        Dim body As New Google.Apis.Drive.v3.Data.File()
        body.Name = System.IO.Path.GetFileName(FilePath)
        body.Description = "Text file"
        body.MimeType = "text/plain"

        'files content
        Dim byteArray As Byte() = System.IO.File.ReadAllBytes(FilePath)
        Dim stream As New System.IO.MemoryStream(byteArray)

        Try
            Dim request As FilesResource.CreateMediaUpload = service.Files.Create(body, stream, "text/plain")
            request.Upload()
            Return request.ResponseBody
        Catch e As Exception
            MsgBox("An error occurred: " + e.Message)
            Return Nothing
        End Try

    Else
        MsgBox("File does not exist: " + FilePath)
        Return Nothing
    End If

End Function

1 个答案:

答案 0 :(得分:0)

如上所述here,由于您使用的是服务帐户,因此将在此服务帐户的云端硬盘中创建所有文件夹和文件,这些文件夹和文件无法通过网络用户界面访问,并且仅限于默认配额。 / p>

  

要在用户的云端硬盘中添加内容,您需要通过常规OAuth 2.0流程来检索此用户的凭据。您可以在此页面上找到有关OAuth 2.0的更多信息:

     

您还可以查看此相关主题:upload files to Google drive in VB.NET - searching for working code