如何使用谷歌应用程序脚本获取 wordpress 管理页面 [英] how to fetch a wordpress admin page using google apps script
问题描述
我需要在我的 Wordpress 博客管理区域内获取一个页面.以下脚本:
I need to fetch a page inside my Wordpress blog admin area. The following script:
function fetchAdminPage() {
var url = "http://www.mydomain.invalid/wp/wp-admin/wp-login.php";
var options = {
"method": "post",
"payload": {
"log": "admin",
"pwd": "password",
"wp-submit": "Login",
"redirect_to":"http://www.mydomain.invalid/wp/wp-admin/edit-comments.php",
"testcookie": 1
}
};
var response = UrlFetchApp.fetch(url, options);
...
}
执行没有错误.无论如何,response.getContentText()
返回登录页面,我无法访问该页面 http://www.mydomain.invalid/wp/wp-admin/edit-comments.php 这是我想要获取的.知道如何做到这一点吗?
is executed without errors. Anyway, response.getContentText()
returns the login page, and I am not able to access the page http://www.mydomain.invalid/wp/wp-admin/edit-comments.php which is the one I want to fetch.
Any idea on how to do this?
推荐答案
Google Apps 脚本和发布到 URL 可能会出现问题,该 URL 会返回重定向标头.
There might be an issue with Google Apps Scripts and post-ing to a URL that gives you back a redirection header.
似乎可能无法通过帖子跟踪重定向 - 这是有关该问题的讨论 - https://code.google.com/p/google-apps-script-issues/issues/detail?id=1254#c3
It seems like it might not be possible to follow the redirect with a post - here's a discussion on the issue - https://code.google.com/p/google-apps-script-issues/issues/detail?id=1254#c3
如果您修改代码以不遵循重定向、捕获 cookie 然后对您的页面进行第二次请求,是否有可能?我还没有真正使用过 GAS,但这是我阅读文档后的最佳猜测:
Would it be possible, if you modify your code to not follow redirects, capture the cookies and then do a second request to your page? I haven't actually used GAS, but here's my best guess from reading the documentation:
function fetchAdminPage() {
var url = "http://www.mydomain.invalid/wp/wp-admin/wp-login.php";
var options = {
"method": "post",
"payload": {
"log": "admin",
"pwd": "password",
"wp-submit": "Login",
"testcookie": 1
},
"followRedirects": false
};
var response = UrlFetchApp.fetch(url, options);
if ( response.getResponseCode() == 200 ) {
// Incorrect user/pass combo
} else if ( response.getResponseCode() == 302 ) {
// Logged-in
var headers = response.getAllHeaders();
if ( typeof headers['Set-Cookie'] !== 'undefined' ) {
// Make sure that we are working with an array of cookies
var cookies = typeof headers['Set-Cookie'] == 'string' ? [ headers['Set-Cookie'] ] : headers['Set-Cookie'];
for (var i = 0; i < cookies.length; i++) {
// We only need the cookie's value - it might have path, expiry time, etc here
cookies[i] = cookies[i].split( ';' )[0];
};
url = "http://www.mydomain.invalid/wp/wp-admin/edit-comments.php";
options = {
"method": "get",
// Set the cookies so that we appear logged-in
"headers": {
"Cookie": cookies.join(';')
}
};
response = UrlFetchApp.fetch(url, options);
};
};
...
}
您显然需要添加一些调试和错误处理,但它应该可以帮助您完成.
You would obviously need to add some debugging and error handling, but it should get you through.
这里发生的事情是我们首先发布到登录表单.假设一切正常,那应该会给我们一个 302(Found) 的响应代码.如果是这种情况,我们将处理标头并专门查找Set-Cookie"标头.如果设置了,我们将删除不需要的东西并存储 cookie 值.
What happens here is that we first post to the log-in form. Assuming that everything goes correctly, that should give us back a response code of 302(Found). If that's the case, we will then process the headers and look specifically for the "Set-Cookie" header. If it's set, we'll get rid of the un-needed stuff and store the cookies values.
最后,我们向管理员上的所需页面发出新的 get 请求(在本例中为 /wp/wp-admin/edit-comments.php
),但这次我们附加了Cookie" 包含在上一步中获取的所有 cookie 的标头.
Finally we make a new get request to the desired page on the admin( in this case /wp/wp-admin/edit-comments.php
), but this time we attach the "Cookie" header which contains all of the cookies acquired in the previous step.
如果一切正常,你应该得到你的管理页面:)
If everything works as expected, you should get your admin page :)
我建议存储 cookie 信息(以防您要向页面发出多个请求)以节省时间、资源和请求.
I would advise on storing the cookies information(in case you're going to make multiple requests to your page) in order to save time, resources and requests.
再说一遍——我还没有真正测试过代码,但理论上它应该可以工作.请测试它,然后将您的任何发现返回给我.
Again - I haven't actually tested the code, but in theory it should work. Please test it and come back to me with any findings you make.
这篇关于如何使用谷歌应用程序脚本获取 wordpress 管理页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!