Perl 在Google sheets应用程序中进行身份验证

Perl 在Google sheets应用程序中进行身份验证,perl,google-authentication,Perl,Google Authentication,我有一个类似批处理的应用程序,它由调度器周期性地调用,不涉及人工用户。它使用Perl包通过从数据库获取的数据更新GoogleSheets电子表格中的一些单元格 很长一段时间以来,只需为包的“新”方法提供用户名和密码,就可以简单地进行身份验证。但是最近,Google要求我们使用OAuth2协议进行身份验证 但我相信这对许多比我更有知识的人来说是非常有帮助的。不过,如果有人能回答一些问题来澄清,我将不胜感激,具体如下: 创建凭据:一旦您在Google开发者控制台中创建了一个项目,并要求创建一个新的客

我有一个类似批处理的应用程序,它由调度器周期性地调用,不涉及人工用户。它使用Perl包通过从数据库获取的数据更新GoogleSheets电子表格中的一些单元格

很长一段时间以来,只需为包的“新”方法提供用户名和密码,就可以简单地进行身份验证。但是最近,Google要求我们使用OAuth2协议进行身份验证

但我相信这对许多比我更有知识的人来说是非常有帮助的。不过,如果有人能回答一些问题来澄清,我将不胜感激,具体如下:

  • 创建凭据:一旦您在Google开发者控制台中创建了一个项目,并要求创建一个新的客户端ID,您将看到三个选项:

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

  • 我应该要求JSON还是P12密钥

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

  • 在第13行,J.T评论说“您需要将代码放在这里并接收令牌”。什么代码?做什么

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

  • 换句话说,我需要有人一步一步地轻轻地陪我走过整个过程

    谢谢大家


    MeirG

    我也不得不经历这一过程,一开始知道的不多,所以我很乐意帮助解释。以下是答案,但请随时要求澄清。基本上,您需要首先运行一个需要手动干预的脚本——这可以让您从Google获得一个访问令牌,然后您的批处理脚本可以在不需要人工干预的情况下反复使用该令牌。所以你必须在开始时跳过一些圈,但一旦完成,你就准备好了。因此:

  • 选择“web应用程序”。不直观,但它会起作用
  • 1b。将要求您配置“同意屏幕”。你在这里写什么并不重要,只要给这个项目一个标题就行了

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

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

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

  • 复制脚本打印出的URL,并将其粘贴到浏览器中
  • 如果谷歌要求你登录,请登录谷歌。然后单击下一页上的“允许访问”
  • 在浏览器的下一页上,左侧将有一个标有“授权码”的框。(如下所示:但您的身份验证代码将更长。)不要单击代码下方的按钮,而是复制该字符串,确保获得整个内容(可能在对话框中看不到)
  • 返回到运行脚本的终端,粘贴复制的代码
  • 如果一切顺利,脚本将用该代码交换访问令牌,并将令牌保存在磁盘上。然后批处理脚本可以重复使用该标记

    下面是执行所有这些操作的扩展代码:

    #!/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
        ;
    
    “开箱即用”(当然,除了替换客户ID和客户机密)。谢谢@ELNJ!
    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);