Use R Scipt to Login to Web site and perform some kind of action - r

How can I use R to login to a web site and perform some action? I think the code below is very close, but something seems to be off here.
library(httr)
library(XML)
handle <- handle("http://subscribers.footballguys.com")
path <- "amember/login.php"
# fields found in the login form.
login <- list(
amember_login = "username"
,amember_pass = "password"
,amember_redirect_url =
"https://www.google.com/accounts/Login"
)
THEN
response <- POST(handle = handle, path = path, body = login)
Any insight into this would be most appreciated.

Related

?How can I mock a manual redirect using python requests_mock?

I have a function where I'm doing a http request to a site that should do an automatic redirect but I'm doing this manually using allow_redirects=False. I have the function itself working something like this:
def make_redirect_request(self):
url = https://some.url.com
response = requests.get(url, headers={"header": "some header"}, status_code=302, allow_redirects=False)
redirected_response = requests.get(response.next.url, headers={"header": "some other header"}, status_code=200)
self.publish(redirected_response.content) # some publishing function for publishing content on page
This works as I would like it to, but I'm struggling to make a unittest that checks this correctly. I have tried something like this:
#unittest.mock.patch("publish")
#unittest.mock.patch("url", "mock_url")
def test_redirect_url(self, mock_publish):
with patch('requests.get', self.session.get) #predefined session with parameters I don't use in this function
mock_response = b"some byte response"
next_url = "mock_redirected_url"
self.adapter.register_uri("GET", "mock_url", status_code=302, allow_redirects=False, next=next_url)
#I want the next parameter to give me the url that I'm being redirected to, which is given by response.next.url in the actual function, but this doesn't work here and it also doesn't understand the allow_redirects parameter
self.adapter.register_uri("GET", next_url, content=mock_response, status_code=200)
self.session.mount("mock", self.adapter)
self.make_redirect_request()
mock_publish.assert_called_once()
I'm not sure how to get the first request to pass the url given by response.next.url to the second request, as the adapter doesn't seem to take the next and allows_redirects arguments.

How do I use {polished} package with {brochure} framework?

