Lending Club API with R

2019-06-09 03:48发布

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")

2条回答
再贱就再见
2楼-- · 2019-06-09 04:18

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)
查看更多
你好瞎i
3楼-- · 2019-06-09 04:20

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")
查看更多
登录 后发表回答