I can't pass a link to the watch2gether API

I can’t pass a link to the watch2gether API

Hi, I just started to use the API to create W2G rooms for a discord bot I wrote in Discord py. To get the POST Request right before plugging it into the Bot I threw what I found in the documentation into a test script. So far I got the following things working:

  1. API response ( a room is created and I receive the response in a JSON format including the streaky)
  2. Background Color & Opacity

However, I also used the opportunity to pass a “share” in the request so (understanding the documentation correctly) the video linked should be prefilled when I follow the link to the room. But curiously, it doesn’t. I hope somebody here can help me with this problem. Find my code below this message.

from dotenv import load_dotenv
from requests import post
from os import getenv


load_dotenv()

video = "https://www.youtube.com/watch?v=dQw4w9WgXcQ" # Example youtube link, not trying to rick roll anybody

headers = {
    "Accept": "application/json",
    "Content-Type": "application/json"
}

body = {
    "w2g_api_key": getenv("WATCH2GETHER_API_KEY"),  # Watch2Gether API key
    "share": video,
    "bg_color": "#FF0000",  # Watch2Gether Background Color
    "bg_opacity": 50  # Watch2Gether Background opacity
}

data = post("https://w2g.tv/rooms/create.json", headers, body).json()

print(data)

print(f"https://w2g.tv/rooms/{data['streamkey']}")

Thanks a lot for getting in touch! Are you 100% sure that Background and Opacity are working correctly with your request? That would be a clear indicator that the request can be parsed correctly.

Hi, I just tried switching it to a random color. It seems like it is not changed accordingly. What would be your strategy to resolving this? (sorry if this is a silly question, this is my first time hooking into a web api via a POST request)

Okay that means that there is something wrong with the JSON payload. Can you debug that request to see the body that is actually posted? This is python, right? You want to make sure that the body variable is turned into a JSON string when posting it. That’s why i call JSON.stringify on the object in the JS sample code.

So I fixed it:
Appearently requests.post did not like getting the json response sucked out of it while being called.
So what I did is I wrote the Request object into an actual variable and then got hand of the dictionary.
The color is being changed correctly and I am being Rick rolled every time I run this script. Success :slight_smile:

The new and finished code is now:

from dotenv import load_dotenv
from requests import post
from os import getenv


load_dotenv()

# Example youtube link, not trying to rick roll anybody
video = "https://www.youtube.com/watch?v=dQw4w9WgXcQ"

headers = {
    "Accept": "application/json",
    "Content-Type": "application/json"
}

body = {
    "w2g_api_key": getenv("WATCH2GETHER_API_KEY"),  # Watch2Gether API key
    "share": video,
    "bg_color": "#000000",  # Watch2Gether Background Color
    "bg_opacity": 100  # Watch2Gether Background opacity
}
# body = str(body)

response = post("https://w2g.tv/rooms/create.json", headers=headers, json=body)

print("Status Code", response.status_code)
response_dict = response.json()

print(f"\n\n\n{response_dict}")
print(f"\n\nhttps://w2g.tv/rooms/{response_dict['streamkey']}")