1

I'm creating a VB.NET program that I'd like to interface with dropbox. I'm starting with the "list_folder" command which will return the contents on a specified path. Here is the URL where you can play with the command:

https://dropbox.github.io/dropbox-api-v2-explorer/#files_list_folder

The HTTP request syntax provided is as follows:

 POST /2/files/list_folder
 Host: https://api.dropboxapi.com
 User-Agent: api-explorer-client
 Authorization: Bearer HBNBvdIls8AA12AAFTvyzhNJrdwwpQcswxpRVjmwRIJANPIea7Jc1Ke
 Content-Type: application/json
 {
     "path": "/Backups"
 }

What I'm trying to do is the equivalent in a VB.NET command. Here's what i have so far:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Dim uri As String = "https://api.dropboxapi.com/2/files/list_folder"
    Dim request As Net.HttpWebRequest = Net.HttpWebRequest.Create(uri)

    request.Method = "POST"
    request.UserAgent = "api-explorer-client"

    ' this is wrong, need to supply an 'authorization token' somehow:
    Dim credentials As New Net.NetworkCredential("username", "password")
    request.Credentials = credentials

    request.ContentType = "application/json"

    'request.ContentLength  = ???
    ' how do I set content to the "path: backups" data?

    Dim response As Net.HttpWebResponse = request.GetResponse

    Debug.Print(response.StatusDescription)

    Dim dataStream As IO.Stream = response.GetResponseStream()
    Dim reader As New IO.StreamReader(dataStream)          ' Open the stream using a StreamReader for easy access.
    Dim responseFromServer As String = reader.ReadToEnd()  ' Read the content.

    MsgBox(responseFromServer)  ' Display the content.

    ' Cleanup the streams and the response.
    reader.Close()
    dataStream.Close()
    response.Close()

End Sub

What I'm missing is somehow encoding the "path": "/Backups" data specified by the doc into the request object. I'm also missing how to encode the "Authorization" access token into the request. (Above I'm using a username/password but that's probably wrong.)

Can anybody complete the VB.NET HTTP request for me? Thanks very much.

** UPDATE new code based on helpful links from the_lotus -- this works, thanks!:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Dim uri As String = "https://api.dropboxapi.com/2/files/list_folder"
    Dim request As Net.HttpWebRequest = Net.HttpWebRequest.Create(uri)

    request.Method = "POST"
    request.UserAgent = "api-explorer-client"

    request.Headers.Add("Authorization", "Bearer HBN-BvdIlsAAAFTyAQzhNJrBNINPIea7Jc1Ke")

    '{
    '"path": "/Backups"
    '}

    Dim json_data As String = "{"+ Chr(34) + "path" + Chr(34) + ": " + Chr(34) + "/Backups" + Chr(34) + "}"
    request.ContentType = "application/json"
    Dim json_bytes() As Byte = System.Text.Encoding.ASCII.GetBytes(json_data)
    request.ContentLength = json_bytes.Length

    Dim stream As IO.Stream = request.GetRequestStream

    stream.Write(json_bytes, 0, json_bytes.Length)


    Dim response As Net.HttpWebResponse = request.GetResponse

    Debug.Print(response.StatusDescription)

    Dim dataStream As IO.Stream = response.GetResponseStream()
    Dim reader As New IO.StreamReader(dataStream)          ' Open the stream using a StreamReader for easy access.
    Dim responseFromServer As String = reader.ReadToEnd()  ' Read the content.

    MsgBox(responseFromServer)  ' Display the content.

    ' Cleanup the streams and the response.
    reader.Close()
    dataStream.Close()
    response.Close()

End Sub
Chris
  • 629
  • 1
  • 10
  • 28

1 Answers1

0

You can add custom header with request.Headers.

request.Headers.Add("Authorization", "Bearer HBN-BvdIlsAAAFTyAQzhNJrBNINPIea7Jc1Ke")

To send the POST information, you can use request.GetRequestStream.

Also, make sure the data send in the POST is properly formatted JSON.

the_lotus
  • 12,668
  • 3
  • 36
  • 53