2015-06-09 135 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

相關問題