Jump to content




http.post() to Pastebin throws "Invalid api_option"


8 replies to this topic

#1 Windows10User

  • Members
  • 62 posts
  • LocationC:\Minecraft\saves\stuff\computer

Posted 09 May 2018 - 02: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


#2 Lupus590

  • Members
  • 1,975 posts
  • LocationUK

Posted 09 May 2018 - 04:54 PM

The builtin pastebin program use textutils.urlEncode for the paste name and code.

https://github.com/d...astebin.lua#L63

Edited by Lupus590, 09 May 2018 - 04:54 PM.


#3 Windows10User

  • Members
  • 62 posts
  • LocationC:\Minecraft\saves\stuff\computer

Posted 09 May 2018 - 05: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 by Windows10User, 09 May 2018 - 05:08 PM.


#4 Windows10User

  • Members
  • 62 posts
  • LocationC:\Minecraft\saves\stuff\computer

Posted 09 May 2018 - 05:28 PM

BTW, what does the second argument in string.match( sResponse , "[^/]+$") even do?!

#5 EveryOS

    Resident Necromancer

  • Members
  • 566 posts
  • LocationBOO!!

Posted 09 May 2018 - 05:36 PM

View PostWindows10User, on 09 May 2018 - 05:28 PM, said:

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

#6 Windows10User

  • Members
  • 62 posts
  • LocationC:\Minecraft\saves\stuff\computer

Posted 09 May 2018 - 06:28 PM

View PostEveryOS, on 09 May 2018 - 05:36 PM, said:

View PostWindows10User, on 09 May 2018 - 05:28 PM, said:

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?

#7 SquidDev

    Frickin' laser beams

  • Members
  • 1,409 posts
  • LocationDoes anyone put something serious here?

Posted 09 May 2018 - 06: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.

#8 Lupus590

  • Members
  • 1,975 posts
  • LocationUK

Posted 10 May 2018 - 12:01 PM

Have you tried looking at the APi docs? https://pastebin.com/api

#9 Windows10User

  • Members
  • 62 posts
  • LocationC:\Minecraft\saves\stuff\computer

Posted 10 May 2018 - 12:24 PM

Yeah, I literally studied it and the built-in pastebin app.

Edited by Windows10User, 10 May 2018 - 12:24 PM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users