Lending Club API with R - r

I am trying to pull data with Lending Club's API with R:
https://www.lendingclub.com/developers/lc-api.action
but I am unsure how to do it. This is what I have now but I keep getting an unauthorized error. I called Lending Club for API support because it did not specify where to put the API Key, unfortunately they do not have any support for their API. They said all the information is on the website.
I have an account with Lending Club and an API Key.
This is my code, I added an "&api-key=" because I have used something similar for a different API.
library(rjson)
library(RCurl)
library(jsonlite)
apikey <- "pP0tK321JWldXCMYHJ8VmIhMHuM="
url <- "https://api.lendingclub.com/api/investor/v1/loans/listing"
url <- paste0(url,"&api-key=",apikey)
getURL(url)
fromJSON(url)
output:
> getURL(url)
Error in function (type, msg, asError = TRUE) :
SSL certificate problem: self signed certificate in certificate chain
> fromJSON(url)
Error in download_raw(txt) : client error: (401) Unauthorized
If anyone has worked with Lending Club's API with R please give me some guidance. Thank you!
EDIT//
Thanks it works, I have another question regarding the "query" argument. I added a query "showall", but how do I add TRUE?
If you click the following link it will show the query options.
https://www.lendingclub.com/developers/listed-loans.action
rr <- GET("https://api.lendingclub.com/api/investor/v1/loans/listing",
add_headers(Authorization="key"), query = "showall")

I wrote a package to work with the Lending Club API which should make this problem easier for you. Try this:
install.packages("LendingClub")
library(LendingClub)
LC_CRED<- MakeCredential(investorID, APIkey)
ListedLoans(showAll=TRUE)$content
You can see a few more examples by reading the vignette:
vignette("LendingClub")

Getting SSL stuff properly configured with RCurl can be kind of messy. I recommend httr.
Rather than an API key, it looks like the service requires an authorization header. Follow the info on that page to generate one for your account.
Then, when you have the Authortization value, you can make your request like
library(httr)
rr <- GET("https://api.lendingclub.com/api/investor/v1/loans/listing",
add_headers(Authorization="Vkqakl1lAygRyXRwlKCOyHWG4DE"))
Since I don't have an account, i'm not sure what the response will be, but you should be able to access it with
content(rr)

Related

Twitter API v2.0 DELETE request using httr and R

With little experience in API programming, I am looking for a way to send a DELETE or POST request using the Twitter API v2.0 with the httr package in R. I usually use the academictwitteR package to interact with the API, but the package uses a bearer token authentication that seems to me to not be adequate for write privileges (required by DELETE and POST). Therefore, it seemed that the first step is setting up the OAuth 1.0a authentication credintials as described here. I downloaded and stored the four variables (oauth_token; oauth_token_secret; oauth_consumer_key; oauth_consumer_secret) from the app I created, but then I am stuck as to how to set up the request with httr.
I can't provide an example since I could not figure out the code, I hope you understand. Any help is much appreciated!
Not sure about your specific requirements and what you tried before.
Would it be a solution to just use the rtweet package?
It is quite handy and offers quite a lot of functions to interact with the twitter api(also posting and deleting tweets possible)
E.g.
#Posting
post_tweet("hello world")
#Following
post_follow("someuser")
# Not sure about deleting, but should work like this
post_tweet(destroy_id= "postID")
To get you post ID to delete it, you can get maybe use get_my_timeline. This should give your posts with ids.
See here in the vignette for a short intro about functions.
Of course you also need an access token first.
They have a very good explanation page on how to do this. Also a FAQ for problems. The explanation is rather long and probably too specific to go through in detail here. But would be interesting to know if it works for you.
Further things:
Make sure you have httpuv installed
Be sure to have the very latest rtweet version (best is from github I think)
Also check the following: Go to developer.twitter.com/en/apps and then to your app. Under 'Permissions' make sure to give Read and Write. Under 'App Detail' add 127.0.0.1:1410 as Callback Url. Under 'Keys and tokens' create your Keys
Regenerate your tokens/keys
So try this:
install.packages("httpuv")
devtools::install_github("mkearney/rtweet")
library(rtweet)
library(httpuv)
create_token(
app = appname,
consumer_key = key,
consumer_secret = secret,
access_token = access_token,
access_secret = access_secret
)
Update
I just saw, on the very latest version in github they completely changed their methods and create_token is now depreciated.
Here is the new way to do it: documentation
So seems you habe to use rtweet_bot() now.
library("askpass")
rtweet_bot(
api_key = askpass("API key"),
api_secret = askpass("API secret"),
access_token = askpass("access token"),
access_secret = askpass("access token")
)
The rest of the code I posted should stay the same.
In general it seems there are 3 ways of authenticating now:
rtweet_user() interactively authenticates an existing twitter user.
This form is most appropriate if you want rtweet to control your
twitter account.
rtweet_app() authenticates as a twitter application. An application
can't perform actions (i.e. it can't tweet) but otherwise has
generally higher rate limits (i.e. you can do more searches). See
details at
https://developer.twitter.com/en/docs/basics/rate-limits.html. This
form is most appropriate if you are collecting data.
rtweet_bot() authenticates as bot that takes actions on behalf of an
app. This form is most appropriate if you want to create a twitter
account that is run by a computer, rather than a human.
I looked at the curl examples for DELETE and POST requests you provided. They are rewrotten in R below. You need to replace the $OAUTH_SIGNATURE though. Please let me know if it works for you as I cannot check the code as I do not have a Twitter account nor do I have the OAuth token.
library(httr)
r <- DELETE(url = "https://api.twitter.com/2/users/2244994945/following/6253282",
add_headers('Authorization'= 'OAuth $OAUTH_SIGNATURE'))
content(r)
r <- POST(
url = "https://api.twitter.com/2/users/6253282/following",
body = c("target_user_id" = "2244994945"),
add_headers('Authorization'= 'OAuth $OAUTH_SIGNATURE'),
content_type_json()
)
content(r)

