New to API; Trying to get info from API of IQAir but Incorrect API Key in RStudio - r

I was trying to get the content from an API (with key) from IQAir. The documentation of the API can be found via this link [api documentation][1].
I am using RStudio and new to using API. The code I use is as follow:
myurl <- paste0("http://api.airvisual.com/v2/countries?key=", Sys.getenv("my_key"))
my_raw_result <- httr::GET(myurl)
str(my_raw_result$content)
mycontent <- httr::content(my_raw_result, as = "text")
mycontent.json <- jsonlite::fromJSON(mycontent)
dplyr::glimpse(mycontent.json)
And the screenshot for the documentation is
[documentation screenshot][2]
I got an error message as below:
> dplyr::glimpse(mycontent.json)
List of 2
$ status: chr "fail"
$ data :List of 1
..$ message: chr "incorrect_api_key"
Could someone help with this? Thank you very much!
[1]: https://api-docs.iqair.com/?version=latest
[2]: https://i.stack.imgur.com/cRSiO.png

Related

rscopus package on R in MacBook - Invalid API key error

I am trying to use the Scopus API for the first time. I have the API key and the institution token. However, I am still getting an error, when I try to use it in R on my Mac. Here is my code:
library(rscopus)
set_api_key(MY_KEY)
hdr=inst_token_header(MY_TOKEN)
key=get_api_key()
print(rscopus::get_api_key(), reveal=TRUE)
have_api_key()
auth_info = process_author_name(last_name="Muschelli", first_name="John", verbose=FALSE)
The error message is:
> library(rscopus)
>
> set_api_key(MY_KEY)
> hdr=inst_token_header(MY_TOKEN)
> key=get_api_key()
> print(rscopus::get_api_key(), reveal=TRUE)
[1] "MY_KEY"
> have_api_key()
[1] TRUE
>
> if (have_api_key()) {
+ auth = elsevier_authenticate(api_key=key)
+ }
HTTP specified is: https://api.elsevier.com/authenticate
Warning message:
In elsevier_authenticate(api_key = key) : Forbidden (HTTP 403).
> auth_info = process_author_name(last_name="Muschelli", first_name="John", verbose=FALSE)
$`service-error`
$`service-error`$status
$`service-error`$status$statusCode
[1] "AUTHENTICATION_ERROR"
$`service-error`$status$statusText
[1] "Invalid API Key: valid apikey credentials required."
Error in get_complete_author_info(...) : Service Error
I tried
if (have_api_key()) {
auth = elsevier_authenticate(api_key=key)
}
but I get the error:
HTTP specified is: https://api.elsevier.com/authenticate
Warning message:
In elsevier_authenticate(api_key = key) : Forbidden (HTTP 403).
I have tried using auth_token_header(MY_TOKEN) instead of inst_token_header(MY_TOKEN) but the code is still not working.
I have also taken the following step in my terminal:
export Elsevier_API=MY_KEY > ~/.bash_profile
source ~/.bash_profile
I am still getting the error. However, the combination of key and institution token work here: https://dev.elsevier.com/scopus.html
Can anyone please help me debug this issue?
Thank You!
I figured out the issue. So, the correct way to query would be to pass the headers argument as well:
auth_info = process_author_name(last_name="Muschelli", first_name="John", verbose=FALSE, headers=hdr)
And now the code will work! :)

Error when trying to prase a HTTP-Request in R

