Using NSW Data with R

How to read NSW Data using R. For example, to get an authorization token we tried the following:

library(RCurl)
client_id<-“xxxxxxxxxxxxxxxxxxxx”
client_secret<-“xxxxxxxxxxxxxxx”
id_secret <- base64(paste(client_id,client_secret,sep=":"))[[1]]
my_headers <- add_headers(
‘Authorization’=paste(“Basic”, id_secret, sep=" " ),
‘Content-Type’=‘application/json’
)
my_body <- ‘grant_type=client_credentials’
my_token <- POST(“https://api.onegov.nsw.gov.au/oauth/client_credential/accesstoken”,
my_headers, body=my_body)
access_token<-content(my_token)$access_token

…but unfortunately without success.

it will depend on the API you are trying to access. which dataset are you trying to access?

Hi Jayen,
Thanks for your interest. We are trying to create an authentication token in R to access the NSW Fuel API as described on their website at:
https://api.nsw.gov.au/fuel-price-check/apis/get/oauth/client_credential/accesstoken
We can get our token both using the NSW.DATA API page above and also using an independent API Tester but not yet in R alone. The R code above is useful in order to implement more frequent requests. Doing this manually each time is not viable. Appreciate the help.
Andrew

Sorry, discourse didn’t notify me of your reply.

I don’t know R but that page does a GET not POST. Try this:

library(httr)
library(RCurl)
client_id<-"xxxxxxxxxxxxxxxxxxxx"
client_secret<-"xxxxxxxxxxxxxxx"
id_secret <- base64(paste(client_id,client_secret,sep=":"))[[1]]
my_headers <- add_headers(
                'Authorization'=paste("Basic", id_secret, sep=" " ),
                'Content-Type'='application/json'
              )
my_token <- GET("https://api.onegov.nsw.gov.au/oauth/client_credential/accesstoken?grant_type=client_credentials",
                my_headers)
access_token<-content(my_token)$access_token

Hi Jayen,
finally got back to this myself and can confirm that your suggestion works.The trick was to move the grant_type from the body to the url…and to use GET, of course.
Thanks and Regards
Andrew
www.connectingthedots.com.au

© Data.NSW