如何使用 Chilkat DLL 从 Google 驱动器文件夹中获取 FolderID

How to get the FolderID from Google Drive folders using Chilkat DLL

我正在使用 ChilKat 开发一个使用 VB.NET 的工具,该工具执行单个文件上传到我的 google 驱动器帐户。 我能够获取根文件夹中的文件夹 ID,但是如果有文件夹路径,我很难获取文件夹 ID。

此时 参数 FolderPath 未被使用(我会,当我发现如何正确获取 FolderID 时)。目前,我可以获得“Nova”文件夹 ID,但 none 以下树中的其他文件夹:

是否有更简单的方法从 GoogleDrive 获取文件夹 ID? 我还想在 Google 驱动器上创建文件夹路径,以防它们不存在。

我从来没有处理过 JSON 或 HTTP 请求,所以我有点迷路了。 任何帮助将不胜感激! 提前致谢!

Private Function FolderID(ByVal FolderPath As String) As String

    Dim rest As New Chilkat.Rest

    '  Connect using TLS.
    Dim success As Boolean = rest.Connect("www.googleapis.com", 443, True, True)

    '  Provide the authentication credentials (i.e. the access token)
    Dim gAuth As New Chilkat.AuthGoogle
    gAuth.AccessToken = M_AccessToken
    rest.SetAuthGoogle(gAuth)

    Dim json As New Chilkat.JsonObject
    json.EmitCompact = False

    '  Get the folder Testes folder that is in the Google Drive root.
    rest.AddQueryParam("q", "'root' in parents and name='Testes'")
    Dim jsonResponse As String = rest.FullRequestNoBody("GET", "/drive/v3/files")
    If Not rest.LastMethodSuccess Then
        Return rest.LastErrorText
        Exit Function
    End If

    json.Load(jsonResponse)

    rest.ClearAllQueryParams()

    '  Now that we know the ID for the Testes directory, get the id for the folder Nova having Testes as the parent.
    Dim sbQuery As New Chilkat.StringBuilder
    sbQuery.Append("name = 'nova' and '")
    sbQuery.Append(json.StringOf("files[0].id"))
    sbQuery.Append("' in parents")

    rest.AddQueryParamSb("q", sbQuery)

    jsonResponse = rest.FullRequestNoBody("GET", "/drive/v3/files")
    If Not rest.LastMethodSuccess Then
        Return (rest.LastErrorText)
        Exit Function
    End If

    json.Load(jsonResponse)

    Return json.StringOf("files[0].id")

End Function

我通过执行迭代请求管理了一种方法。 不知道这是否是正确的方法,但它确实有效...

这是代码,现在使用格式为 /folder1/folder2/folderN

的 FolderPath
Private Function GetFolderID(ByVal FolderPath As String) As String

    Dim Rest As New Chilkat.Rest

    ' Connect to Google APIs server
    Dim Connected As Boolean = Rest.Connect("www.googleapis.com", 443, True, True)
    If Not Connected Then
        Return "Error attempting to connect: " & Rest.ConnectFailReason
        Exit Function
    End If

    ' Provide the Access token
    Dim GAuth As New Chilkat.AuthGoogle
    GAuth.AccessToken = M_AccessToken
    Rest.SetAuthGoogle(GAuth)

    ' Instance to JSON object
    Dim JSON As New Chilkat.JsonObject
    JSON.EmitCompact = False

    ' Parse the provided path and split to array
    Dim ParseFolder As String = Strings.Right(FolderPath, Len(FolderPath) - 1)
    Dim Folders As String() = Split(ParseFolder, "/")

    '  Get the root folder that is in the Google Drive folders structure
    Rest.AddQueryParam("q", "'root' in parents and name='" & Folders(0) & "'")
    Dim Response As String = Rest.FullRequestNoBody("GET", "/drive/v3/files")
    If Not Rest.LastMethodSuccess Then
        Return Rest.LastErrorText
        Exit Function
    End If
    JSON.Load(Response)

    'Iterate on the folders to get the last folder's id
    Rest.ClearAllQueryParams()
    For i = 1 To Folders.Length - 1
        Dim sbQuery As New Chilkat.StringBuilder

        sbQuery.Append("name = '" & Folders(i) & "' and '")
        sbQuery.Append(JSON.StringOf("files[0].id"))
        sbQuery.Append("' in parents")

        Rest.AddQueryParamSb("q", sbQuery)

        Response = Rest.FullRequestNoBody("GET", "/drive/v3/files")

        If Not Rest.LastMethodSuccess Then
            Return Rest.LastErrorText
            Exit Function
        End If

        JSON.Load(Response)
    Next

    ' Get the folder id
    Return JSON.StringOf("files[0].id")

End Function