403 Error with new cloud API with POST only

(Davin K Dameron) #1

I’m working on a .net app for interacting with the new cloud API. I’m able to do the GET functions just fine. However, when I try a post function, I get a 403 error. I’m for sure adding the token to the header. Here’s an example of the JSON and URL.


{“deviceEvents”: [{“component” : “main”,“capability”: “powerMeter”,“attribute”: “power”,“value”: 101,“unit”: “W”}]}

Here’s the code (token is defined but not included for obvious reasons)

Private Function SendRequest(url As String, jsonString As String) As String
    Dim uri As Uri = New Uri(url)
    Dim req As WebRequest = WebRequest.Create(uri)
    Dim jsonDataBytes = Encoding.UTF8.GetBytes(jsonString)

    req.Headers.Add("Authorization", "Bearer: " & token)
    req.ContentType = "application/json"
    req.Method = "POST"
    req.ContentLength = jsonDataBytes.Length

    Dim stream = req.GetRequestStream()
    stream.Write(jsonDataBytes, 0, jsonDataBytes.Length)

    Dim response = req.GetResponse().GetResponseStream()

    Dim reader As New StreamReader(response)
    Dim res = reader.ReadToEnd()

    Return res
End Function

(Davin K Dameron) #2

Quick update. I am able to POST commands like “on”, etc. Maybe the issue is just with my JSON format for the event?

(Davin K Dameron) #3

Ok, just noticed this in the documentation. Now I see why it’s not working.

Create events for a device. When a device is managed by a SmartApp then it is responsible for creating events to update the attributes of the device in the SmartThings platform. The token must be for a SmartApp and it must be the SmartApp that created the Device.