在没有浏览器的情况下在 AWS Ubuntu 上对谷歌表进行身份验证 [英] Authenticating google sheets on AWS Ubuntu without browser
问题描述
我在 AWSUbuntu Server 12.04.2"上运行 R Studio,并通过浏览器访问 R Studio.
I'm running R Studio on an AWS "Ubuntu Server 12.04.2" and accessing R Studio via my browser.
当我尝试使用带有代码的 googlesheets 包对 google auth API 进行身份验证时:gs_auth(token = NULL, new_user = FALSE,key = getOption("googlesheets.client_id"),secret = getOption("googlesheets.client_secret"),cache = getOption("googlesheets.httr_oauth_cache"), verbose = TRUE)
When I try to authenticate google auth API using the package googlesheets with the code:
gs_auth(token = NULL, new_user = FALSE,
key = getOption("googlesheets.client_id"),
secret = getOption("googlesheets.client_secret"),
cache = getOption("googlesheets.httr_oauth_cache"), verbose = TRUE)
这里的问题是它将我重定向到本地机器(基于 Windows)的浏览器.即使我授权它,它也会重定向到类似http://localhost:1410/?state=blahblah&code=blahblah".
The problem here is that it redirects me to browser which is of local machine (windows based). Even if I authorize it, it redirects to a URL like "http://localhost:1410/?state=blahblah&code=blahblah".
在这种情况下我如何授权 googlesheets?
How do I authorize googlesheets in such case?
我什至尝试从我的 Windows 机器转移现有的 httr-oauth 令牌以删除 ubuntu 服务器.
I have even tried transfering existing httr-oauth token from my windows machine to remove ubuntu server.
推荐答案
从服务器创建 gs_auth
令牌的最简单方法是将 httr_oob_default
选项设置为 true,这将告诉 httr 使用带外方法进行身份验证.您将获得一个 URL,并希望返回一个授权代码.
The simplest way to create a gs_auth
token from a server is to set the httr_oob_default
option to true, which will tell httr to use the out of band method for authenticating. You will be given a URL and expected to return an authorization code.
library(googlesheets)
options(httr_oob_default=TRUE)
gs_auth(new_user = TRUE)
gs_ls()
当您设置 httr_oob_default
选项时 httr 所做的一件事是将 URI 重新定义为 urn:ietf:wg:oauth:2.0:oob
,如代码所示oauth-init.
One thing httr does when you set the httr_oob_default
option is to redefine the URI to urn:ietf:wg:oauth:2.0:oob
as seen in the code for oauth-init.
或者,您可以使用 httr 命令手动创建 .httr-oauth
令牌.通过在 oauth2.0_token
命令中设置 use_oob=TRUE
来使用带外认证模式.
Alternatively, you can create a .httr-oauth
token manually using httr commands. Use the out of band authentication mode by setting use_oob=TRUE
in the oauth2.0_token
command.
library(googlesheets)
library(httr)
file.remove('.httr-oauth')
oauth2.0_token(
endpoint = oauth_endpoints("google"),
app = oauth_app(
"google",
key = getOption("googlesheets.client_id"),
secret = getOption("googlesheets.client_secret")
),
scope = c(
"https://spreadsheets.google.com/feeds",
"https://www.googleapis.com/auth/drive"),
use_oob = TRUE,
cache = TRUE
)
gs_ls()
另一种不太优雅的解决方案是在桌面上创建 .httr-oauth
令牌,然后将文件复制到服务器.
Another, less elegant, solution is to create the .httr-oauth
token on your desktop and then copying the file to a server.
这篇关于在没有浏览器的情况下在 AWS Ubuntu 上对谷歌表进行身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!