Advertisement
Guest User

Untitled

a guest
Jul 3rd, 2015
463
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.95 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. # Code to get a web-based token that can be stored
  4. # and used later to authorize our spreadsheet access.
  5.  
  6. # Based on code from https://gist.github.com/hexaddikt/6738162
  7.  
  8. #-------------------------------------------------------------------
  9.  
  10. # To use this code:
  11.  
  12. # 1. Edit the lines below to put in your own
  13. # client_id and client_secret from Google.
  14. # 2. Run this script and follow the directions on
  15. # the screen, which will give step you
  16. # through the following steps:
  17. # 3. Copy the URL printed out, and paste
  18. # the URL in a browser to load the page.
  19. # 4. On the resulting page, click OK (possibly
  20. # after being asked to log in to your Google
  21. # account).
  22. # 5. You will be redirected to a page that provides
  23. # a code that you should copy and paste back into the
  24. # terminal window, so this script can exchange it for
  25. # an access token from Google, and store the token.
  26. # That will be the the token the other spreadsheet access
  27. # code can use.
  28.  
  29.  
  30. use Net::Google::DataAPI::Auth::OAuth2;
  31. use Net::Google::Spreadsheets;
  32. use Storable; #to save and restore token for future use
  33. use Term::Prompt;
  34.  
  35. # Provide the filename in which we will store the access
  36. # token. This file will also need to be readable by the
  37. # other script that accesses the spreadsheet and parses
  38. # the contents.
  39.  
  40. my $session_filename = "stored_google_access.session";
  41.  
  42.  
  43. # Code for accessing your Google account. The required client_id
  44. # and client_secret can be found in your Google Developer's console
  45. # page, as described in the detailed instruction document. This
  46. # block of code will also need to appear in the other script that
  47. # accesses the spreadsheet.
  48.  
  49. # Be sure to edit the lines below to fill in your correct client
  50. # id and client secret!
  51. my $oauth2 = Net::Google::DataAPI::Auth::OAuth2->new(
  52. client_id => 'your_client_id.apps.googleusercontent.com',
  53. client_secret => 'your_client_secret',
  54. scope => ['http://spreadsheets.google.com/feeds/'],
  55. redirect_uri => 'https://developers.google.com/oauthplayground',
  56. );
  57. # We need to set these parameters this way in order to ensure
  58. # that we get not only an access token, but also a refresh token
  59. # that can be used to update it as needed.
  60. my $url = $oauth2->authorize_url(access_type => 'offline',
  61. approval_prompt => 'force');
  62.  
  63. # Give the user instructions on what to do:
  64. print <<END
  65.  
  66. The following URL can be used to obtain an access token from
  67. Google.
  68.  
  69. 1. Copy the URL and paste it into a browser.
  70.  
  71. 2. You may be asked to log into your Google account if you
  72. were not logged in already in that browser. If so, go
  73. ahead and log in to whatever account you want to have
  74. access to the Google doc.
  75.  
  76. 3. On the next page, click "Accept" when asked to grant access.
  77.  
  78. 4. You will then be redirected to a page with a box in the
  79. left-hand column labeled "Authorization code".
  80. Copy the code in that box and come back here.
  81.  
  82. Here is the URL to paste in your browser to get the code:
  83.  
  84. $url
  85.  
  86. END
  87. ;
  88.  
  89. # Here is where we get the code from the user:
  90. my $code = prompt('x', 'Paste the code obtained at the above URL here: ', '', '');
  91.  
  92. # Exchange the code for an access token:
  93. my $token = $oauth2->get_access_token($code) or die;
  94.  
  95. # If we get to here, it worked! Report success:
  96. print "nToken obtained successfully!n";
  97. print "Here are the token contents (just FYI):nn";
  98. print $token->to_string, "n";
  99.  
  100. # Save the token for future use:
  101. my $session = $token->session_freeze;
  102. store($session, $session_filename);
  103.  
  104. print <<END2
  105.  
  106. Token successfully stored in file $session_filename.
  107.  
  108. Use that filename in your spreadsheet-access script to
  109. load the token as needed for access to the spreadsheet data.
  110.  
  111. END2
  112. ;
  113.  
  114. use Net::Google::Spreadsheets;
  115. use Net::Google::DataAPI::Auth::OAuth2;
  116. use Net::OAuth2::AccessToken;
  117. use Storable;
  118.  
  119. # Authentication code based on example from gist at
  120. # https://gist.github.com/hexaddikt/6738247
  121.  
  122. # Get the token that we saved previously in order to authenticate:
  123. my $session_filename = "stored_google_access.session";
  124.  
  125.  
  126. # Be sure to edit the lines below to fill in your correct client
  127. # id and client secret!
  128. my $oauth2 = Net::Google::DataAPI::Auth::OAuth2->new(
  129. client_id => 'your_client_id.apps.googleusercontent.com',
  130. client_secret => 'your_client_secret',
  131. scope => ['http://spreadsheets.google.com/feeds/'],
  132. redirect_uri => 'https://developers.google.com/oauthplayground',
  133. );
  134.  
  135. # Deserialize the file so we can thaw the session and reuse the refresh token
  136. my $session = retrieve($sessionfile);
  137.  
  138. my $restored_token = Net::OAuth2::AccessToken->session_thaw($session,
  139. auto_refresh => 1,
  140. profile => $oauth2->oauth2_webserver,
  141. );
  142.  
  143. $oauth2->access_token($restored_token);
  144. # Now we can use this token to access the spreadsheets
  145. # in our account:
  146. my $service = Net::Google::Spreadsheets->new(
  147. auth => $oauth2);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement