Error: could not find function "workspace" - r

I am trying to access an Azure ML Workspace from RStudio using the script:
install.packages("AzureML")
ws <- workspace(
id = "xxxxxxxxxxxxxxxxxxxxxxxxxx",
auth = "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy"
)
and get the error could not find function "workspace"
The workspace id and primary authorisation token are 100% correct.
I am very new to R and presume I am making a schoolboy error.
Any help gratefully appreciated ?
Cheers,
Andy

https://cran.r-project.org/web/packages/AzureML/vignettes/getting_started.html suggests the following:
library(AzureML)
ws <- workspace(
id = "your workspace ID",
auth = "your authorization token"
)
Your code is missing the library(AzureML) part.
Documentation of library: https://www.rdocumentation.org/packages/base/versions/3.4.3/topics/library
After calling that, the workspace function should be available.

Related

R httr - Error Getting API OATH Token - Missing grant_type

I am trying to test out the Dun and Bradstreet API in R using httr.
I am trying to leverage the oauth2 functionality in httr.
Here is my code:
library(httr)
library(jsonlite)
clientid = redacted
secret = redacted
app <- oauth_app(appname = "Dun", key = clientid, secret = secret)
endpoint <- oauth_endpoint(base_url = "https://login.bisnode.com/sandbox/v1/token.oauth2", authorize="https://login.bisnode.com/sandbox/v1/token.oauth2", access = "https://login.bisnode.com/sandbox/v1/token.oauth2")
token <- oauth2.0_token(endpoint = endpoint, app = app, scope = "credit_data_companies", user_params = list(grant_type = "client_credentials"))
In Rstudio I get this message:
Waiting for authentication in browser... Press Esc/Ctrl + C to abort
In the browser, I get this message:
{"error":"invalid_request","error_description":"grant_type is
missing"}
I am sure this is something simple, but I could not find a solution in any other posts. Thank you!

Question on Oauth2 Resource Owner Password Credential Grant in R httr

I use Oauth2 authorization to get the token for REST API. I tested the setting in SoapUI and it is working with the Resource Owner Password Credential Grant as shown below.
The setting in the SoapUI testing is working
Now, I want to retrieve the token through R httr library, but I don’t know how to set the resource owner name and password in the R code. Here is my Code
library(httr)
library(jsonlite)
r <- POST("https://XXXXXXX.com/authservice/connect/token",
config = list(),
body = list(
# grant_type = "RESOURCE_OWNER_PASSWORD_CREDENTIALS",
grant_type = "resource_owner_credentials",
resourceOwnerName = "ResourceName",
resourceOwnerPassword = "Resourcepwd",
# resource_Owner_Name = "ResourceName",
# resource_Owner_Password = "Resourcepwd",
# resourceOwner_Name = "ResourceName",
# resourceOwner_Password = "Resourcepwd",
# resource_OwnerName = "ResourceName",
# resource_OwnerPassword = "Resourcepwd",
# resource_Name = "ResourceName",
# resource_Password = "Resourcepwd",
# resource_id = "ResourceName",
# resource_secret = "Resourcepwd",
client_id = "ClientIDname",
client_secret = "Clientpassword",
scope = "xxxGroup"
),
encode = "form"
)
warn_for_status(r)
content(r)
status_code(r)
I got a 400 error ("unsupported_grant_type" and bad request). I am guessing there may be something wrong with the syntax on the resource onwer name and password, so I also tried all the possible combinations that I commented in above code but they all failed.
My question is: are the parameter names for both resource owner name and password pre-defined by the API document, or they are the fixed names defined by R-httr librabry, regardless what token authorization service?
Can someone help to take a look at? Any input is greatly appreciate!

REST API to stocktwits.com in R

I have been banging my head over this the whole day. I am trying to access StockTwits API (https://api.stocktwits.com/developers) from an R session. I have earlier accessed the twitter API (via rtweet) without hassles.
I have created an app and got the client id and key (the below are just examples).
app_name = "some.name";
consumer_key = "my_client_id";
consumer_secret = "my_client_key";
uri = "http://iimb.ac.in" # this is my institute's homepage. It doesn't allow locahost OR 127.0.0.1
scope = "read,watch_lists,publish_messages,publish_watch_lists,direct_messages,follow_users,follow_stocks";
base_url = "https://api.stocktwits.com/api/2/oauth"; # see https://api.stocktwits.com/developers/docs/api
The procedure is to create an oauth2.0 app and endpoint. Then call oauth2.0_token.
oa = httr::oauth_app(app_name, key = consumer_key, secret = consumer_secret, redirect_uri = uri);
oe = httr::oauth_endpoint("stocktwits", "authorize", "token", base_url = base_url);
mytoken = httr::oauth2.0_token(oe, oa, user_params = list(resource = base_url), use_oob = F); # use_oob = T doesn't work.
After firing the above, it takes me to the browser for sign-in. I sign-in and it asks me to connect. After that, I am taken back to my URI plus a code, i.e. https://www.iimb.ac.in/?code=295ea3114c3d8680a0ed295d52313d7092dd90ae&state=j9jXzEqri1
Is the code my access token or something else? The oauth2.0_token() call keeps waiting for the code since the callback is not localhost. I didn't seem to get a hang of that.
I then try to access the API using the above code as access token but I am thrown "invalid access token" error. The format is described in https://api.stocktwits.com/developers/docs/api#search-index-docs
Can someone tell me what I have missed? If required I can share my app_name, consumer_key and consumer_secret for replication.

RJython send Mail via .bat Firewall

We have .bat Files which call an R-script and generate some data and then send an email to certain users via rJython.
If I run this script in RStudio everything goes well and the mail is sent. But if i call the .bat file we get the following error.
It has probably something to do with the firewall in our company, can somebody help us out? We need to tell our IT departement something bc they cant help us either.
Error in jython.exec(rJython,mail): (-1, 'Unmapped exception:java.net.SocketException: Permission denied: connect') Execution halted
The R-Code to send the mail is:
#Sendmail Code
rJython<-rJython()
rJython$exec("import smtplib")
rJython$exec("from email.MIMEText import MIMEText")
rJython$exec("import email.utils")
rJython$exec("COMMASPACE = ', '")
recipients_string_j = paste("toaddrs = [",recipients_string,"]",sep="")
#mail config
mail<-c( fromaddr = 'test#XXX.com",
recipients_string_j,
paste("msg = MIMEText('",data.frame,"','html')",sep="",collapse=""),
paste("msg['Subject'] = 'DUMMY SUBJECT'"),
"msg['From'] = fromaddr",
"msg['To']= COMMASPACE.join(toaddrs)",
"server = smtplib.SMTP('00.00.00.000')",
"server.sendmail(fromaddr, toaddrs, msg.as_string())",
"server.quit()")
#send mail
jython.exec(rJython,mail)

POST update to Twitter using Oauth and HTTR, R

I am trying to post an update to Twitter using the api. Following the steps at https://github.com/hadley/httr/blob/master/demo/oauth1-twitter.r, I can get the GET call to work, but I haven't managed to get the POST to work. Here is the relevant code:
library(httr)
library(httpuv)
oauth_endpoints("twitter")
myapp <- oauth_app("twitter",
key = "key"
secret = "secret"
)
twitter_token <- oauth1.0_token(oauth_endpoints("twitter"), myapp)
#this works fine
req <- GET("https://api.twitter.com/1.1/statuses/home_timeline.json",
config(token = twitter_token))
#this doesn't work. Returns error 403: status is forbidden
POST(url = "https://api.twitter.com/1.1/statuses/update.json",
body = "test",
config(token = twitter_token)
)
Any solution that allows a status update (Tweet) from R is acceptable.
The docs say you need to pass a parameter status as a query parameter, not in the body, which makes sense b/c it can only be 140 characters long anyway.
POST("https://api.twitter.com/1.1/statuses/update.json",
query = list(status = "testing from R"),
config(token = twitter_token)
)

Resources