How to get wordpress admin page using Google Apps script

I need to get a page in my administration area of ​​a Wordpress blog. Next 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); ... } 

performed without errors. In any case, response.getContentText() returns the login page, and I cannot access the http: //www.mydomain.invalid/wp/wp-admin/edit-comments.php page that I want to receive. Any idea on how to do this?

+7
authentication wordpress google-apps-script urlfetch
source share
1 answer

There may be a problem with the Google Apps scripts and sending to a URL that returns you a redirect header.

It seems that you will not be able to redirect with the message - here is a discussion of the issue - https://code.google.com/p/google-apps-script-issues/issues/detail?id=1254#c3 p>

Would it be possible if you change your code so as not to follow redirects, grab cookies and then execute a second request to your page? I didn't actually use GAS, but here is 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 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 will obviously need to add some debugging and error handling, but you need it.

What happens here is that we first send a message to the registration form. Assuming everything is going right, this should return us the response code 302 (Found). In this case, we will process the headers and look specifically at the "Set-Cookie" header. If it is installed, we will get rid of unnecessary material and save cookie values.

Finally, we create a new request to the desired admin page (in this case /wp/wp-admin/edit-comments.php ), but this time we attach the cookie header, which contains all the cookies received in the previous step.

If everything works as expected, you should get your admin page.

I would advise storing cookie information (in case you are going to make multiple requests to your page) in order to save time, resources and requests.

Again - I have not actually tested the code, but theoretically it should work. Please check it out and come back to me with any conclusions you make.

+10
source share

All Articles