简体   繁体   中英

RESTful API request with “Bearer” authentication for AppAnnie in Powershell

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)

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM