64 posts
Location
C:\Minecraft\saves\stuff\computer
Posted 09 May 2018 - 03:37 PM
I am working on a Pastebin API (basically the pastebin program, but way better). When I went out to test the upload function, it threw "Bad API request, invalid api_option". I set it to "paste" just like Pastebin says, but I still get that.
Upload function:
function upload(file, title, user, pass)
if not http then
return false, "HTTP required"
end
if fs.isDir(file) then
return false, "\"file\" is a directory"
end
if fs.exists(file) == false then
return false, "\"file\" doesn't exist"
end
local userkey = ""
if user and pass then
local uresponse = http.post("https://www.pastebin.com/api/api_login.php",
"api_dev_key=24eeeaa32fe9c4724e7e4f4f4c601f80&"..
"api_user_name="..textutils.urlEncode(user).."&"..
"api_user_password="..textutils.urlEncode(pass)
)
if uresponse then
if string.match(uresponse.readAll(), " ") == nil then
userkey = uresponse.readAll()
end
uresponse.close()
end
end
local hdata = fs.open(file, "r")
local data = hdata.readAll()
hdata.close()
local presponse = http.post("https://www.pastebin.com/api/api_post.php",
"api_option=paste&"..
"api_dev_key=CENSORED&"..
"api_paste_code="..data.."&"..
"api_user_key="..userkey.."&"..
"api_paste_name="..title and title or "".."&"..
"api_paste_format=lua&"..
"api_paste_expire_date=N&"..
"api_paste_private=0"
)
if presponse then
local presponses = presponse.readAll()
presponse.close()
return string.sub(presponses, 21)
end
end
2427 posts
Location
UK
Posted 09 May 2018 - 05:54 PM
The builtin pastebin program use
textutils.urlEncode for the paste name and code.
https://github.com/d...astebin.lua#L63
Edited on 09 May 2018 - 03:54 PM
64 posts
Location
C:\Minecraft\saves\stuff\computer
Posted 09 May 2018 - 06:07 PM
Yeah, I looked at the source code a few times and that clearly slipped past my eyes. Will try it out!
EDIT: Nope, still invalid api_option. The postData from the pastebin program works, tho…
Edited on 09 May 2018 - 04:08 PM
64 posts
Location
C:\Minecraft\saves\stuff\computer
Posted 09 May 2018 - 06:28 PM
BTW, what does the second argument in string.match( sResponse , "[^/]+$") even do?!
749 posts
Location
BOO!!
Posted 09 May 2018 - 06:36 PM
BTW, what does the second argument in string.match( sResponse , "[^/]+$") even do?!
It's called a pattern, it is to return part of a string
http://lua-users.org/wiki/PatternsTutorial
64 posts
Location
C:\Minecraft\saves\stuff\computer
Posted 09 May 2018 - 07:28 PM
BTW, what does the second argument in string.match( sResponse , "[^/]+$") even do?!
It's called a pattern, it is to return part of a string
http://lua-users.org...atternsTutorial
I know it's a pattern, but why random characters?
1426 posts
Location
Does anyone put something serious here?
Posted 09 May 2018 - 07:53 PM
So
[^/] means any character which isn't a /.
+ means one or more, and
$ means the end of the string. So when you provide a string like
foo/bar/baz, it'll match as many non-
/ characters at the end of the string - namely
baz. In this particular case, I believe pastebin returns
http://pastebin.com/PASTE_ID in its response, and so it strips the ID out of that.
2427 posts
Location
UK
Posted 10 May 2018 - 01:01 PM
Have you tried looking at the APi docs?
https://pastebin.com/api
64 posts
Location
C:\Minecraft\saves\stuff\computer
Posted 10 May 2018 - 01:24 PM
Yeah, I literally studied it and the built-in pastebin app.
Edited on 10 May 2018 - 11:24 AM