【问题标题】:Google Drive API v3 VB.NET Upload and Download Files to ServiceAccountGoogle Drive API v3 VB.NET 上传和下载文件到 ServiceAccount
【发布时间】:2017-04-28 16:58:12
【问题描述】:

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

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

我在将代码从 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

【问题讨论】:

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


    【解决方案1】:

    here 所述,由于您使用的是服务帐户,所有文件夹和文件都将在此服务帐户的驱动器中创建,无法通过 Web UI 访问,并且将限制为默认配额。

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

    你也可以查看这个相关的帖子:upload files to Google drive in VB.NET - searching for working code

    【讨论】:

    • 我可以接受服务帐户的这些限制;我不需要通过 Web UI 访问任何内容。我希望所有用户都上传/下载到同一个驱动器,我读到唯一可以实现的方法是通过服务帐户而不是使用 OAuth 2.0。同时,我已经使用 OAuth 2.0 成功上传/下载了一个文本文件。对客户端 ID 和客户端密码进行硬编码是否足以允许多个用户访问同一存储?即使有一种方法可以通过驱动器手动授予对每个用户电子邮件地址的访问权限,这也足够了。
    • 找到了解释为什么我应该使用服务帐户的链接:stackoverflow.com/questions/41587763/…
    猜你喜欢
    • 1970-01-01
    • 2021-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-23
    • 1970-01-01
    相关资源
    最近更新 更多