im using R package httr to get a HTTP-Response for a specific link.
When trying to parse the content of the response im getting the Error:
Fehler in parse(text = script_content) : <text>:1:10: Unerwartete(s) '['
1: {"lines":[
Translated to enlgish it says something like this (sorry for my error messages being in German):
Error in parsing(text = script_content) : <text>1:10: Unexpected '['
1: {"lines":[
It seems as there is a problem with the format/encoding of the text. Here is my code:
script <-
GET(
url = "https://my_url.which_origin_is_not_important/my_script.R",
authenticate(username, pass)
)
script_content <- content(script, as = "text", encoding = "ISO-8859-1")
parsed_condent <- parse(text = script_content )
The value of script_content looks like this:
"{\"lines\":[{\"text\":\"################## FUNCTION ##################\"},{\"text\":\"\"},{\"text\":\"library(log4r)\"}],\"start\":0,\"size\":32,\"isLastPage\":true,\"limit\":500,\"nextPageStart\":null}"
Some more background to this operation: Im trying to source a code, which is currently inside of a private repository. I wrote the code myself i'm trying to source. I made sure, that the issue is not coming from within th code.
I got the solution from: Sourcing R files in a private github folder
Thanks for any advice!!

read informations from Web Feature Service (WFS) with R

I like to use a Web Feature Service (WFS) [1] to get soil informations. But I am very unsure how to do it right, maybe you can give me a hint on how to get all available informations.
I can query bmf:Musterstueck and convert it to a Simple Feature. But if I do the same with so:SoilHorizon or so:ObservedSoilProfile I get the error message like
Error: Cannot open "https://via.bund.de/bmf/inspire/so/wfs?service=wfs&version=2.0.0&request=GetFeature&typename=so%3ASoilHorizon"; The file doesn't seem to exist.
In addition: Warning message:
In CPL_read_ogr(dsn, layer, query, as.character(options), quiet, :
GDAL Error 1: HTTP error code : 403
Thank you
Christof
[1] https://via.bund.de/bmf/inspire/so/wfs?service=wfs&request=getcapabilities
name title
bmf:Musterstueck Musterstücke der Bodenschätzung
so:SoilHorizon INSPIRE SoilHorizon
so:ObservedSoilProfile INSPIRE ObservedSoilProfile
library(tidyverse)
library(sf)
library(ows4R)
library(httr)
wfs = WFSClient$new("https://via.bund.de/bmf/inspire/so/wfs", "2.0.0", logger = "INFO")
wfs$getFeatureTypes(pretty = TRUE)
url <- parse_url("https://via.bund.de/bmf/inspire/so/wfs")
url$query <- list(service = "wfs",
version = "2.0.0",
request = "GetFeature",
typename = "bmf:Musterstueck")
request <- build_url(url)
request
bmf_musterstueck <- read_sf(request)
bmf_musterstueck

Post API request in R

I want to use textrazor RET API in R (https://www.textrazor.com/docs/rest).
code bellow return 400 status and error
No encoding supplied: defaulting to UTF-8.
[1] "{\"ok\":false, \"time\":0, \"error\":\"Please provide a TextRazor API Key in either the X-TextRazor-Key header, or apiKey POST Param\"}\n"
My code :
base <- "http://api.textrazor.com/"
tekst <- "Stagnacja, znużenie i brak możliwości rozwoju działają frustrująco."
pobranie_api7 <- POST(url=base,extractors="words", text=tekst)

persian arguments of geocode doesn't work in persian system locale

I have a list of some districts in the city of Tehran which are saved in Persian in a CSV file. My first goal is to find these districts lat-lon. In order to load them correctly, I should set the system's locale to "Persian"
Sys.setlocale(locale = "persian")
but, I have found that when I try to use them as arguments in functions like geocode() from ggmap library, it doesn't work!
>geocode(c[1])
Information from URL : http://maps.googleapis.com/maps/api/geocode/json?
address=%C2%CC%E6%CF%C7%E4%ED%E5&sensor=false
lon lat
1 NA NA
Warning messages:
1: In readLines(connect, warn = FALSE) :
cannot open URL 'http://maps.googleapis.com/maps/api/geocode/json?
address=%C2%CC%E6%CF%C7%E4%ED%E5&sensor=false': HTTP status was '400 Bad
Request'
2: In geocode(c[1]) : geocoding failed for "آجودانيه".
if accompanied by 500 Internal Server Error with using dsk, try google.
here c is my vector of Persian districts.
on the other hand, when I change system locale to English and type the district in Persian in geocode(), it works correctly.
>geocode("آجودانیه")
Information from URL : http://maps.googleapis.com/maps/api/geocode/json?address=%D8%A2%D8%AC%D9%88%D8%AF%D8%A7%D9%86%DB%8C%D9%87&sensor=false
lon lat
1 51.4861 35.80742
how can I find these districts lat-lon?
as a summary, when system locale is in Persian I receive NAs and when I return locale to English it works correctly.
I've built a worldwide geocoding api and I'm happy to notice it works either way in your case:
https://geocode.xyz/%D8%A2%D8%AC%D9%88%D8%AF%D8%A7%D9%86%D9%8A%D9%87
- Iran x,y z: 35.79860,51.47870 🇮🇷
- Ajwdnyh, IR Iran » Confidence Score: 0.90
http://geocode.xyz/%D8%A2%D8%AC%D9%88%D8%AF%D8%A7%D9%86%DB%8C%D9%87
- 🇮🇷
- آجودانیه, IR Iran » Confidence Score: 0.90
Or if you prefer XML output : http://geocode.xyz/%D8%A2%D8%AC%D9%88%D8%AF%D8%A7%D9%86%DB%8C%D9%87?geoit=xml
<geodata>
<latt>35.79860</latt>
<longt>51.47870</longt>
<elevation/>
<standard>
<addresst/>
<postal/>
<prov>IR</prov>
<city>ajwdnyh</city>
<countryname>Iran</countryname>
<confidence>0.90</confidence>
</standard>
<alt></alt>
</geodata>
.. and json : http://geocode.xyz/%D8%A2%D8%AC%D9%88%D8%AF%D8%A7%D9%86%DB%8C%D9%87?geoit=json
{
"standard" : {
"addresst" : {},
"city" : "ajwdnyh",
"prov" : "IR",
"countryname" : "Iran",
"postal" : {},
"confidence" : "0.90"
},
"longt" : "51.47870",
"alt" : {},
"elevation" : {},
"latt" : "35.79860"
}
PS. You may also upload your CSV file for batch geocoding here: https://geocode.xyz/batch

Resources