【问题标题】:Classic ASP integration with the Tumblr API与 Tumblr API 的经典 ASP 集成
【发布时间】:2010-12-07 21:33:01
【问题描述】:

尝试将 Classic ASP 与 Tumblr API 集成。我想自动化 The 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 帖子(照片、引用等)示例。

【问题讨论】:

    标签: api asp-classic tumblr


    【解决方案1】:

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

    将您的请求数据构建为 url 字符串,就像您为 GET 构建参数一样。确保它是 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 请求标头

    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
    

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

    【讨论】:

    • 我应该使用什么内容类型?我想我已经接近了,因为我收到“授权失败”作为文本响应。无论如何,将此页面 (efreedom.com/Question/1-2326071/…) 转换为上面的经典 ASP 示例。由于 Tumblr API 没有给出实际的具体错误消息,这只是在这一点上反复试验。
    • 你快到了。我不得不检查 api 文档。常规帖子需要标题和正文。此外,如果您不对电子邮件、密码和类型进行 server.urlencode 编码,其他所有内容都可以使用。谢谢好先生的帮助。
    猜你喜欢
    • 2012-03-23
    • 2011-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多