如何使用Google Apps Script获取WordPress管理页面?

10

我需要在WordPress博客管理区域内获取一个页面。以下脚本:

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页面。

你有什么想法吗?


你能检查一下 response.getHeaders() 的值吗?看看你得到了哪些头信息。如果你得到了状态码 200,那么你的凭证很可能是不正确的(WordPress 在成功登录后会发出状态码 302 来将你重定向到仪表盘或 redirect_to 参数)。 - Nikola Ivanov Nikolov
这是我输入正确密码后收到的标题:{Content-Length=1186,Expires=Wed,11 Jan 1984 05:00:00 GMT,Set-Cookie=wordpresspass_CENSOREDSTRING=+; expires=Mon,29-Oct-2012 09:18:51 GMT; path=/wp/,Connection=Keep-Alive,Server=Apache,X-Powered-By=PHP/5.3.3-7+squeeze14,Cache-Control=no-cache,must-revalidate,max-age=0,Pragma=no-cache,X-Frame-Options=SAMEORIGIN,Date=Tue,29 Oct 2013 09:18:51 GMT,Vary=Accept-Encoding,Content-Encoding=gzip,Keep-Alive=timeout=2,max=97,Content-Type=text/html; charset=UTF-8} - tic
这是当我使用错误密码时收到的内容:{Content-Length=1546,Expires=Wed,11 Jan 1984 05:00:00 GMT,Set-Cookie=wordpress_test_cookie=WP+Cookie+check;path=/wp/,Connection=Keep-Alive,Server=Apache,X-Powered-By=PHP/5.3.3-7+squeeze14,Cache-Control=no-cache,must-revalidate,max-age=0,Pragma=no-cache,X-Frame-Options=SAMEORIGIN,Date=Tue,29 Oct 2013 09:20:37 GMT,Vary=Accept-Encoding,Content-Encoding=gzip,Keep-Alive=timeout=2,max=99,Content-Type=text/html; charset=UTF-8} - tic
1个回答

16

在使用Google Apps Scripts进行POST请求并收到重定向头时可能会出现问题。

似乎不能通过POST请求跟随重定向 - 这是一个讨论该问题的链接 -

https://issuetracker.google.com/issues/36754794

如果您修改代码以不跟随重定向、捕获Cookie,然后再向您的页面发出第二个请求,这样是否可行? 我实际上还没有使用GAS,但在阅读文档后,这是我最好的猜测:

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);
     };
   };
   ...
}

显然,您需要添加一些调试和错误处理,但它应该能够帮助您完成任务。

这里的操作是首先向登录表单提交请求。假设一切都进行得正确,那么我们应该会收到一个302(已找到)的响应代码。如果是这种情况,然后我们将处理头文件,特别是查找“Set-Cookie”头。如果设置了该头,我们将摆脱不需要的内容并存储Cookie值。

最后,我们在管理界面上对所需页面发出新的get请求(在本例中为/wp/wp-admin/edit-comments.php),但这次我们附加了“Cookie”头,其中包含在前一步中获取的所有Cookie。

如果一切正常,您应该可以得到管理员页面:)

我建议将Cookie信息保存下来(以防您要向您的页面发出多个请求),以节省时间、资源和请求。

再说一遍-我实际上没有测试过这段代码,但理论上它应该可以工作。请测试它,并告诉我您的任何发现。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接