Help for Error 401 - API-KEY

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?

# NORMAL CODE: 
import requests

api_key = 'API-KEY'
creator_id = 'UserID'
file_path = 'File'

def upload_asset(creator_id, api_key, file_path):
    url = 'https://apis.roblox.com/assets/v1/assets'
    headers = {
        'Authorization': f'Bearer {api_key}'
    }
    data = {
        'creatorId': creator_id,
        'assetType': 'Decal',
        'name': 'My Asset',
        'description': 'Description of the asset'
    }
    files = {
        'file': open(file_path, 'rb')
    }
    response = requests.post(url, headers=headers, data=data, files=files)
    if response.status_code == 200:
        return response.json().get('id')
    else:
        print('Error:', response.status_code, response.json())
        return None


asset_id = upload_asset(creator_id, api_key, file_path)
print('Asset ID:', asset_id)

# RBLX-OPEN-CLOUD LIBRARY CODE:
def upload_image_to_roblox(png_file, creator):
    try:
        with open(png_file, 'rb') as file:
            response = creator.upload_asset(
                file, 
                AssetType.Decal,
                name=f"Asset {os.path.basename(png_file)}",
                description="This is the description"
            )

        if not isinstance(response, Asset):
            while True:
                status = response.fetch_operation()
                if status:
                    response = status
                    break

        asset_id = response.id
        image_url = f"http://www.roblox.com/asset/?id={asset_id}"
        return image_url
    except Exception as e:
        error_message = f"Error uploading {png_file}: {str(e)}"
        messagebox.showerror("Error", error_message)
        return None

I would really appreciate your help, I’ve been looking for the error for a long time :c

You’re entering the API key incorrectly. Roblox does not use the Authorization header but uses their own x-api-key header.

Your header should be like this:

headers = {
    'x-api-key': api_key
}

Thanks bro, i didn’t really notice that but now i run it and i get a different error “500”

Error:

Sending request...
Request Error: 500 Server Error: Internal Server Error for url: https://apis.roblox.com/assets/v1/assets
Asset ID:  None

When I put the URL “https://apis.roblox.com/assets/v1/assets/” in my browser, it shows:

{"errors":[{"message":"","code":0}]}

Code:

import requests

api_key = 'API-KEY'
creator_id = 'UserID'
file_path = 'File'

def upload_asset(creator_id, api_key, file_path):
    url = 'https://apis.roblox.com/assets/v1/assets'
    headers = {
        'x-api-key': api_key
    }
    data = {
        'creatorId': creator_id,
        'assetType': 'Decal',
        'name': 'My Asset',
        'description': 'Description of the asset'
    }
    files = {
        'fileContent': open(file_path, 'rb')
    }
    print('Sending request...')
    try:
        response = requests.post(url, headers=headers, data=data, files=files)
        response.raise_for_status()
        print('Response received.')
        if response.status_code == 200:
            asset_id = response.json().get('id')
            print('Asset ID:', asset_id)
        else:
            print('Error:', response.status_code, response.json())
    except requests.exceptions.RequestException as e:
        print('Request Error:', e)

asset_id = upload_asset(creator_id, api_key, file_path)
print('Asset ID: ', asset_id)

Do you know why this is?

1 Like

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)

and to install v2 of the library you should use:

pip install git+https://github.com/treeben77/rblx-open-cloud.git@v2

I hope this helps!

1 Like

Thank you very much, your help was very useful!!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.