Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
287 views
in Technique[技术] by (71.8m points)

vb.net - How to get the FolderID from Google Drive folders using Chilkat DLL

Im using the ChilKat to develop a tool using VB.NET, that performs a single file upload to my google drive account. Im able to get the folderID within a root folder, but im struggling to get the folderID if there is a path of folders.

At this moment the argument FolderPath is not being used (i will, when i uncover how to properly get the FolderID). For now, i can get the "Nova" folder id, but none of the other folders within the following tree:

enter image description here

Is there an easier way to get the folderID from GoogleDrive? I would also like to create the path of folders on Google Drive, in case they dont exist.

I never worked with JSON or HTTP requests, so im kind of lost here. Any help would be mostly appreciated! Thanks in advance!

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
question from:https://stackoverflow.com/questions/65862352/how-to-get-the-folderid-from-google-drive-folders-using-chilkat-dll

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I managed a way by performing iterative requests. Don't know if this is the correct way, but it works...

Here's the code, now using the FolderPath with format /folder1/folder2/folderN

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...