如何正确使用与API数据请求的请求头? [英] How correctly use request header with API data requests?

查看:1649
本文介绍了如何正确使用与API数据请求的请求头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到连接到Appannie,且R使用HTTR包API的方式(与API的连接没有任何经验可言)。
该API需要包括请求头
从appannie的网站引用:
注册一个App安妮帐户,并生成API密钥。
这个键添加到您的请求头如下:结果
授权:承载''
的引文超过

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

我写了code看起来像这样

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..."))

命令HTTP_STATUS(的getData)显示我客户端错误:(401)未经授权
有人可以请帮我一下吧,做我做错了什么?

the command http_status(getdata) shows me "client error: (401) Unauthorized" can someone please help me with it, what do I do wrong?

推荐答案

您没有正确指定的标题。 add_headers(...)要求命名列表。

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"

这个作品,在这个意义上,我没有得到的401错误。在我的情况该帐户1000不存在。

This "works" in the sense that I don't get the 401 error. In my case the account 1000 does not exist.

关于来自注释的HTTP / HTTPS的问题,HTTP是DES preciated,将不再被接受为2014年4月1日,所以你还不如开始使用https。

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.

这篇关于如何正确使用与API数据请求的请求头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