使用R请求JWT访问令牌

时间:2019-02-21 19:33:34

标签: r jwt

我正在尝试通过R中的Box API使用JWT获得OAuth访问令牌。

我已经在R中的指令中复制了python代码,但是当我去请求访问令牌时,它一直给我400错误。这是我到目前为止的代码:

library(curl)
library(httr)
library(openssl)
library(jose)
library(jsonlite)

# Read JSON config
config <- jsonlite::fromJSON("...config.json")

# Decrypt private key
appAuth <- config$boxAppSettings$appAuth
privateKey <- appAuth$privateKey
passphrase <- appAuth$passphrase

key <- read_key(privateKey, password = passphrase)

# Create JWT assertion

claims <- jwt_claim(iss = config$boxAppSettings$clientID,
          sub = config$enterpriseID,
          box_sub_type = 'enterprise',
          aud = authentication_url, 
          exp = as.numeric(Sys.time()) + 120,
          jti = paste(as.character(rand_bytes(64)), sep = "", collapse = ""))

keyId <- appAuth$publicKeyID

assertion <-
  jwt_encode_sig(
    claim = claims,
    key = key,
    size = 512,
    header = list('kid' = keyId)
  )

# Request Access Token

authentication_url <- 'https://api.box.com/oauth2/token'

url <- parse_url(authentication_url)

params <- list('grant_type'= 'urn:ietf:params:oauth:grant-type:jwt-bearer',
                'assertion' = assertion,
                'client_id' = config$boxAppSettings$clientID,
                 'client_secret' = config$boxAppSettings$clientSecret)

url$query <- params

url_full <- build_url(url)

httr::GET(url_full)

有人知道如何进行这项工作吗?

0 个答案:

没有答案