R - 对Euromonitor的API请求

问题描述 投票:0回答:1

鉴于API规范......

POST https://api.euromonitor.com/authentication/connect/token HTTP/1.1

Host: api.euromonitor.com

Content-Type: application/x-www-form-urlencoded

Ocp-Apim-Subscription-Key: xxxyyyzzz

grant_type=string&[email protected]&password=yyyzzz

...如何将subscription-key和grant_type参数合并到R程序中以请求数据?

我假设我发送了一个Post请求,但到目前为止我所尝试的所有内容(如下所示)都会导致400 Bad Request错误。

url <- "https://api.euromonitor.com/authentication/connect/token HTTP/1.1"

body_list <- list(subs_key="xxxyyyzzz", 
                  grant_type="string&[email protected]&password=yyyzzz")

r <- POST(url, body=body_list, encode="form", verbose())

更新:

仔细阅读Euromonitor文档会发现grant_type在正文中,其余参数在标题中。我已经改变了我的程序:

url <- "https://api.euromonitor.com/authentication/connect/token HTTP/1.1"
r <- POST(url, add_headers(.headers=c("Host"="api.euromonitor.com",
                           "Content-Type"="application/x-www-form-urlencoded",
                           "Ocp-Apim-Subscription-Key"="xxxyyyzzz")),
      body="grant_type=password&[email protected]&password=yyyzzz", verbose())

但我仍然收到400 Bad Request错误:

-> POST /authentication/connect/token HTTP/1.1 HTTP/1.1
-> Host: api.euromonitor.com
-> User-Agent: libcurl/7.59.0 r-curl/3.2 httr/1.4.0
-> Accept-Encoding: gzip, deflate
-> Accept: application/json, text/xml, application/xml, */*
-> Content-Type: application/x-www-form-urlencoded
-> Ocp-Apim-Subscription-Key: xxxyyyzzz
-> Content-Length: 69
-> 
>> grant_type=password&[email protected]&password=yyyzzz

<- HTTP/1.1 400 Bad Request
<- Content-Type: text/html; charset=us-ascii
<- Date: Tue, 12 Mar 2019 02:55:39 GMT
<- Connection: close
<- Content-Length: 311
r api parameters
1个回答
0
投票

对于您要执行此操作的记录:

url <- "https://api.euromonitor.com/authentication/connect/token"
response <- POST(url, add_headers(.headers=c("Ocp-Apim-Subscription-Key"="xxxyyyzzz",
                                  "Content-Type"="application/x-www-form-urlencoded")),
      body="grant_type=password&[email protected]&password=yyyzzz")
token <- content(response, "text")
© www.soinside.com 2019 - 2024. All rights reserved.