Hi everyone, I’m trying to connect my Roblox API-KEY with Python, but when I run my code I get a 401 error. I’ve also used the “rblx-open-cloud” library and I get the error “Your key may have expired, or may not have permission to access this resource.” Could you help me please?
Uploading assets is rather complex, Roblox expects a multipart formdata which could be created like this:
payload = {
#'creatorId': creator_id, # this is also wrong!
'creationContext': {
'creator': {'userId': creator_id} #change userId to groupId if using a group
'expectedPrice': 0 # expected robux cost to upload; leave this as 0
},
'assetType': 'Decal',
'name': 'My Asset',
'description': 'Description of the asset'
}
with open(file_path, "rb") as file:
body, contentType = urllib3.encode_multipart_formdata({
"request": json.dumps(payload),
"fileContent": (
file.name, file.read(), file_mime_type
)
})
headers = {
'x-api-key': api_key,
'content-type': contentType
}
response = requests.post(url, headers=headers, data=body)
note that mime type needs a valid value such as image/png. Also, that first request to upload the file will not return an asset ID but instead an operation you’ll need to poll until you receive the asset ID.
For these reasons, I do recommend using the rblx-open-cloud library because it makes your code much less complicated and stressful, specifically with the beta version 2.
Here is how you could use rblx-open-cloud to achieve the same result:
user = rblxopencloud.User(creator_id, api_key)
with open(file_path, "rb") as file:
asset = user.upload_asset(file, rblxopencloud.AssetType.Decal, 'My asset', 'description of the asset').wait()
print("asset ID:", asset.id)