我设置了一个小脚本,以通过Google Drive API将Google Sheet的第一个标签导出为csv。我可以执行一次脚本,并且可以正常工作。在执行过程中,我提供了一个URL,该URL可让我在线进行身份验证并提供代码。该代码将存储在token.yaml文件中。然后脚本成功导出了我的工作表。
第二次执行脚本时,出现错误
dailyLimitExceededUnreg: Daily Limit for Unauthenticated Use Exceeded.
Continued use requires signup. (Google::Apis::ClientError)
当我删除第一次(成功)尝试期间创建的token.yaml文件时,可以再次执行该脚本,包括。如上所述的身份验证。当我再次执行脚本时,我又回到了错误。
require 'google/apis/drive_v3'
require 'googleauth'
require 'googleauth/stores/file_token_store'
require 'fileutils'
OOB_URI = 'urn:ietf:wg:oauth:2.0:oob'.freeze
APPLICATION_NAME = 'Maker'.freeze
CREDENTIALS_PATH = 'credentials.json'.freeze
FILE_ID = '<my-file-id>'
# The file token.yaml stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
#
# copied from https://developers.google.com/drive/api/v3/quickstart/ruby
TOKEN_PATH = 'token.yaml'.freeze
SCOPE = Google::Apis::DriveV3::AUTH_DRIVE
##
# Ensure valid credentials, either by restoring from the saved credentials
# files or initiating an OAuth2 authorization. If authorization is required,
# the user's default browser will be launched to approve the request.
#
# @return [Google::Auth::UserRefreshCredentials] OAuth2 credentials
#
# copied from https://developers.google.com/drive/api/v3/quickstart/ruby
def authorize
client_id = Google::Auth::ClientId.from_file CREDENTIALS_PATH
token_store = Google::Auth::Stores::FileTokenStore.new file: TOKEN_PATH
authorizer = Google::Auth::UserAuthorizer.new client_id, SCOPE, token_store
user_id = 'default'
credentials = authorizer.get_credentials user_id
create_credentials(authorizer, user_id) if credentials.nil?
end
def create_credentials(authorizer, user_id)
url = authorizer.get_authorization_url base_url: OOB_URI
puts 'Open the following URL in the browser and enter the ' \
"resulting code after authorization:\n" + url
code = STDIN.gets
authorizer.get_and_store_credentials_from_code user_id: user_id,
code: code,
base_url: OOB_URI
end
# copied from https://developers.google.com/drive/api/v3/about-auth
def download_csv
service = Google::Apis::DriveV3::DriveService.new
service.client_options.application_name = APPLICATION_NAME
service.authorization = authorize
service.export_file(FILE_ID, 'text/csv') #only downloading first sheet
end
puts download_csv
答案 0 :(得分:0)
Google的快速入门示例(https://developers.google.com/drive/api/v3/quickstart/ruby)引用了其API版本0.8,该版本已过时。有一个较新的版本0.11。当我安装完该软件后,一切都可以正常工作。