我正在尝试创建一个 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/