Classic ASP integration with the Tumblr API
-
09-10-2019 - |
Question
Trying to integrate Classic ASP with the Tumblr API. I want to automate the The Tumblr write API along with posts from the Classic ASP website. The Tumblr API is located here: http://www.tumblr.com/docs/en/api.
This is the write PHP example for the Tumblr API.
// Authorization info
$tumblr_email = 'info@davidville.com';
$tumblr_password = 'secret';
// Data for new record
$post_type = 'regular';
$post_title = 'The post title';
$post_body = 'This is the body of the post.';
// Prepare POST request
$request_data = http_build_query(
array(
'email' => $tumblr_email,
'password' => $tumblr_password,
'type' => $post_type,
'title' => $post_title,
'body' => $post_body,
'generator' => 'API example'
)
);
// Send the POST request (with cURL)
$c = curl_init('http://www.tumblr.com/api/write');
curl_setopt($c, CURLOPT_POST, true);
curl_setopt($c, CURLOPT_POSTFIELDS, $request_data);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($c);
$status = curl_getinfo($c, CURLINFO_HTTP_CODE);
curl_close($c);
// Check for success
if ($status == 201) {
echo "Success! The new post ID is $result.\n";
} else if ($status == 403) {
echo 'Bad email or password';
} else {
echo "Error: $result\n";
}
I'm trying a translation into ASP. I need to know how to get the status back from the page. Even a hint to start with would be great. A solution, even better. I've gotten this far with Classic ASP and Microsoft XMLHttpObject:
' Authorization info
tumblr_email = "info@davidville.com"
tumblr_password = "secret"
' Data for new record
post_type = "regular"
post_title = "The post title"
post_body = "This is the body of the post."
' Prepare POST request
request_data = "email=" tumblr_email & "&" &
request_data = request_data & "password=" & tumblr_password & "&" &
request_data = request_data & "type=" & post_type & "&" &
request_data = request_data & "title=" & post_title & "&" &
request_data = request_data & "body=" & post_body & "&" &
request_data = request_data & "generator=Your Generator Name"
request_data = server.urlencode(request_data)
Dim objHttp, strQuery
strQuery = “http://www.tumblr.com/api/write”
set objHttp = Server.CreateObject(“Msxml2.ServerXMLHTTP”)
objHttp.open “GET”, strQuery, false
objHttp.send
Response.Write objHttp.ResponseText
Set objHttp = Nothing
Here's the correct code, after trial and error, for a regular post to Tumblr using Classic ASP. Thanks to https://stackoverflow.com/users/69820/oracle-certified-professional for the help.
' Authorization info
tumblr_email = "your_registered_email"
tumblr_password = "your_tumblr_password"
' Data for new record
post_type = "regular"
post_title = "The post title"
post_body = "This is the body of the post."
' Prepare POST request
request_data = "email=" & tumblr_email & "&"
request_data = request_data & "password=" & tumblr_password & "&"
request_data = request_data & "type=" & post_type & "&"
request_data = request_data & "title=" & server.urlencode(post_title) & "&"
request_data = request_data & "body=" & server.urlencode(post_body)
set http = CreateObject("MSXML2.ServerXMLHTTP")
http.open "POST", "http://www.tumblr.com/api/write", false
http.setRequestHeader "Content-type", "application/x-www-form-urlencoded"
http.setRequestHeader "Content-length", len(content)
http.setRequestHeader "Connection", "close"
http.send request_data
Response.Write http.responseText
I'll be adding other examples for Tumblr posts (photos, quotes, etc.) on http://www.genxts.com in a few days.
Solution
To run a POST request via classic ASP, all you need to do is use the MSXML2
library (as you would do in IE Ajax):
build your request data as a url string the same way as you buid parameters for a GET. Make sure it's urlencoded
dim request_data: request_data = "key1=value1&key2=value2"
create the request object and connect to the url via post. the 3rd parameter determines whether the call is synchronous or not. presumably you need the server version of the object, instead of the standard MSXML2.XMLHTTP
object
dim http: set http = CreateObject("MSXML2.ServerXMLHTTP")
http.open "POST", "http://www.tumblr.com/api/write", false
set any http request headers you need
http.setRequestHeader "Content-type", "..."
add your request data to the request
http.send request_data
you can then access the response via
dim text: text = http.responseText
or
dim xml: set xml = http.responseXML
depending on the type of data you're returning. Here's a handy link: http://msdn.microsoft.com/en-us/library/ms754586(v=VS.85).aspx
ok, I managed to get this to work
dim http: set http = CreateObject("MSXML2.ServerXMLHTTP")
dim email: email = "email@example.com"
dim password: password = "*password"
dim content: content = content & "type=regular"
content = content & "&body=this is a test"
content = content & "&email=" & email
content = content & "&password=" & password
http.open "POST", "http://www.tumblr.com/api/write", false
http.setRequestHeader "Content-type", "application/x-www-form-urlencoded"
http.setRequestHeader "Content-length", len(content)
http.setRequestHeader "Connection", "close"
http.send content
Response.Write http.responseText
it seems it didn't like the URL encoding. I tried just encoding the other content that wasn't the email & password
content = Server.URLEncode(content)
content = content & "&email=" & email
content = content & "&password=" & password
but I got a "post must not be empty" message