在 Google 表格应用程序中进行身份验证

2024-06-19

我有一个类似批处理的应用程序,由调度程序定期调用,无需人类用户参与。它使用 PerlNet::Google::电子表格 http://metacpan.org/pod/Net::Google::Spreadsheets包通过从数据库获取的数据来更新 Google-sheets 电子表格中的某些单元格。

长期以来,只需向包的“新”方法提供用户名和密码即可简单地进行自身身份验证。但最近,Google 要求我们使用 OAuth2 协议进行身份验证。

J.T. https://stackoverflow.com/users/4943604/j-t假如一个办法 https://stackoverflow.com/questions/30476405/netgoogleauthsub-login-failed-with-new-google-drive-version我相信这对许多比我更有知识的人来说非常有帮助。但是,如果有人可以回答一些问题来澄清,我将不胜感激,如下所示:

  1. 创建凭据:在 Google 开发者控制台中创建项目并要求创建新的客户端 ID 后,您会看到三个选项:

    • 对于“Web 应用程序”(然后要求提供“授权的 JavaScript 来源”和“授权的重定向 URI”。这些与我的情况相关吗?)
    • 对于“服务帐户”(我怀疑这是我的选择,但如果没有回答以下问题,我无法验证它。)
    • 对于“已安装的应用程序”(可以举例说明吗?)

    我应该选择哪一个5月申请?

  2. 我应该请求 JSON 还是 P12 密钥?

  3. 我该如何处理我获得的各种类型的实体?我应该在 Perl 脚本中嵌入什么?

  4. 在第 13 行,J.T 评论说“您需要在此处放置代码并接收令牌”。什么样的代码?做什么?

  5. 由于没有人类用户,我需要应用程序自己完成完整的身份验证过程。 J.T. 的代码提示用户输入“代码”。该代码是“凭据”实体之一吗?我该怎么做?

换句话说,我需要有人轻轻地引导我一步一步地完成整个过程。

谢谢各位!

MeirG


我也必须经历这个,一开始并不了解太多,所以我很乐意帮助解释。以下是答案,但请随时要求澄清。基本上,您需要首先运行一个需要手动干预的脚本 - 这可以让您从 Google 获取访问令牌,然后您的批处理脚本可以反复使用该令牌,而无需人工干预。因此,一开始你必须克服一些困难,但一旦完成,一切就都准备好了。所以:

  1. 选择“网络应用程序”。不直观,但它会起作用。

1b.系统会要求您配置“同意屏幕”。你在这里放什么并不重要——只要给项目一个标题即可。

1c.对于“重定向 uri”,删除提供的“example.com”值并输入“https://developers.google.com/oauthplayground”。

忽略 JSON 和 P12 键;它们适用于其他类型的应用程序。填写上述信息并单击“创建客户端 ID”后,您将看到一个显示客户端 ID 和客户端密钥的页面(暂停后)。这是您在下面的代码中需要的两个字符串。

