conn = new Socket();
conn.encoding = "binary";
if (conn.open ("www.roblox.com:80", "binary")) {
var file = new File(doc.path + "/" + "Home" + ".png");
if (file.exists) {
alert("Connected!");
conn.write("POST https://publish.roblox.com/v1/assets/upload HTTP/1.0\r\nHost:www.roblox.com\r\nConnection: close\r\n\r\n");
//reply = conn.read(999999);
//alert(reply); // FULL REPLY
conn.close();
}
}
As you can see I have the variable “file” which is an image file named “Home.png”. I want to send this to Roblox and I want it uploaded on my account as an image or decal. I also want it to return me the asset id of the newly uploaded image.
I don’t know how to post the image to the endpoint, and I can’t tell if I am on the right track. Any assistance would be greatly appreciated!
Okay so I managed to connect to it and send it data, and it did return the data it said it would. I am getting an Error 403, and according to the documentation, that means I don’t have proper authorization and the token validation has failed. I don’t know how to get permission.
I don’t know much about this, but isn’t this not allowed? If this were allowed people could upload images that aren’t appropriate for roblox, that might be why you don’t have authorization to do this
It doesn’t seem to have any documentation, so I used Tarmac.
I used Python for this. If you are not using Python, reply back telling me what you are using so I can see if I can help.
def sendImageToServer(sendingfile,Retry = True, Token=XCSRFTOKEN):
API_ENDPOINT = "https://data.roblox.com/data/upload.json"
data = {
'uploadAssetRequest.files':sendingfile, #base64.b64encode()
'content-length': '75174'
}
headers = {'Accept': 'application/json','Content-Type': 'multipart/form-data','X-CSRF-TOKEN': Token}
cookies = {'.ROBLOSECURITY': 'put your roblo security in here'}
r = requests.post(url = API_ENDPOINT, data = data, headers = headers, cookies=cookies)
print(r)
print(r.text)
print(r.headers)
if r.status_code == 403: # If the response is 403...
try:
JSON = json.loads(r.text)
ResponseCode = JSON["errors"][0]["code"]
if ResponseCode == 0: # And Roblox response is 0...
if Retry == True: # If retry is enabled...
return sendImageToServer(sendingfile,False, r.headers["x-csrf-token"]) # Re-do the request, but this time, with the `x-csrf-token` supplied as well.
except:
return r
return r, Token
I got help from @Batman212369 who also made this post:
Roblox certainly does a horrible job documenting how their API works, so most of the time you have to suffer to be able to find out how it works (kind of like with the 403 error I was getting). From what I know, a lot of API calls that aren’t ‘GET’ methods require an X-CSRF-TOKEN. If it is giving you a 403, then it is probably because of the Cookie or the X-CSRF-TOKEN.
When it comes to JavaScript, I have no idea how to do web-requests, so I cannot really help with that, but I can suggest a few ways to solve the problem. The way I solved my problem was by looking into finished APIs, such as roblox.js. If you want to make it a bit easer, you could just use the API out of the box, or if you want to hard-code it, then I do recommend going in and looking for that exact API call you require and seeing how the person implemented it. That is how I got the concept of the X-CSRF-TOKEN, which I really do believe Roblox should have documented somewhere.
Since you are trying to upload an image using JS, I highly recommend you check out the uploadItem.js script from roblox.js as it seems like you can upload images using it. I also recommend looking at the documentation to see what type of arguments it retrieves so you know what is being passed in. You can find the uploadItems.jshere, or just go under roblox-js/lib/asset/uploadItem.js. I believe that script does exactly what you want, so do take a look and get a basic idea of how it’s done. From there, now that you have the basic idea, you can implement it on your own.
I hope this helped. I know it is a bit long, so I apologize for that.