问题描述
我正在使用 ChilKat 开发一个使用 VB.NET 的工具,该工具将单个文件上传到我的谷歌驱动器帐户。 我能够在根文件夹中获取文件夹 ID,但是如果有文件夹路径,我很难获取文件夹 ID。
此时 参数 FolderPath 没有被使用(当我发现如何正确获取 FolderID 时,我会使用)。目前,我可以获取“Nova”文件夹 ID,但无法获取以下树中的其他文件夹:
是否有更简单的方法可以从 GoogleDrive 获取文件夹 ID? 我还想在 Google Drive 上创建文件夹的路径,以防它们不存在。
我从来没有处理过 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