如何使用API 将文件上传到VBA中的BOX

问题描述 投票:0回答:1

我正在尝试使用API​​将用户计算机中的文件上传到BOX。在GitHub上我发现了这个:https://github.com/Tackgnol/VBA-BOX-API/blob/master/mBox.bas

Box Sync和Box Drive不是解决方案(不能强制用户安装)。

当我打印StatusText时,我得到了“Method Not Allowed”。

65853643834是从url获取的文件夹ID。 h5ntjo525se0tbhwswq8ozpoqsge ****来自Box Dev“Client ID”

enter image description here

Sub UploadFile()
    UploadBoxFile "h5ntjo525se0tbhwswq8ozpoqsge****"
End Sub

Sub UploadBoxFile(ByVal sToken As String)
    Dim curlInput As XMLHTTP60
    Dim sQuery As String
    Dim sXMLInput As String

    Set curlInput = CreateObject("MSXML2.XMLHTTP.6.0")

    sQuery = "https://upload.box.com/api/2.0/files/content"

    sXMLInput = "attributes={name: ""fileSample.txt"", ""parent"": {""id"":         ""65853643834""}}" & vbNewLine & "file=C:\Users\MichalPalko\Downloads\fileSample.txt"

    curlInput.Open "POST", sQuery, False

    curlInput.setRequestHeader "Authorization:", "Bearer " & sToken & 3243
    curlInput.send sXMLInput
    Debug.Print curlInput.StatusText
End Sub
excel vba api upload box
1个回答
0
投票

我遇到了同样的问题,但对您的代码进行了一些调整,并收到了“不支持的媒体类型”的StatusText响应。

我觉得我很近,但在黑暗中刺伤。你有没有搞清楚?

我调整后的代码如下:

Sub UploadBoxFile(sFilePath As String)
    Dim curlInput 'As XMLHTTP60
    Dim sQuery As String
    Dim sXMLInput As String

    Dim sToken As String
    Dim sFolderID As String
    Const sP As String = """"
    Const sAp As String = "'"

    sQuery = "https://upload.box.com/api/2.0/files/content"
    sToken = "XXXXXXXXXXXXXXXXXXXXXXXXX"     'My Obscured token
    sFolderID = "1234567890"                 'My Obscured FolderID
    sFileName = "TEST File 0001a.txt"

    sXMLInput = "attributes=" & sAp & "{" & sP & "name" & sP & ":" & sP & sFileName _
        & sP & ", " & sP & "parent" & sP & ":{" & sP & "id" & sP & ":" & sP & sFolderID & sP & "}}" & sAp _
        & vbNewLine & "file=" & sFilePath

Debug.Print sXMLInput

    Set curlInput = CreateObject("MSXML2.XMLHTTP.6.0")
    With curlInput
        .Open "POST", sQuery, False
        .setRequestHeader "Authorization:", "Bearer " & sToken
        .send sXMLInput
        Debug.Print .StatusText
    End With
End Sub
© www.soinside.com 2019 - 2024. All rights reserved.