Ruby Google Drive API-禁止在web\u内容\u链接上使用403
我可以在谷歌硬盘上很好地阅读和扫描文件,但无论我做什么,我似乎都无法访问web\u content\u链接。我的烫发看起来不错。我完全不知所措 我将一些GoogleDrive API逻辑抽象到了Google_setup.rb 然后在我的rake文件中,我只想访问下载文件。我似乎能够很好地扫描文件,但即使在我看来,我应该有烫发来访问下载链接,我总是得到403禁止的错误 如果你能帮忙,请告诉我 google\u setup.rbRuby Google Drive API-禁止在web\u内容\u链接上使用403,ruby,google-drive-api,Ruby,Google Drive Api,我可以在谷歌硬盘上很好地阅读和扫描文件,但无论我做什么,我似乎都无法访问web\u content\u链接。我的烫发看起来不错。我完全不知所措 我将一些GoogleDrive API逻辑抽象到了Google_setup.rb 然后在我的rake文件中,我只想访问下载文件。我似乎能够很好地扫描文件,但即使在我看来,我应该有烫发来访问下载链接,我总是得到403禁止的错误 如果你能帮忙,请告诉我 google\u setup.rb require 'google/apis/drive_v3' requ
require 'google/apis/drive_v3'
require 'googleauth'
require 'googleauth/stores/file_token_store'
require 'fileutils'
require 'open-uri'
module GoogleSetup
OOB_URI = "urn:ietf:wg:oauth:2.0:oob".freeze
def self.authorize
client_id = Google::Auth::ClientId.from_file Rails.root.join('lib', 'assets', 'credentials.json').freeze
token_store = Google::Auth::Stores::FileTokenStore.new file: 'token.yaml'
authorizer = Google::Auth::UserAuthorizer.new client_id, Google::Apis::DriveV3::AUTH_DRIVE, token_store
user_id = 'default'
credentials = authorizer.get_credentials user_id
if credentials.nil?
url = authorizer.get_authorization_url base_url: 'https://seeburg.herokuapp.com/'
puts 'Open the following URL in the browser and enter the ' \
"resulting code after authorization:\n" + url
code = ENV["GOOGLE_CODE"]
credentials = authorizer.get_and_store_credentials_from_code(
user_id: user_id, code: code, base_url: OOB_URI
)
end
drive_service = Google::Apis::DriveV3::DriveService.new
drive_service.client_options.application_name = 'Seeburg Google Drive Integration'
drive_service.authorization = credentials
drive_service
end
def self.get_files(query)
drive_service = GoogleSetup.authorize
files = []
@page_token = ''
while @page_token
begin
response = drive_service.list_files(page_size: 100, q: query, page_token: @page_token, fields: 'nextPageToken, files')
@page_token = response.next_page_token || false
if response.files.empty?
puts 'No files found'
else
files = response.files
end
sleep 0.5
rescue StandardError => e
puts e
end
end
files
end
end
测试google.rake
require 'google_setup'
require 'fileutils'
require 'open-uri'
desc 'Test Google Drive API'
task test_google: :environment do
folder_id = "1j1Ly_NveiCtfrolzSxmrbHS1DenPZagV";
query = "name contains 'MP3' and '#{folder_id}' in parents";
GoogleSetup.get_files(query).each do |file|
##If I can get this section to work, everything else I need will work
begin
puts download = URI.open(file.web_content_link)
rescue StandardError => e
puts e
end
end
end
结果是超时问题。谷歌只允许你在认证的时间范围内采取某些行动。关于
我总是收到403禁止的错误。
,我能问一下错误消息吗?你在尝试通过网络发送请求时会收到相同的错误吗?如果您得到错误,它应该有更多的信息。