This was a complicating in logic execution i came across using {polished} and {brochure}.
When placing secure_ui/secure_server inside of a brochure::Page() in the same order of the example given by the {polished} dev team, there are changes to how a Shiny App is deploy on the {brochure} infrastructure. I was not sure where to relocate the polsiehd logic to.
Differences
no global.R file in a brochureApp()
multiple calls to different module_ui/server functions since each brochure::page() is its owns shiny session
single page shinyApp vs true multipage shinyApp
When needing to merge the two logics you must:
move polished_config() in globals.R --> golem::runApp() [initiate global setting for brochureApp()]
run_app <- function(
onStart = NULL,
options = list(),
enableBookmarking = NULL,
...
) {
# old 'globals.R' logic
polished_config(
app_name = "humblFinance",
api_key = "xxxx"
)
with_golem_options(
app = brochureApp(
# Putting the resources here
golem_add_external_resources(),
page1(),
),
golem_opts = list(...)
)
}
wrap each brochure::page() ui/server with polished::secure_ui/server()`
# an example login page
login <- function(id = "login", href = "/login") {
page(
href = href,
ui = secure_ui(
mod_login_ui(id = id),
sign_in_page_ui = sign_in_custom()
),
server = secure_server(
function(input, output, session) {
mod_login_server(id = id)
}
)
)
}
NOTE
sign_in_custom() is a function that returns a customized UI object from polished::sign_in_default() to create personal business webpages.
I would recommend wrapping polished::sign_in_default() in a custom global function since you will need to define this on ever brochure::page() that you want to have protected behind polished auth.
once you authenticate one page through polished, you will be able to access all other protected pages while you are still logged in. After loggign out and attempting to access any one of the protected pages will result in a custom login page

How can RWordPress retrieve blog post content?

I would like to retrieve the content of posts from my WordPress blog. Using the package RWordPress it is straightforward to retrieve categories and tags and titles, but what about the content of posts?
# Download and load the package
if (!require('RWordPress')) {
devtools::install_github(c("duncantl/XMLRPC", "duncantl/RWordPress"))
}
library(RWordPress)
# list all the functions in the package
lsf.str("package:RWordPress")
Here, for example, is the code to obtain categories, with my specifics redacted in brackets:
Cat <- getCategoryList(login = c([my user name] = '[my password'),
.server = 'http://[my blog on].wpengine.com/xmlrpc.php')
The linked SO question is not applicable as it doesn't use RWordPress [HTML and CSS and PHP coding].
This site is about posting on WordPress, not retrieving from WordPress [publishing, not gettting]. Another question uses xmlrpc as does RWordPress and a getPosts call, but it does not rely on R.
Posts <- getPosts(num = 100, blogid = 0L, login = c([my user name] = '[my password]'), .server = 'http://[my blog name].wpengine.com/xmlrpc.php')
The above code returns dates and titles and status, but not content.
Thank you for any guidance.
******************* Edit after first answer
After requiring RWordPress and XMLRPC, and then defining an object for login and for the .server, here is the console message:
> getPageList(blogid = 0L, login = WordpressLogin, .server = WordpressURL)
Error in xml.rpc(.server, op, as.integer(blogid), names(login), as.character(login), :
Problems
I find that "Problems" is not an informative error message for me.
Tell me if I am missing something, but for me the description identifier of posts seem to deliver the whole text.
RWordpress maps all the functions in XML-RPC wp
if (!require('RWordPress')) {
devtools::install_github(c("duncantl/XMLRPC", "duncantl/RWordPress"))
}
library(RWordPress)
options(WordpressLogin = c(myusername = 'mypassword'),
WordpressURL = 'http://localhost/myblog/wordpress/xmlrpc.php')
# library(knitr)
# can refer this page
# http://codex.wordpress.org/XML-RPC_wp
#Rwordpress has a one to one mapping
getCategories()
#get a list of pages
getPageList()
# pick one id from above list
id=27
getPage(pageid = id)
# description seems to have all the text of post, even though the
# document is sparse
getPage(pageid = id)$description
#similarly for posts
getPost(postid = 6)$description
I am of course using a locally installed blog, but I'd reckon this should work remotely.

how to get the current user domain in serverside while using form authentication?

I have an Application where i need to find the current Log in Users Domain name in the server site, but my authentication mode is Form, kindly help me out.
Finally i thought a nice way of solving the problem, and got it done too.
By using JS we can get the current system details then store it into a hidden filed and we can access the current system domain name & user name in the server side.
function domain_Data() {
var comdetails = new ActiveXObject("wscript.shell");
var comusername = comdetails.ExpandEnvironmentStrings("%username%");
var comusername1 = comdetails.ExpandEnvironmentStrings("%UserDomain%");
var add = comusername1 + ":" + comusername;
alert(add);
document.getElementById("_hide").value = add;}

Unable to get AccessToken using OAuth2 to impliment Google Document List Api

In my ASP.NET application I am implementing Google Document List API, to fetch User data I using OAuth2 to do so I did some code:
string CLIENT_ID = "123456789321.apps.googleusercontent.com";
string CLIENT_SECRET = "xxxxxxxxxxxxxxxxxxxxxxxx";
string SCOPE = "https://docs.google.com/feeds/ https://docs.googleusercontent.com/ https://spreadsheets.google.com/feeds/";
string REDIRECT_URI = "urn:ietf:wg:oauth:2.0:oob";
parameters = new OAuth2Parameters();
parameters.ClientId = CLIENT_ID;
parameters.ClientSecret = CLIENT_SECRET;
parameters.RedirectUri = REDIRECT_URI;
parameters.Scope = SCOPE;
parameters.AccessCode = Convert.ToString(HttpContext.Current.Session["AccessCode"]);
OAuthUtil.GetAccessToken(parameters);
settings = new RequestSettings("My Application", parameters);
Every time OAuthUtil.GetAccessToken(parameters); gives error that is:
Can any one tell me where I am doing mistake?
Also, how to access RefreshToken??
You are using the redirect URI for installed in a web application.
I'd recommend you to update your code to use the newer Drive API, which also includes a complete ASP.NET sample application and tutorial:
https://developers.google.com/drive/examples/dotnet

Resources