r - googleUser.getAuthResponse().id_token 不返回 Shiny 的 id_token

标签 r shiny google-oauth google-sheets-api google-authentication

我正在尝试创建一个 Shiny 的应用程序,该应用程序首先使用 OAuth 进行授权(请参阅 https://developers.google.com/identity/sign-in/web/sign-in ),然后获取 token 并使用 googlesheets4 访问 Google 表格。图书馆。登录过程正常工作,这意味着我可以看到我的电子邮件(以及来自 googleUser.getBasicProfile() 的其他数据。似乎没有返回 id_token。最小示例:
app.R

library(shiny)
library(shinyjs)
library(googlesheets4)

shinyApp(
  ui = fluidPage(
    useShinyjs(),  
    tags$head(tags$script(src="https://apis.google.com/js/platform.js")),
    tags$meta(name = "google-signin-scope", content = "profile email"),
    tags$meta(name = "google-signin-client_id", content = "<CLIENT_ID>.apps.googleusercontent.com"),
    includeScript("signin.js"),
    div(id = "signin", class = "g-signin2", "data-onsuccess" = "onSignIn"),
    actionButton("signout", "Sign Out", onclick="signOut();", class="btn-danger"),

    with(tags, dl(dt("Email"), dd(textOutput("g.email")),
                  dt("Token"), dd(textOutput("g.id_token"))
                  )),

    tableOutput("df")
  ),


  server = function(input, output) {

    output$g.email = renderText({ input$g.email }) # here my email is printed in the app
    output$g.id_token = renderText({ input$g.id_token}) # no id_token available?

    output$df <- renderTable({
      sheets_auth(token = input$g.id_token)
      read_sheet("<GOOGLE_SHEET_ID>")
      })
  }
)

singin.js
function onSignIn(googleUser) {
  var profile = googleUser.getBasicProfile();
  Shiny.onInputChange("g.email", profile.getEmail());
  var id_token = googleUser.getAuthResponse().id_token;
  Shiny.onInputChange("g.id_token", id_token);
}

function signOut() {
  var auth2 = gapi.auth2.getAuthInstance();
  auth2.signOut();
  Shiny.onInputChange("g.email", null);       
  Shiny.onInputChange("g.id_token", null);
}

我如何访问 id_token并将其传递给 sheets_auth功能?

最佳答案

在浏览器控制台中执行以下命令以验证它是否返回实际 token 。

  console.log(JSON.stringify(gapi.auth2.getAuthInstance().currentUser.get().getAuthResponse()));

{ "token_type":"Bearer", "login_hint":"<Huge mess of letters>", "expires_in":2112, "id_token":"<insert your ridiculously long string here>",...}

另一个使用 true 标志登录到验证:
console.log(JSON.stringify(gapi.auth2.getAuthInstance().currentUser.get().getAuthResponse(true)));

    { "token_type":"Bearer", "access_token":"<an actual access token goes here>", "scope":"<whatever scopes you have authorized>", "login_hint":"<another mess of letters>", "expires_in":2112, "id_token":"<Insert your ridiculously long string here>", ...}

仔细检查:有时如果我们不发送范围, token ID 可能不存在,您可以通过解决这个问题
gapi.auth2.init({
  client_id: <googleClientID>,
  'scope': 'https://www.googleapis.com/auth/plus.login'
})

来源:GoogleUser.getAuthResponse() doesn't contain access_token

关于r - googleUser.getAuthResponse().id_token 不返回 Shiny 的 id_token,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58882481/

相关文章:

javascript - 在 rpivottable 中分组排序

r - 如何使用 dplyr 选择每组中具有最大值的行?

r - 使用grep帮助在R中子集数据帧

RStudio 挂起,无法在 Ubuntu 22.04LTS 上启动(R 从源代码编译)

html - 如何使用 Shiny 的 html 格式化文本?

r - Shiny :在导体执行时防止端点中的初始错误消息

r - 无法使用 Shiny 的 actionButton 和 eventReactive 方法在 flexdashboard 的 tabset 面板中获得预测图

javascript - Google OAuth2 - 交换 token 的访问代码 - 不起作用

python - 获取 Google OAuth2 凭据或从承载类型 token 刷新 token ?

vue.js - 此消息类型不允许使用参数 : code_challenge_method, 如何在 nuxt 中修复它?