经典ASP与Tumblr API的集成

时间:2010-12-07 21:33:02

标签: api asp-classic tumblr

尝试将Classic ASP与Tumblr API集成。我想自动化Tumblr编写API以及来自Classic ASP网站的帖子。 Tumblr API位于:http://www.tumblr.com/docs/en/api

这是Tumblr API的编写PHP示例。

// 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";  
}  

我正在尝试翻译ASP。我需要知道如何从页面返回状态。即使是一开始的暗示也会很棒。一个解决方案,甚至更好。我已经使用Classic ASP和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

这是经过反复试验后使用Classic ASP定期发布Tumblr的正确代码。感谢https://stackoverflow.com/users/69820/oracle-certified-professional的帮助。

' 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 

我会在几天内在http://www.genxts.com上为Tumblr帖子(照片,引号等)添加其他示例。

1 个答案:

答案 0 :(得分:0)

要通过经典ASP运行POST请求,您需要做的就是使用MSXML2库(就像在IE Ajax中一样):

将您的请求数据构建为url字符串,方法与为GET创建参数的方式相同。确保它是urlencoded

dim request_data: request_data = "key1=value1&key2=value2"

创建请求对象并通过post连接到url。第3个参数确定呼叫是否同步。大概你需要对象的服务器版本,而不是标准的MSXML2.XMLHTTP对象

dim http: set http = CreateObject("MSXML2.ServerXMLHTTP")
http.open "POST", "http://www.tumblr.com/api/write", false

设置您需要的任何http请求标头

http.setRequestHeader "Content-type", "..."

将您的请求数据添加到请求

http.send request_data

然后您可以通过

访问回复
dim text: text = http.responseText

dim xml: set xml = http.responseXML

取决于您返回的数据类型。这是一个方便的链接:http://msdn.microsoft.com/en-us/library/ms754586(v=VS.85).aspx


好吧,我设法让这个工作

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

它似乎不喜欢URL编码。我尝试编码其他不是电子邮件和内容的内容。密码

content = Server.URLEncode(content)
content = content & "&email=" & email
content = content & "&password=" & password

但我收到了“帖子不能为空”的消息