2015-06-09 62 views
1

我有一个类似批处理的应用程序,它由调度程序定期调用,不涉及任何人类用户。它使用Perl Net::Google::Spreadsheets包通过从数据库获取的数据更新Google表单电子表格中的某些单元格。在Google表单应用程序中进行身份验证

很长一段时间,通过为包的'新'方法提供用户名和密码进行身份验证很简单。但最近,Google要求我们使用OAuth2协议进行身份验证。

J.T.提供a solution我相信对于许多知识丰富的人来说,这对我来说是非常有帮助的。不过,我会很感激,如果有人能回答一些问题作出澄清如下:

  1. 创建凭证:一旦你创建的谷歌开发者控制台中的项目,并在询问是否创建一个新的客户端ID,你是提供三个选项:

    • 对于“Web应用程序”(它然后要求提供一个“授权的JavaScript来源”和“授权的重新导向的URI”这些有关我的情况?)
    • 对于“服务帐户”(我认为这是我的选择,但没有回答以下任务离子我无法验证它。)
    • 对于“已安装的应用程序”(人能给出例子,比如?)

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

  2. 我应该问一个JSON或P12键吗?

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

  4. 在第13行,J.T评论说:“你需要在这里放置代码并接收一个令牌”。什么样的代码?做什么?

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

换句话说,我需要有人走过我的整个过程,一步一步地走过去。

谢谢大家!

MeirG

回答

3

我不得不去通过这个也一样,不知道多在一开始,所以我很乐意帮助解释它。以下是答案,但请随时要求澄清。基本上,您需要首先运行一个需要手动干预的脚本 - 这样您可以从Google获取访问令牌,然后批处理脚本可以反复使用,无需人工干预。所以你一开始就必须跳过一些篮球,但一旦完成,你就全部成功了。所以:

  1. 选择“web应用程序”。不直观,但它会工作。

    1b。你会被要求配置一个“同意屏幕”。你放在这里并不重要 - 只要给项目一个标题即可。

    1c。对于“redirect 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但您的授权码将会更长。)不要单击代码下面的按钮,但要复制该字符串,确保获取整个内容(可能会在对话框中伸出视线)。
  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 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); 
+0

工作“开箱即用”(当然除了代替客户端ID和client_secret)。谢谢@ELNJ! – MeirG

相关问题