I'm trying to find the way to connect to Appannie's API with R using the httr package (have no experience with API connection at all).
The API requires to include the request header
Citation from appannie's site:
Register an App Annie account and generate an API key.
Add this key to your request header as follows:
Authorization: Bearer '' citation over
I wrote the code which looks like this
query <- "http://api.appannie.com/v1/accounts/1000/sales?break_down=application+dat
&start_date=2012-01-01
&end_date=2012-02-01
¤cy=USD
&countries=US
&page_index=1"
getdata<-GET(url=query, add_headers("Authorization: bearer 811b..."))
the command http_status(getdata) shows me "client error: (401) Unauthorized" can someone please help me with it, what do I do wrong?
HTTP Headers are an important part of the API request and response as they represent the meta-data associated with the API request and response. Headers carry information for: Request and Response Body. Request Authorization.
Some common examples of Request Headers would be: Authorization: Send credentials for basic HTTP authentication to give permission for access. Cache-Control: Tell the browser how long a resource is eligible to be cached and re-used.
Request headersIndicates the content type that is used in the body of the request. The supported content type is XML. application/xml. If-Match. Including If-Match in the header enables optimistic updating with ETag.
You are not specifying the header correctly. add_headers(...)
requires a named list.
library(httr) # for GET(...)
library(rjson) # for fromJSON(...)
query <- "https://api.appannie.com/v1/accounts/1000/sales?break_down=application+dat&start_date=2012-01-01&end_date=2012-02-01¤cy=USD&countries=US&page_index=1"
getdata<-GET(url=query, add_headers(Authorization="bearer <your api key>"))
fromJSON(content(getdata,type="text"))
# $code
# [1] 403
#
# $error
# [1] "Invalid connection account"
This "works" in the sense that I don't get the 401 error. In my case the account 1000 does not exist.
Regarding the http/https issue from the comment, http is despreciated and will no longer be accepted as of 2014-04-01, so you might as well start using https.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With