Microsoft Graph API - error 403 "Insufficient privileges to complete the operation"

I'm trying to use the AzureR family of R packages to interact with Outlook through the Graph API. Using Microsoft365R I have the following code:
outl <- get_business_outlook(
tenant = tenant_id,
app = client_id,
password = client_secret
)
But this results in a 403 error:
Error in process_response(res, match.arg(http_status_handler), simplify) :
Forbidden (HTTP 403). Failed to complete operation. Message:
Insufficient privileges to complete the operation.
The app in question has the API permissions Mail.ReadWrite, Mail.ReadWriteShared, Mail.Send, Mail.Send.Shared, offline_access, openid, User.Read.
I also tried using the AzureGraph package directly like:
login <- create_graph_login(
tenant = tenant_id,
app = client_id,
password = client_secret
)
This works and I get a token. I then try to extract user information with me <- login$get_user(), but this throws the same 403 error as above. I suspect there is something I need to do to actually authenticate the user, but I can't really figure out what.
I am entirely new to the Graph API so it's very possible that I have missed something obvious. Any help appreciated!
Microsoft365R/AzureGraph author here. In the code you show, both with get_business_onedrive() and create_graph_login(), you are authenticating as the app, not as the user. This means that there is no user account involved, hence you're unable to view user details or send email.
To authenticate as the user, run
# Microsoft365R
get_business_outlook("tenant_id", app="client_id")
# AzureGraph
create_graph_login("tenant_id", app="client_id")
ie, without the password argument. You should know it's working if R opens up a browser window for you to login to Azure (or to show it's successfully logged in).
The latest revision of the AzureAuth package has a vignette that explains a bit more on the various authentication scenarios. AzureAuth::get_azure_token is the underlying function used to obtain an OAuth token by Microsoft365R and AzureGraph, and you can pass down the arguments mentioned in the vignette from get_business_outlook and create_graph_login.

API GET call in R using filter from OData

I am trying to download data in R via an API using OData protocol. I am new to APIs so please bear with me.
I am trying to send a GET command using the following from the API specs:
https://address/v1/meter?$filter=GroupGUID eq guid’ID’
where address is replaced by the URL and ID is replaced by the ID of the meter.
The API uses basic authorization, so I am using the following code from the httr library:
MyUrl <- "https://address/v1/meter?$filter=GroupGUID eq guid'ID'"
MyData <- GET(MyUrl, authenticate("Username", "access code", "basic"))
This gives an status code 400 - The request is badly formed
The authentication works, when I am not using the $filter command.
I have been in touch with the developers of the API and they have confirmed that my GET command is correct. But they are not familiar with R, so they cannot help. I suspect that it may be the space before and after “eq” that is causing the problem.
Can anyone help or point me to a description of using the Odata protocol in R?
Best regards, Rune
I finally figured it out. If I replace space " " with "%20", it works.
This solution is also described her: passing odata $filter in httr GET request

RMonkey: Can't integrate survey data from SurveyMonkey to R, can't get OAuth 2.0

I want to connect the survey responses from my SurveyMonkey survey to R so that these can be analysed in real time as they come in for an RShiny dashboard.
I've loaded the Rmonkey package, and I've created a private app through SurveyMonkey and derived the SurveyMonkey username, password, client ID, SurveyMonkey secret, access token and the OAuth Redirect URL. Below is my code
options(sm_api_key = 'xxxxx')
options(sm_secret = 'yyyy')
options(sm_client_id = 'zzzz')
smlogin()
After running that, we are directed to a web browser and get the following error message: "Survey Monkey The authorization request failed: API key is not required for this app."
We have been using this document for help (https://github.com/cloudyr/Rmonkey) but are concerned that it is from 2016 and thus may be outdated. Any help would be appreciated. Thanks in advance!

OAuth2 GitHub API token

I have a Client ID and Client Secret after having set up an application in github, I'm not sure what the URL or the callback URL is meant to be for that...which i think is causing me problems
I also have a private repo that I would like the application to access...
The way I would like to access the private repo would be via R, so I have found some packages that might help including ROAuth and oauth, but I'm not too sure how to go about using these to get the tokens, as they tend to require a bunch of URLs to make the requests from, and I am unsure as to what the URLs are to get these requests for tokens.
Looking at http://developer.github.com/v3/oauth/ doesn't seem to be amazingly helpful in terms of my inputs for oauth or Oauth2Authorize functions for each of the respective packages.
The end goal is to source files from the private repo, since source_url('private.repo.file.url') doesn't work
I tried the basic authentication using curl through bash, but wasn't able to find a token.
Any walkthrough examples of how to go about doing this would be greatly appreciated.
P.S. this is a follow up question from r sourcing private repos from github
You just need to create an oAuth token at https://github.com/settings/tokens
and get required file via GitHub API using code like below
library(RCurl)
library(devtools)
jsonRawFile <- fromJSON(getURL("https://api.github.com/repos/USERNAME/REPONAME/contents/filename.R",
httpheader = c(Authorization = "token 38ebb0393fe1757ffde9c45d81adzzzzzzzzz",
"User-Agent" = "RCurl"),
.opts = list(ssl.verifypeer = FALSE)))
source_url(jsonRawFile$download_url)
The format of Authorization header should be strictly "token " + your_token_from_account.

Resources