如何使用谷歌Apps脚本获取一个字preSS管理页面 [英] how to fetch a wordpress admin page using google apps script

查看:121
本文介绍了如何使用谷歌Apps脚本获取一个字preSS管理页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要我的Word preSS博客管理区域内获取一个网页。下面的脚本:

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?

推荐答案

有可能会与谷歌Apps脚本和ING后到,让你回重定向报头中的URL的问题。

There might be an issue with Google Apps Scripts and post-ing to a URL that gives you back a redirection header.

现在看来似乎可能无法跟随重定向与后 - 这里是对这个问题的讨论 - <一个href=\"https://$c$c.google.com/p/google-apps-script-issues/issues/detail?id=1254#c3\">https://$c$c.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

有没有可能,如果您修改code不进行重定向,捕捉cookie,然后做第二个请求到您的网页?我还没有实际使用的气体,但这里是我最好的猜测从阅读的文档:

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响应code(找到)。如果是这样的话,我们会再处理的头和专为设置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 ),但这一次,我们附上曲奇头包含所有在previous步骤获取的饼干。

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.

再次 - 我没有实际测试过的code,但在理论上它应该工作。请测试它与你做任何调查结果回来给我。

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.

这篇关于如何使用谷歌Apps脚本获取一个字preSS管理页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