3

I am trying to access the smartsheet API. They have a sample code provided in curl to access it.

To access your Sheet list, construct an HTTPS request using your favorite programming or scripting language. Here is an example using the curl from a linux command line:

curl https://api.smartsheet.com/1.0/sheets \
-H "Authorization: Bearer 0da6cf0d-848c-4266-9b47-cd32a6151b1f" \
-H "Assume-User: john.doe%40smartsheet.com"

How do I do that in vb.net or from a html form?

need_the_buzz
  • 423
  • 2
  • 9
  • 18

3 Answers3

7

This is a rather large subject but at it's most simple you could try this...

Imports System.Net

and then...

Dim wHeader As WebHeaderCollection = New WebHeaderCollection()

wHeader.Clear()
wHeader.Add("Authorization: Bearer 0da6cf0d-848c-4266-9b47-cd32a6151b1f")
wHeader.Add("Assume-User: john.doe%40smartsheet.com")

Dim sUrl As String = "https://api.smartsheet.com/1.0/sheets"

Dim wRequest As HttpWebRequest = DirectCast(System.Net.HttpWebRequest.Create(sUrl), HttpWebRequest)

'wRequest.ContentType = "application/json" ' I don't know what your content type is
wRequest.Headers = wHeader
wRequest.Method = "GET"

Dim wResponse As HttpWebResponse = DirectCast(wRequest.GetResponse(), HttpWebResponse)

Dim sResponse As String = ""

Using srRead As New StreamReader(wResponse.GetResponseStream())
    sResponse = srRead.ReadToEnd()
End Using

I'm unfamiliar with the smartsheet API but you can use this as a start point.

If you are using a Proxy you will need to add...

Dim wProxy As IWebProxy = WebRequest.GetSystemWebProxy()
wProxy.Credentials = System.Net.CredentialCache.DefaultCredentials

and specify the proxy when you make the request...

wRequest.Proxy = wProxy
Ciarán
  • 3,017
  • 1
  • 16
  • 20
  • Thank you Ciaran. When I tried both of your codes i am getting the error "The remote server returned an error: (401) Unauthorized". Is that something to do with the token? – need_the_buzz Feb 12 '13 at 16:13
  • You are Unauthorized I'm afraid, i.e. you are being denied access. Do you have a password to supply in the header as well as your User? Go through your smartsheet documentation again and see where you supply your credentials – Ciarán Feb 12 '13 at 16:19
  • Yes I have generated the token by myself and provided my email address. I have used this documentation http://www.smartsheet.com/developers/api-documentation#h.3bqgetcbgx89 – need_the_buzz Feb 12 '13 at 16:21
  • Are you behind a Firewall and using a web proxy? – Ciarán Feb 12 '13 at 16:25
  • actually I was trying to connect to production and I dun have the authorization, I have changed it to the sandbox and it worked. my bad!!! but you are awesome.. – need_the_buzz Feb 12 '13 at 16:44
  • Version 1.0 is not supported. Please use version 2.0 - e.g. .../2.0/sheets. – avioing Feb 29 '16 at 22:41
2

To create a web request in VB.Net, you can use the HttpWebRequest class.

The -H argument in curl creates an extra header. To add headers to a HttpWebRequest you simply add them to the WebHeaderCollection Headers.

Example:

Dim myHttpWebRequest = CType(WebRequest.Create("https://api.smartsheet.com/1.0/sheets"), HttpWebRequest)
myHttpWebRequest.Headers.Add("Authorization: Bearer 0da6cf0d-848c-4266-9b47-cd32a6151b1f")
myHttpWebRequest.Headers.Add("Assume-User: john.doe%40smartsheet.com")
Dim myHttpWebResponse = CType(myHttpWebRequest.GetResponse(), HttpWebResponse)
sloth
  • 99,095
  • 21
  • 171
  • 219
-1

use the example and it worked perfectly for me, some changes. I leave you the example.

  Dim wHeader As WebHeaderCollection = New WebHeaderCollection()
        wHeader.Clear()
        wHeader.Add("Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9")
        wHeader.Add("Assume-User: g.n.moran%gmail.com")

        Dim wRequest As HttpWebRequest = DirectCast(System.Net.HttpWebRequest.Create(url), HttpWebRequest)
        wRequest.Headers = wHeader
        wRequest.Method = "POST"

        Dim byteArray As Byte() = Encoding.UTF8.GetBytes(json)
        wRequest.ContentType = "application/json"
        wRequest.ContentLength = byteArray.Length

        Dim dataStream As Stream = wRequest.GetRequestStream()
        dataStream.Write(byteArray, 0, byteArray.Length)
        dataStream.Close()
        dataStream.Dispose()
        ServicePointManager.Expect100Continue = False

        Dim wResponse As HttpWebResponse = DirectCast(wRequest.GetResponse(), HttpWebResponse)
        oerror.Codigo = CType(wResponse, HttpWebResponse).StatusCode
        oerror.Descripcion = CType(wResponse, HttpWebResponse).StatusDescription

        Dim responseFromServer As String = ""

        dataStream = wResponse.GetResponseStream()
        Dim reader As New StreamReader(dataStream)

        Using srRead As New StreamReader(wResponse.GetResponseStream())
            responseFromServer = srRead.ReadToEnd()
        End Using
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 02 '22 at 07:10