在vb.net中Restsharp dropbox上传

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

我正在努力编写将文件从我的计算机上传到Dropbox的代码,该程序正在运行,但在Dropbox上,没有任何事情发生。有人可以向我解释为什么会这样吗?我究竟做错了什么?

    Dim client As RestClient = New RestClient("https://api.dropbox.com/1/metadata/link")
    Dim request As IRestRequest = New RestRequest("files_put/auto/{path}", Method.POST)
    Dim fileInfo As FileInfo = New FileInfo("C:\Users\rw\Desktop\logo.png")
    Dim fileLength As Long = fileInfo.Length
    request.AddHeader("Authorization", "myapptoken")

    request.AddHeader("Content-Length", fileLength.ToString())
    request.AddUrlSegment("path", String.Format("home/{0}", fileInfo.Name))
    Dim data As Byte() = File.ReadAllBytes("C:\Users\rw\Desktop\logo.png")
    Dim body = New Parameter With {
   .Name = "file",
   .Value = data,
   .Type = ParameterType.RequestBody


  }

request.Parameters.Add(body)
Dim response As IRestResponse = client.Execute(request)
End Sub
vb.net dropbox restsharp
3个回答
0
投票

您似乎正在尝试使用retired Dropbox API v1。

你应该使用Dropbox API v2代替。我推荐使用the official Dropbox .NET SDK。有一个使用它上传here的例子。


0
投票

如果您或任何人更喜欢使用RestSharp,这是使用Dropbox API v2上传小于150MB的文件的工作代码:

Dim sourceFile As String = "path\Filename.xlsx"
Dim data As Byte() = File.ReadAllBytes(sourceFile)
Dim fileLength As Long = data.Length
Dim targetPath As String = "/path/to/target/folder/in/dropbox/"

Dim js As New RestSharp.Serialization.Json.JsonSerializer
Dim dropboxApiArgs As String = js.Serialize(New With {.path = targetPath & IO.Path.GetFileName(sourceFile)}) ' or use Path.Combine to join path & filename

Dim client As RestClient = New RestClient("https://content.dropboxapi.com")
Dim request As IRestRequest = New RestRequest("2/files/upload", Method.POST)

request.AddHeader("Authorization", "#yourtoken#")
request.AddHeader("Content-Type", "application/octet-stream")
request.AddHeader("Dropbox-API-Arg", dropboxApiArgs)
request.AddHeader("Content-Length", fileLength.ToString())

Dim body As New Parameter With {
   .Name = "file",
   .Value = data,
   .Type = ParameterType.RequestBody
}
request.Parameters.Add(body)

Dim response As IRestResponse = client.Execute(request)

If response.IsSuccessful Then
    console.writeline("SUCCESS: " & response.StatusDescription) ' OK
Else
    console.writeline("FAILED: " & response.StatusDescription) ' Eg: Bad Request
End If

console.writeline(response.Content) ' get upload details such as rev number, etc. when successful; or else the error message

0
投票

不幸的是我的程序仍然有错误,如状态代码:0,内容长度:0详细信息:无法从传输连接读取数据。在读取所有数据之前关闭连接。

我的程序现在看起来像这样:Public Sub Main()

    Dim sourceFile As String = "C:\Users\rafalwieczorek\Desktop\Proximus_logo.png"
    Dim data As Byte() = File.ReadAllBytes(sourceFile)
    Dim fileLength As Long = data.Length
    Dim targetPath As String = "/home/"

    Dim js As New RestSharp.Serialization.Json.JsonSerializer
    Dim dropboxApiArgs As String = js.Serialize(New With {.path = targetPath & IO.Path.GetFileName(sourceFile), .mode = "add", .autorename = True, .mute = False, .strict_conflict = False})

    Dim client As RestClient = New RestClient("https://content.dropboxapi.com/2/files/upload")
    Dim request As IRestRequest = New RestRequest(Method.POST)

    request.AddHeader("Authorization", "ZHBe8GKc1QAAAAAAAAAAKIFuc-sCarTOFEqEuVYcdlNGLTH2oIzrNO5aEqXg2kF9")
    request.AddHeader("Content-Type", "application/json")
    request.AddHeader("Dropbox-API-Arg", dropboxApiArgs)
    'request.AddHeader("Content-Length", fileLength.ToString())

    '  request.Parameters.Add(body)


    Dim response As IRestResponse = client.Execute(request)

    If response.IsSuccessful Then
        Console.WriteLine("SUCCESS: " & response.StatusDescription)
    Else
        Console.WriteLine("FAILED: " & response.StatusDescription)
    End If

    Console.WriteLine(response.Content)

End Sub

你能看到任何错误吗?

© www.soinside.com 2019 - 2024. All rights reserved.