Reputation: 11
I've been trying to send a post request to https://streamlabs.com/api/v2.0/donations with the access token from user settings (note that i didnt register my app but i am just going to use it for my own app so there seems to be no use to do so | https://dev.streamlabs.com/reference/authorize) but I keep getting a 401 response from the streamlabs server stating that the authentication failed because the token was invalid. I copied the entire token of 250 length and pasted it in the bearer header. Here is my code in kotlin which is modified version of their own snippet on https://dev.streamlabs.com/reference/donations-1:
val jsonPayload = JSONObject().apply {
put("skip_alert", "no")
put("name", notification.senderName)
put("message", notification.message)
put("amount", notification.amount)
put("currency", "USD")}
Log.d(TAG, "Request payload: $jsonPayload")
val mediaType = "application/json".toMediaTypeOrNull()
val body = jsonPayload.toString().toRequestBody(mediaType)
val request = Request.Builder()
.url("https://streamlabs.com/api/v2.0/donations")
.post(body)
.addHeader("accept", "application/json")
.addHeader("content-type", "application/json")
.addHeader("Authorization", "Bearer $apiToken")
.build()
client.newCall(request).execute().use { response ->
val responseBody = response.body?.string()
val responseCode = response.code
and here is the logcat output of tag:filename :
2025-03-02 00:53:55.370 23294-23294 AlertsApi com.example.upialerts D Starting test alert
2025-03-02 00:53:55.371 23294-23346 AlertsApi com.example.upialerts D Starting API call with token length: 250
2025-03-02 00:53:55.371 23294-23346 AlertsApi com.example.upialerts D First 4 chars of token: ****...
2025-03-02 00:53:55.371 23294-23346 AlertsApi com.example.upialerts D Request payload: {"skip_alert":"no","name":"Test Donation","message":"This is test donation","amount":1,"currency":"USD"}
2025-03-02 00:53:55.374 23294-23346 AlertsApi com.example.upialerts D Request URL: https://streamlabs.com/api/v2.0/donations
2025-03-02 00:53:55.374 23294-23346 AlertsApi com.example.upialerts D Request headers: accept: application/json
content-type: application/json
Authorization:
2025-03-02 00:53:55.802 23294-23346 AlertsApi com.example.upialerts D Response code: 401
2025-03-02 00:53:55.803 23294-23346 AlertsApi com.example.upialerts D Response headers: date:
content-type: text/html; charset=UTF-8
cache-control: no-cache, private
access-control-allow-origin:*
access-control-allow-methods: GET, POST, PUT, DELETE, OPTIONS
access-control-allow-headers: authorization, content-type
cf-cache-status: DYNAMIC
set-cookie:
server: cloudflare
cf-ray:
2025-03-02 00:53:55.803 23294-23346 AlertsApi com.example.upialerts D Response body: Unauthorized.
2025-03-02 00:53:55.803 23294-23346 AlertsApi com.example.upialerts E API call failed with code: 401
2025-03-02 00:53:55.803 23294-23346 AlertsApi com.example.upialerts E Error response: Unauthorized.
2025-03-02 00:53:55.803 23294-23346 AlertsApi com.example.upialerts E Authentication failed - Invalid token
B L — Today at 01:26 AM
Upvotes: 1
Views: 23