3

I want to get data from AppAnnie via their API through Powershell.

Authentication is described here: http://support.appannie.com/entries/23215057-2-Authentication

My code looks like:

$apiKey = "someApiKey" $url = "api.appannie.com/v1.1/apps/ios/app/661947195/ranks?start_date=2014-04-23&end_date=2012-04-23&interval=hourly&countries=US+CN" $headers = @{"Authorization" = "Bearer " + $apiKey} $appAnnieResult = Invoke-RestMethod -Uri $url -Headers $headers $appAnnieResult 

The result:

Invoke-RestMethod : Der Remoteserver hat einen Fehler zurückgegeben: (401) Nicht autorisiert. 

What am I missing?

Tried with and without http://, https:// in front of URL

To try it out you'll need an APIKey from AppAnnie (for which you'll need a free account)

Thank you!

Sandro

Update: with the suggest https prefix and extensive logging I get this as header information

Transfer-Encoding=chunked Connection=keep-alive Keep-Alive=timeout=10 Vary=Accept-Language,Cookie Content-Language=en Content-Type=application/json Date=Thu, 24 Apr 2014 16:03:06 GMT Set-Cookie=sessionId="someLongSessionId"; Path=/ Server=nginx sessionId="theSameLongSessionId"= 

The response type now is

"StatusCode": 400, "StatusDescription": "BAD REQUEST" 

And I thought "Bad request" would be worse than "Unauthorized". But it looks like authorization is no longer the issue (there seems to be a session), but then something with my request must be wrong, right? From the given example I only changed the appId and the dates (checked the date for correct position of day and month)

Update 2: Attempts with different parameters, responses as a comment at the end of the lines

$url = "https://api.appannie.com/v1.1/apps/ios/app/848160327/ranks?start_date=2014-04-23&end_date=2012-04-24" #400, bad request $url = "https://api.appannie.com/v1.1/apps/ios/app/848160327/ranks?countries=US+CN" #403, forbidden $url = "https://api.appannie.com/v1.1/apps/ios/app/848160327/ranks?interval=hourly" #403, forbidden $url = "https://api.appannie.com/v1.1/apps/ios/app/848160327/ranks?countries=US+CN&interval=hourly" #403, forbidden $url = "https://api.appannie.com/v1.1/apps/ios/app/848160327/ranks?start_date=2014-04-23&end_date=2012-04-24&interval=hourly" #400, bad request $url = "https://api.appannie.com/v1.1/apps/ios/app/848160327/ranks?start_date=2014-04-23&end_date=2012-04-24&countries=US+CN" #400, bad request 

I think the responses are what is to be expected from the docs, as start_date and end_date are the only required parameters. As long as they are in, it's a bad request (400), if not it becomes a forbidden (403)

1
  • Try taking off any of the optional parameters (only use start_date and end_date) and see if you get the same result. Also if it's still not working update your code above to include any changes you've made. Like you said, usually 400 is better. Commented Apr 24, 2014 at 17:56

2 Answers 2

4

Resolutions

  1. End date needs to be GREATER than start date
  2. I can not retrieve that kind of data for apps, which are not in my account (are not my own)

So all attempts would have been fruitless in some way

Sign up to request clarification or add additional context in comments.

Comments

3

The doc seems to specify that "bearer" is lowercase. Do you get the same error if you don't capitalize that word?

Also, it looks like you might need to encode the header like:

$headers = @{"Authorization"="bearer "+ [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($apiKey))} 

Code to dump response exception details and headers:

try{ $appAnnieResult = Invoke-RestMethod -Uri $url -Headers $headers } catch { write-host (Convertto-Json $_.Exception.Response) $headers = $_.Exception.Response.Headers $cookies = $_.Exception.Response.Cookies $headers |% { write-host "$_=$($headers[$_])"} $cookies |% { write-host "$_=$($cookies[$_])"} } 

And you definitely want to keep the https:// in the uri.

3 Comments

It's different at another point in the docs. Tried both, no difference
Overlooked the encoding bit. Tried that as well now and no change
Looked at the doc again and it seems that appannie is just expecting the API key as text. Not sure exactly what else might be wrong, but sometimes you can get some clues out of the response exception by doing: {Moved code to above}

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.