This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Cranium's profile picture

Pastebin API help

Started by Cranium, 14 November 2012 - 05:43 PM
Cranium #1
Posted 14 November 2012 - 06:43 PM
Right now, I am trying to improve upon the Pastebin program that comes with ComputerCraft. I have looked through the API documentation on pastebin.com, but I have no idea what I am looking at. The only problem I am having right now is the part about getting an 'api_user_key'. If anyone knows what I am supposed to do with the code here, it would be greatly appreciated if you could enlighten me.
I know I am supposed to use an http.post() command, but no idea what goes in there.
etopsirhc #2
Posted 14 November 2012 - 09:26 PM
leave the user key blank , from what i can tell you should only need it if you are setting up the program to let people log into their pastebin accounts though CC . though if you are doing that idk how you'd get what to put there :/

[edit] yeah just was looking though the api more and it is part of the login system

Creating A New Paste, [Optional Parameters]
These parameters are not required when you create a new paste, but are possible to add:

1. api_user_key - this paramater is part of the login system, which is explained further down the page.
ect..
Espen #3
Posted 15 November 2012 - 02:47 AM
This is how you send POST data via http.post():
local response = http.post( "http://www.somehost.com/document.php",
				"Variable1=Value1&"..
				"Variable2=Value2&"..
				"Variable3=Value3"
				)
You basically pass a string with all your required values to the function and concatenate them with &.
So it's a bit similar as with a GET request, just that you don't send the string as part of the URL and that you don't use the ? character.
Cranium #4
Posted 15 November 2012 - 04:40 AM
I want to do the entire integration. Posting, downloading, listing, logging in, making private posts, the whole works. The only problem I am having is this block here:

$api_dev_key   = '-snip-';
$api_user_name   = 'a_users_username';
$api_user_password  = 'a_users_password';
$api_user_name   = urlencode($api_user_name);
$api_user_password  = urlencode($api_user_password);
$url   = 'http://pastebin.com/api/api_login.php';
$ch   = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'api_dev_key='.$api_dev_key.'&api_user_name='.$api_user_name.'&api_user_password='.$api_user_password.'');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_NOBODY, 0);
$response   = curl_exec($ch);
echo $response;
The API documentation made this with an example in PHP using cURL. I know neither of those, and I was wondering how I'm going to make that something in CC.
It says that the api_user_name and api_user_password is supposed to be the user/pass of the person logging in. But What do I do with the 'a_users_username' variable?
Orwell #5
Posted 15 November 2012 - 04:50 AM
I only see the string 'a_users_username'? Which variable do you mean? I could be wrong, but I think that this is basically what you want:

local devKey = "-snip-"
local username = "Cranium"
local password = "kid"
username = textutils.urlEncode(username)
password = textutils.urlEncode(password)
local response = http.post("http://pastebin.com/api/api_login.php","api_dev_key="..devKey.."&api_user_name="..username.."&api_user_password="..password)
local content = response.readAll()
print(content)
Cranium #6
Posted 15 November 2012 - 05:42 AM

$api_user_name   = 'a_users_username';
$api_user_password  = 'a_users_password';
$api_user_name   = urlencode($api_user_name);
$api_user_password  = urlencode($api_user_password);
It's these sections here. I know I replace 'api_user_name' and 'api_user_password' with what the user will input, but what do I replace 'a_users_username' and 'a_users_password' with?
Orwell #7
Posted 15 November 2012 - 07:01 AM
$api_user_name is the variable. 'a_users_username' is just a string that's in that variable. You see? $ denotes the variables, '' denotes the strings. Look at my example one post up, it should be clear (it's the exact same in lua). There 'Cranium' is the username and 'kid' is the password. So you don't replace $api_user_name, you replace 'a_users_username' with the username.
Cranium #8
Posted 15 November 2012 - 07:31 AM
Yeah, it was confusing me with the two equations to the variables. But now I see that it was defining the variable in PHP/cURL, and then redefining it, only to post it later. Using Lua is so much easier in my opinion.
Espen #9
Posted 15 November 2012 - 08:00 AM
Yeah, it was confusing me with the two equations to the variables. But now I see that it was defining the variable in PHP/cURL, and then redefining it, only to post it later. Using Lua is so much easier in my opinion.
But really the only difference is the use of a $ to denote a variable (and maybe the semi-colon, but you can use it in Lua too), the rest is equivalent in how it can be used in Lua.
PHP:
$api_user_name   = 'a_users_username';
$api_user_password  = 'a_users_password';
$api_user_name   = urlencode($api_user_name);
$api_user_password  = urlencode($api_user_password);

Lua:
api_user_name   = 'a_users_username';
api_user_password  = 'a_users_password';
api_user_name   = urlencode(api_user_name);
api_user_password  = urlencode(api_user_password);

So as you can see it's not really any more difficult if you judge it by the code example you were referring to.^^
Cranium #10
Posted 15 November 2012 - 08:07 AM
So all I would need to do is this:

local key = "developer key"
local response = http.post(
"http://pastebin.com/api/api_login.php",
"api_dev_key="..key.."&"..
"api_user_name="..textutils.urlEncode(user).."&"..  
"api_user_password="..textutils.urlEncode(pass)  
)
if response then
	local sResponse = response.readAll()
	print(sResponse)
end
I can't test it right now, but that looks like it should work, as long as I have the right dev key. I'm looking forward to this program working to the max :P/>/>
Espen #11
Posted 15 November 2012 - 08:18 AM
Yep that should work just fine, as long as you pass the correct pastebin-url and -variables for the different API requests.
Wish you success with the rest of the program. :P/>/>
Orwell #12
Posted 15 November 2012 - 08:27 AM
Yes, that should work. :)/>/> Smart of you to check whether the response isn't nil.. Forgot about that. :P/>/>
Cranium #13
Posted 15 November 2012 - 08:32 AM
I learned very early on when dealing with http that you don't always receive a response in a timely manner. It's become habit by now.