Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何用R语言生成JWS_R_Google Oauth_Jwt - Fatal编程技术网

如何用R语言生成JWS

如何用R语言生成JWS,r,google-oauth,jwt,R,Google Oauth,Jwt,我尝试为google-oauth2.0ServiceAccount生成JWT 我设置了收割台和有效载荷(索赔)。但当我试图用RSASA256签署base64header.base64claim时,我得到了错误的签名。 我在PKI包中发现只有一个函数允许使用指定的哈希函数与RSA对contet进行签名 我怎么知道我的签名不正确?我发现它可以从输入和私钥生成JWT 所以我所能看到的是,我的R函数签名不同于jwt.io签名。 我已经用这两个JWT令牌测试了对的请求,JWT.io一个正在工作 本部分适用

我尝试为google-oauth2.0ServiceAccount生成JWT

我设置了收割台和有效载荷(索赔)。但当我试图用RSASA256签署base64header.base64claim时,我得到了错误的签名。 我在PKI包中发现只有一个函数允许使用指定的哈希函数与RSA对contet进行签名

我怎么知道我的签名不正确?我发现它可以从输入和私钥生成JWT

所以我所能看到的是,我的R函数签名不同于jwt.io签名。 我已经用这两个JWT令牌测试了对的请求,JWT.io一个正在工作

本部分适用于JWT收割台

library(base64enc)
library(jsonlite)
library(PKI)
#JWT header set up
    alg <- "RS256"
    typ <- "JWT"
    header <- list("alg" = alg, "typ" = typ)
    h <- toJSON(header, auto_unbox=TRUE)
    enc.header <- base64encode(charToRaw(h))
库(base64enc)
图书馆(jsonlite)
图书馆(PKI)
#JWT头设置

alg我终于找到了一个有问题的地方。 一直以来都是关于Base64编码的。 我已经检查了正确和正确的JWTs,发现了一些模式。 不正确的一个在有效负载和签名中有
“==”
,我已将其替换为
。 此外,在签名中,我将所有
“/”
替换为
“\u”
,将所有
“+”
替换为
“-”
。 希望它能给有同样问题的人一个提示。

这是用于获取令牌的代码,该令牌适用于一些谷歌API(但不适用于我需要的云。如果您可以使用,请告诉我)。此外,httroauth_服务_令牌比编写自己的代码更容易使用

init_oauth_service_account <- function(endpoint, secrets, scope = NULL) {
  signature <- jwt_signature(secrets, scope = scope)

  res <- POST(endpoint$access, body = list(
    grant_type = "urn:ietf:params:oauth:grant-type:jwt-bearer",
    assertion = signature
  ), encode = "form")
  stop_for_status(res)

  content(res, type = "application/json")
}
...
jwt_base64 <- function(x) base64url(jwt_json(x))
jwt_json <- function(x) jsonlite::toJSON(x, auto_unbox = TRUE)
base64url <- function(x) {
  if (is.character(x)) {
    x <- charToRaw(x)
  }
  out <- chartr('+/', '-_', base64enc::base64encode(x))
  gsub("=+$", "", out)
}

init\u oauth\u服务\u帐户访问有用链接。但有一件事,我没有发现如何为这个函数设置代理使用。我工作场所的防火墙不允许我在没有代理设置的情况下发送请求。在httr::POST中,我能够将代理设置设置为配置<代码>输出
y <- file("~/keys/euroset-test-70c2d0d4eed1.pem")
key <- PKI.load.key(y)
what <- paste(enc.header,enc.claim, sep=".")
JWS <- PKI.sign(what, key, "SHA256")
enc.sign <- base64encode(JWS)
JWT <- paste(what,enc.sign, sep=".")
JWT
init_oauth_service_account <- function(endpoint, secrets, scope = NULL) {
  signature <- jwt_signature(secrets, scope = scope)

  res <- POST(endpoint$access, body = list(
    grant_type = "urn:ietf:params:oauth:grant-type:jwt-bearer",
    assertion = signature
  ), encode = "form")
  stop_for_status(res)

  content(res, type = "application/json")
}
...
jwt_base64 <- function(x) base64url(jwt_json(x))
jwt_json <- function(x) jsonlite::toJSON(x, auto_unbox = TRUE)
base64url <- function(x) {
  if (is.character(x)) {
    x <- charToRaw(x)
  }
  out <- chartr('+/', '-_', base64enc::base64encode(x))
  gsub("=+$", "", out)
}