无法使用R

时间:2019-03-31 20:27:58

标签: r api restful-authentication todoist

可以这么说,我不太擅长使用API​​的“从头开始”。我的问题可能更多是与我对RESTful API的无知有关,而不是与Todoist API有关,但我在与Todoist苦苦挣扎,因为他们的所有文档都是围绕python设计的,我不确定为什么我的尝试失败了。一旦获得连接/身份验证,我会没事的。

Todoist documentation

我使用httr::GET()尝试了几种配置。开始时,请多加点力气。

我尝试过的事情,其中​​key是我的api令牌:

library(httr)
r<-GET("https://beta.todoist.com/API/v8/", add_headers(hdr))

对于hdr,我使用了多种方法:

  • hdr <-paste0(“授权:承载”,密钥)
  • 就是我的钥匙

我也尝试在网址末尾使用projects

1 个答案:

答案 0 :(得分:0)

更新这些现在已在R软件包rtodoist中实现。


我认为除了URL以外,您几乎拥有它了? (或者从那以后可能就改变了)和标题。以下对我有用,将my_todoist_token替换为找到的here的API令牌。

library(jsonlite)
library(httr)
projects_api_url <- "https://api.todoist.com/rest/v1/projects"

# to get the project as a data frame
header <- add_headers(Authorization = paste("Bearer ", my_todoist_token))
project_df <- GET(url = projects_api_url, header) %>%
  content("text", encoding = "UTF-8") %>%
  fromJSON(flatten = TRUE)

# to create a new project
# unfortunately no way to change the dot color associated with project
header2 <- add_headers(
  Authorization  = paste("Bearer ", my_todoist_token),
  `Content-Type` = "application/json",
  `X-Request-Id` = uuid::UUIDgenerate())

POST(url = projects_api_url, header2,
     body = list(name = "Your New Project Name"
                 # parent = parentID
                 ),
     encode = "json")

# get a project given project id
GET(url = paste0(projects_api_url, "/", project_df$id[10]),
    header) %>%
  content("text", encoding = "UTF-8") %>%
  fromJSON(flatten = TRUE)

# update a project 
POST(url = paste0(projects_api_url, "/", project_df$id[10]),
    header2, body = list(name = "IBS-AR Biometric 2019"), encode = "json")