有人可以指导我如何访问Lufthansa API吗?我已经采取了一个很好的起点,并在每一步都使用检查修改了原始代码行。
步骤1和2似乎有效
library(httr)
library(httpuv)
#Original code taken from
#https://github.com/hadley/httr/blob/master/demo/oauth2-github.r
# 1. Set OAuth settings for dlh:
# http://www.rdocumentation.org/packages/httr/functions/oauth2.0_token.html
dlh = oauth_endpoint(
base_url = "https://api.lufthansa.com/v1/oauth",
request = NULL,
authorize = "authorize",
access = "token"
)
# 2. Register an application
# Replace key and secret below.
# http://www.rdocumentation.org/packages/httr/functions/oauth_app
myapp <- oauth_app("SampleCabin",
key = "****",
secret = "****")
但后来我遇到了问题
options(httr_oauth_cache=TRUE, httr_oob_default=TRUE)
# 3. Get OAuth credentials
# http://www.rdocumentation.org/packages/httr/functions/oauth2.0_token
#Sample
#oauth2.0_token(endpoint, app, scope = NULL, user_params = NULL,
#type = NULL, use_oob = getOption("httr_oob_default"), as_header = TRUE, use_basic_auth = FALSE, cache = getOption("httr_oauth_cache"))
dlh_token <- oauth2.0_token(dlh, myapp,
scope = "https://api.lufthansa.com/v1/oauth/token",
use_oob = getOption("httr_oob_default"),
use_basic_auth = FALSE#,
)
我收到以下消息。
#Please point your browser to the following url:
#https://api.lufthansa.com/v1/oauth/authorize?client_id=****&scope=https%3A%2F%2Fapi.lufthansa.com%2Fv1%2Foauth%2Ftoken&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&response_type=code
#Enter authorization code:
现在我认为上面的代码行是获取令牌。相反,在这一点上,我必须手动粘贴从范围URL中取得的令牌。我在这里做错了什么?
之后是错误消息。
#Error in init_oauth2.0(self$endpoint, self$app, scope = self$params$scope, Bad Request (HTTP 400).
我计划跟随以下内容......
# 4. Use API
req <- GET("https://api.lufthansa.com/v1/operations/flightstatus/route/TLS/FRA/2016-02-15",config(token = dlh_token))
stop_for_status(req)
content(req)
这是我第一次尝试访问除了以下练习之外的API。为了让事情更有趣,我想我需要在这个过程的某个地方添加一个代理地址(我认为是第4步)。
任何想法??
史蒂夫
答案 0 :(得分:0)
不授权和访问必须是URL吗?来自oauth_endpoint帮助文件:
授权
url发送客户端进行授权
访问
url用于交换未经身份验证的身份验证令牌。