10

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
&currency=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?

andrew-zmeul
  • 121
  • 1
  • 1
  • 10
  • did you try "Bearer" (capital B)? – Jasper Mar 26 '14 at 17:10
  • I did, the same result – andrew-zmeul Mar 26 '14 at 17:13
  • 1
    "*The API is stateless, and every request requires authentication. All API requests use API key bearer authentication, and only accept requests made by **HTTPS**.*" via http://support.appannie.com/entries/23215057-3-Authentication. Your `query` starts with `http`. That is probably the issue. – hrbrmstr Mar 26 '14 at 17:32

1 Answers1

12

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&currency=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.

jlhoward
  • 58,004
  • 7
  • 97
  • 140
  • I tried this one added the https and replaced the account id for a right one but still has 403 error. Do I have to be a Super Admin in appannie to do it? – andrew-zmeul Mar 26 '14 at 21:23
  • and in this case for downloads' page in Analytics section: https://www.appannie.com/dashboard/1000/item/396958695/downloads/ is the "1000" - the account? – andrew-zmeul Mar 26 '14 at 21:28
  • According to their [App Sales support page](http://support.appannie.com/entries/23215097-2-API-data-App-Sales), you have to provide both an accountID, and an appID, which you don't seem to do. If you don't know how to use their API, you should contact APP Annie Customer Support. The solution above allows you to connect to their API. It's up to you to provide the correct query string. – jlhoward Mar 26 '14 at 22:26
  • I managed to made it work, had to fix the example from the site little bit, but it works. Thank you! – andrew-zmeul Mar 27 '14 at 06:39