2015-05-27 7 views
6

के साथ विफल रहा है पर्ल में यह कोड वर्षों से काम कर रहा था और अब मेरे स्प्रेडशीट लॉग इन विफल हो गए, जब मैंने अपने खाते में लॉग इन किया तो मैंने देखा कि एक नए ड्राइव संस्करण पर स्विच किया गया है। शायद कुछ प्रमाणीकरण विधियों को हटा दिया गया है?नेट :: Google :: AuthSub लॉगिन नए Google ड्राइव संस्करण

my $auth = Net::Google::AuthSub->new; 
my $response = $auth->login('[email protected]', 'PASS'); 
if ($response->is_success) { 
    print "Hurrah! Logged in\n"; 
} else { 
    die "Login failed: ".$response->error."\n"; 
} 

परिणाम है:

Login failed: 

और कोड:

use Net::Google::Spreadsheets; 
my $service = Net::Google::Spreadsheets->new(
username => '[email protected]', 
password => 'PASS' 
); 

परिणाम है:

Net::Google::AuthSub login failed at /usr/local/share/perl/5.18.2/Net/Google/Spreadsheets.pm line 42. 

के रूप में कहीं सुझाव दिया मैं SSL प्रमाणपत्र जाँच को छोड़ करने की कोशिश की के साथ:

$ENV{PERL_LWP_SSL_VERIFY_HOSTNAME} = 0; 

लेकिन इससे भी मदद नहीं मिलती है। इसे काम करने के लिए मैं क्या कर सकता हूं? धन्यवाद।

+0

ClientLogin API गूगल द्वारा हटा दिया गया है। यह 2 साल के लिए बहिष्कृत किया गया था। मैंने टिकट भर दिया है ([आरटी # 104767] (https://rt.cpan.org/Ticket/Display.html?आईडी = 104767)), लेकिन नेट :: Google :: AuthSub को पिछले 5 वर्षों के दौरान कोई अपडेट नहीं मिला ... – dolmen

उत्तर

12

मुझे अपने प्रश्न का उत्तर देना है क्योंकि मुझे समाधान खोजने में खुशी हुई। Google ने अपना प्रमाणीकरण एल्गोरिदम बदल दिया, इसलिए हमें OAuth 2.0 का उपयोग करना होगा। एपीआई & प्रमाणन -> एपीआई: -> साख -> OAuth -> ग्राहक आईडी - -> इंस्टॉल किए गए ऐप्लिकेशन> अन्य

और अपने एपीआई यानी सक्षम https://console.developers.google.com/

एपीआई & प्रमाणन: आपको कम से साख बनाने की आवश्यकता होगी -> Google Apps API को> डिस्क API

निम्नलिखित कोड ठीक काम करता है:

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

my $oauth2 = Net::Google::DataAPI::Auth::OAuth2->new(
    client_id => 'ID.apps.googleusercontent.com', 
    client_secret => 'SECRET', 
    scope => ['http://spreadsheets.google.com/feeds/'], 
); 
#you can skip URL if you have your token saved and continue from RESTORE label 

my $url = $oauth2->authorize_url(); 
#you will need to put code here and receive token 
print "OAuth URL, get code: $url\n"; 
use Term::Prompt; 
my $code = prompt('x', 'paste the code: ', '', ''); 
my $token = $oauth2->get_access_token($code) or die; 

#save token for future use 
my $session = $token->session_freeze; 
store($session, 'google_spreadsheet.session'); 

RESTORE: 
my $session = retrieve('google_spreadsheet.session'); 
my $restored_token = Net::OAuth2::AccessToken->session_thaw($session, 
    auto_refresh => 1, 
    profile => $oauth2->oauth2_webserver, 
); 
$oauth2->access_token($restored_token); 

my $service = Net::Google::Spreadsheets->new(auth => $oauth2); 
# and then as before.. 

सहेजें और टोकन सत्र उदाहरणमें पाया बहाल

+0

बहुत बढ़िया, एक आकर्षण की तरह काम करता है। शेयर करने के लिए बहुत बहुत शुक्रिया। –

0

इसके लिए धन्यवाद - बहुत उपयोगी। एक जोड़ा यह है कि मूल पोस्टर के उत्तर में कोड एक टोकन उत्पन्न करेगा जो एक घंटे में समाप्त हो जाएगा। यदि आप इसका उपयोग करना जारी रखना चाहते हैं, तो आपको प्रारंभिक अनुरोध तैयार करना सुनिश्चित करना होगा ताकि यह न केवल पहुंच टोकन उत्पन्न करे, बल्कि रीफ्रेश टोकन भी उत्पन्न करे। इस पाने के लिए, बदलने के प्रारंभिक

my $url = $oauth2->authorize_url(); 

my $url = $oauth2->authorize_url(access_type => 'offline', 
           approval_prompt => 'force'); 

को फिर लौट आए टोकन आवश्यक refresh_token शामिल होगा।

+0

आपके इनपुट के लिए धन्यवाद। लेकिन मैंने सिर्फ एक बार टोकन जेनरेट किया और यह पहले से ही एक महीने से अधिक है और समाप्त नहीं हुआ है। मुझे यकीन नहीं है कि यह कैसे काम कर रहा है। –

0

बिल्कुल ऊपर वाला कोड मेरे लिए काम नहीं करेगा। कई घंटों के आसपास देखने के बाद, दस्तावेज़ीकरण पढ़ना और विभिन्न चीजों की कोशिश करने के बाद मैं आखिरकार कुछ कोड चला गया जो काम करता था। इसका 9 0% वही है लेकिन यह 10% गायब था और साथ ही साथ स्पष्टीकरण जो सभी अंतर बनाते थे।

http://pastebin.com/8LeMyLW4

(मूल रूप से इस स्टैक ओवरफ़्लो पद से: Authenticating in a Google sheets application)

#!/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):nn"; 
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); 
संबंधित मुद्दे