2010-12-07 126 views
0

嘗試將經典ASP與Tumblr API集成。我想自動化Tumblr編寫API以及經典ASP網站的帖子。 Tumblr API位於:http://www.tumblr.com/docs/en/api與Tumblr API集成的經典ASP

這是寫Tumblr API的PHP示例。

// Authorization info 
$tumblr_email = '[email protected]'; 
$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。我需要知道如何從頁面獲取狀態。即使有一個提示開始會很好。一個解決方案,甚至更好。我已經得到了這麼遠與傳統的ASP和微軟XMLHttpObject:

' Authorization info 
tumblr_email = "[email protected]" 
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 

下面是正確的代碼,經過反覆試驗,對於一個普通崗位的tumblr使用傳統的ASP。感謝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 

我會加入對的tumblr帖子(照片,報價等)上http://www.genxts.com幾天其他例子。

回答

0

要通過傳統的ASP運行POST請求,所有你需要做的就是使用MSXML2庫(如你在IE阿賈克斯做):

建立你的請求數據作爲一個URL字符串相同的方式,你爲GET創建buid參數。確保它是urlencoded

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

創建請求對象並通過post連接到url。第三個參數決定呼叫是否同步。想必你所需要的對象的服務器版本,而不是標準的MSXML2.XMLHTTP對象

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

組需要

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

請求數據添加到請求

http.send request_data 
任何HTTP請求頭

然後您可以通過

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 protected]" 
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 

,但我得到了一個「後不能爲空」的消息

+0

內容類型,我應該用什麼呢?我想我越來越近,因爲我得到了「授權失敗」作爲文本響應。無論如何翻譯此頁面(http://zh.efreedom.com/Question/1-2326071/Sending-POST-Request-Cocoa-Tumblr)到上面的經典ASP示例。由於Tumblr API沒有提供實際的特定錯誤消息,因此這只是試驗和錯誤。 – Patriotec 2010-12-07 23:11:28