下面的代码本质上与您上面链接的解决方案相同(我非常依赖它),但我对其进行了编辑以更改一些内容,主要是为了提供有关正在发生的情况的更多信息。将客户端 ID 和客户端密钥添加到下面的代码后,运行它。然后您将执行以下步骤:

  1. 复制脚本打印出的 URL,并将其粘贴到浏览器中。
  2. 如果 Google 要求您登录,请登录。然后单击下一页上的“允许访问”。
  3. 在浏览器的下一个页面上,左侧将有一个标有“授权代码”的框。 (像这样:https://members.orcid.org/sites/default/files/image06.png https://members.orcid.org/sites/default/files/image06.png但您的授权码会更长。)Don't单击代码下方的按钮,但一定要复制该字符串,并确保获得整个内容(可能会在对话框中超出视线)。
  4. 返回运行脚本的终端,然后粘贴您复制的代码。

如果一切顺利,脚本将将该代码交换为访问令牌,并将该令牌保存在磁盘上。然后您的批处理脚本可以重复使用该令牌。

这是完成所有这些操作的扩展代码:

#!/usr/bin/perl

# Code to get a web-based token that can be stored 
# and used later to authorize our spreadsheet access. 

# Based on code from https://gist.github.com/hexaddikt/6738162

#-------------------------------------------------------------------

# To use this code:

# 1. Edit the lines below to put in your own
#    client_id and client_secret from Google. 
# 2. Run this script and follow the directions on 
#    the screen, which will give step you 
#    through the following steps:
# 3. Copy the URL printed out, and paste 
#    the URL in a browser to load the page. 
# 4. On the resulting page, click OK (possibly
#    after being asked to log in to your Google 
#    account). 
# 5. You will be redirected to a page that provides 
#    a code that you should copy and paste back into the 
#    terminal window, so this script can exchange it for
#    an access token from Google, and store the token.  
#    That will be the token the other spreadsheet access
#    code can use. 


use Net::Google::DataAPI::Auth::OAuth2;
use Net::Google::Spreadsheets;
use Storable; #to save and restore token for future use
use Term::Prompt;

# Provide the filename in which we will store the access 
# token.  This file will also need to be readable by the 
# other script that accesses the spreadsheet and parses
# the contents. 

my $session_filename = "stored_google_access.session";


# Code for accessing your Google account.  The required client_id
# and client_secret can be found in your Google Developer's console 
# page, as described in the detailed instruction document.  This 
# block of code will also need to appear in the other script that
# accesses the spreadsheet. 

# Be sure to edit the lines below to fill in your correct client 
# id and client secret!
my $oauth2 = Net::Google::DataAPI::Auth::OAuth2->new(
    client_id => 'your_client_id.apps.googleusercontent.com',
    client_secret => 'your_client_secret',
    scope => ['http://spreadsheets.google.com/feeds/'],
    redirect_uri => 'https://developers.google.com/oauthplayground',
                             );
# We need to set these parameters this way in order to ensure 
# that we get not only an access token, but also a refresh token
# that can be used to update it as needed. 
my $url = $oauth2->authorize_url(access_type => 'offline',
                 approval_prompt => 'force');

# Give the user instructions on what to do:
print <<END

The following URL can be used to obtain an access token from
Google.  

1. Copy the URL and paste it into a browser.  

2.  You may be asked to log into your Google account if you 
were not logged in already in that browser.  If so, go 
ahead and log in to whatever account you want to have 
access to the Google doc. 

3. On the next page, click "Accept" when asked to grant access. 

4.  You will then be redirected to a page with a box in the 
left-hand column labeled  "Authorization code".  
Copy the code in that box and come back here. 

Here is the URL to paste in your browser to get the code:

$url

END
    ;

# Here is where we get the code from the user:
my $code = prompt('x', 'Paste the code obtained at the above URL here: ', '', ''); 

# Exchange the code for an access token:
my $token = $oauth2->get_access_token($code) or die;

# If we get to here, it worked!  Report success: 
print "\nToken obtained successfully!\n";
print "Here are the token contents (just FYI):\n\n";
print $token->to_string, "\n";

# Save the token for future use:
my $session = $token->session_freeze;
store($session, $session_filename);

print <<END2

Token successfully stored in file $session_filename.

Use that filename in your spreadsheet-access script to 
load the token as needed for access to the spreadsheet data. 

END2
    ;

一旦您开始工作并将令牌存储在磁盘上,那么批处理脚本的开头就可以设置电子表格访问,如下所示:

use Net::Google::Spreadsheets;
use Net::Google::DataAPI::Auth::OAuth2;
use Net::OAuth2::AccessToken;
use Storable;

# Authentication code based on example from gist at 
#  https://gist.github.com/hexaddikt/6738247

# Get the token that we saved previously in order to authenticate:
my $session_filename = "stored_google_access.session";


# Be sure to edit the lines below to fill in your correct client 
# id and client secret!
my $oauth2 = Net::Google::DataAPI::Auth::OAuth2->new(
    client_id => 'your_client_id.apps.googleusercontent.com',
    client_secret => 'your_client_secret',
    scope => ['http://spreadsheets.google.com/feeds/'],
    redirect_uri => 'https://developers.google.com/oauthplayground',
                             );

# Deserialize the file so we can thaw the session and reuse the refresh token
my $session = retrieve($sessionfile);

my $restored_token = Net::OAuth2::AccessToken->session_thaw($session,
                                auto_refresh => 1,
                                profile => $oauth2->oauth2_webserver,
                                );

$oauth2->access_token($restored_token);
# Now we can use this token to access the spreadsheets 
# in our account:
my $service = Net::Google::Spreadsheets->new(
                         auth => $oauth2);
    
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 Google 表格应用程序中进行身份验证 的相关文章

随机推荐

  • jQuery 检查复选框并触发 javascript onclick 事件

    我正在尝试使用 jQuery 检查复选框并在此过程中触发 onclick 事件 假设我在 html 中定义了一个复选框
  • eclipse - 在android虚拟设备中卡住中文

    当我在 Eclipse 中运行 Android 模拟器时 当我尝试编写文本时 所有键盘字符都会被翻译为中文 为什么是这样 Thanks 点击并按住EditText Select 输入法 Select 安卓键盘
  • 获取 SignalR hub 内的完整 URL

    我正在使用 SignalR 开发一个用户跟踪解决方案 作为学习 SignalR 的有趣项目 用于 ASP NET MVC 应用程序 目前我可以跟踪登录的用户以及他们在特定页面上停留的时间 如果他们移动到另一个页面 我也会跟踪该页面 并且 S
  • JFace/SWT:更改 InputDialog 中按钮的标签

    我想创建一个带有 确定 取消 按钮的自定义标签的输入对话框 我在用着org eclipse jface dialogs InputDialog http help eclipse org galileo index jsp topic or
  • javax.net.ssl.SSLException:没有可用的 PSK。无法恢复

    我正在使用 Jetty 客户端发送传出请求 在 Java 10 下完美运行的代码在 Java 11 下突然出现以下异常 javax net ssl SSLException No PSK available Unable to resume
  • UILocalNotification 在后台 10 分钟后不提示

    In didFinishLaunchingWithOptions调用函数的定时器循环httpRequest每 1 分钟间隔一次 BOOL application UIApplication application didFinishLaun
  • WIX 捆绑包创建

    我尝试创建一个包含 exe 的 MSI 使用 WIX 中的捆绑选项 这样做时出现错误 有人可以帮我解决这个问题吗 下面是代码
  • 每次重新运行终端时,我都必须输入 export PATH=~/anaconda/bin:"$PATH"

    我已经安装了 Anaconda for Mac 但出现了一些问题 当我输入命令时which conda or which ipython I get conda not found and ipython not find 然后我找到这个命
  • 改进 D3 序列旭日示例

    This D3示例作为我的出发点 http bl ocks org kerryrodden 7090426 http bl ocks org kerryrodden 7090426 我想更改提供图表的数据 并且我做了以下新示例 http j
  • 单击输入字段会触发窗口调整大小

    我有一个带有徽标 菜单和搜索的标题 当我在桌面上时 我会按该顺序显示所有元素 但如果我的窗口宽度小于 980 像素 菜单会隐藏 有一个切换按钮 并且徽标会与nav并附在徽标之后 如果宽度更大 则徽标将再次分离并附加到 DOM 中的旧位置 w
  • Jquery UI 日期选择器 设置默认日期

    我使用 jQuery UI 作为日期选择器 我想在字段中显示当前日期作为默认值 以下是我的代码 请帮助 From Date
  • 如何将 NSSecureCoding 与 id 对象一起使用

    我正在创建一个链接列表并使用容器对对象 下一个和上一个属性进行分组 就像基金会收藏一样 我希望它能够实现NSSecureCoding 这是声明 interface ListContainer NSObject
  • 通过单个 GPIO 引脚转储闪存

    我正在使用 Infineon 的 XMC4500 Relax Kit 并尝试通过单个 GPIO 引脚提取固件 我非常天真的想法是通过 GPIO 引脚一次转储一位 然后用逻辑分析仪以某种方式 嗅探 数据 伪代码 while word by w
  • 如何检测 UISwipeGestureRecognizer 的结束?

    来自苹果文档 滑动是一种离散手势 因此每个手势仅发送一次关联的操作消息 void touchesEnded NSSet touches withEvent UIEvent event 当我使用 UISwipeGestureRecognize
  • 使用 QWT 构建时出错

    我收到一个错误 undefined reference to QwtPlot QwtPlot QWidget 当我尝试构建我的项目时 即使设置中一切看起来都很好 在我的 CmakeLists txt 中我有 include director
  • 如何在一段特定时间后在后台运行 ajax 调用? [关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 我想在一段特定的时间后显示警报消息 您想继续吗 如果用户同意 则 ajax 调用必须在后台运行 否则取消 ajax 调用 那么请告诉我
  • TRACKER:错误TRK0005:无法找到:“CL.exe”。该系统找不到指定的文件

    我尝试在 Windows 8 上的 Node js 项目中执行以下命令 npm 安装 电子邮件受保护 cdn cgi l email protection 但我收到一个错误 我不知道如何处理 TRACKER 错误TRK0005 无法找到 C
  • 使用 AJAX 或多线程加速页面加载

    我的页面有 5 个部分 每个部分大约需要 1 秒来渲染 Page Load RenderSection1 1 sec RenderSection2 1 sec RenderSection3 1 sec RenderSection4 1 se
  • 如何将 WordPress 类别选择限制为只有一个?

    我有一个自定义帖子类型和一个自定义分类设置 非常标准的东西 但是 我想知道如何限制我的客户在每个帖子中选择多个分类类别 我不介意他们能够创建和删除分类类型 但我不希望他们选择多个分类类型 因为这些是复选框 所以它们可以 也许单选按钮可以工作
  • 在 Google 表格应用程序中进行身份验证

    我有一个类似批处理的应用程序 由调度程序定期调用 无需人类用户参与 它使用 PerlNet Google 电子表格 http metacpan org pod Net Google Spreadsheets包通过从数据库获取的数据来更新 G