Google Drive APIを使って画像をアップロードしたりする必要があって
content_typeを指定して画像をおくろうとしていたのですが、
なぜかcontent_typeを指定しても遅れない状態で2時間ぐらいはまっていました

# こちらが該当のメソッド
create_file

問題点

参照元のメソッドが間違っていたこと

まさかそんなことは、と思うかもしれませんが、Google Drive APIには同じメソッド名で同じような機能を提供しているところがあるのでちょっとトリッキーなのです

今回はまったところはこちらの2つのメソッド

# GoogleDrive::Sessionクラス
# こちらはcontent_type指定できなかった

module GoogleDrive
  class Session
    def create_file(title, file_properties = {})
      file_metadata = {
        name: title,
      }.merge(file_properties)

      file = drive_service.create_file(
        file_metadata, fields: '*', supports_team_drives: true
      )

      wrap_api_file(file)
    end
  end
end

# Google::Apis::Drive3::DriveServiceクラス
# こっちのcreate_fileはcontent_typeが指定できる
module Google
  module Apis
    module DriveV3
      class DriveService < Google::Apis::Core::BaseService
        def create_file(file_object = nil, ignore_default_visibility: nil, keep_revision_forever: nil, ocr_language: nil, supports_team_drives: nil, use_content_as_indexable_text: nil, fields: nil, quota_user: nil, user_ip: nil, upload_source: nil, content_type: nil, options: nil, &block)
          if upload_source.nil?
            command = make_simple_command(:post, 'files', options)
          else
            command = make_upload_command(:post, 'files', options)
            command.upload_source = upload_source
            command.upload_content_type = content_type
          end
          command.request_representation = Google::Apis::DriveV3::File::Representation
          command.request_object = file_object
          command.response_representation = Google::Apis::DriveV3::File::Representation
          command.response_class = Google::Apis::DriveV3::File
          command.query['ignoreDefaultVisibility'] = ignore_default_visibility unless ignore_default_visibility.nil?
          command.query['keepRevisionForever'] = keep_revision_forever unless keep_revision_forever.nil?
          command.query['ocrLanguage'] = ocr_language unless ocr_language.nil?
          command.query['supportsTeamDrives'] = supports_team_drives unless supports_team_drives.nil?
          command.query['useContentAsIndexableText'] = use_content_as_indexable_text unless use_content_as_indexable_text.nil?
          command.query['fields'] = fields unless fields.nil?
          command.query['quotaUser'] = quota_user unless quota_user.nil?
          command.query['userIp'] = user_ip unless user_ip.nil?
          execute_or_queue_command(command, &block)
        end
      end
    end
  end
end

クラスによって同じメソッドでも挙動が異なり、
content_typeが指定できたりできなかったりします
※上の方はparameter見る感じmetadataに追加したらいけそうかな?とか思ってましたが、全然うまくいかなかったので、もしできた方いたら教えていただきたいです__

とりあえず私は、下の方を使ってcontent_typeを指定できたので
それ以上は調べていませんが、APIの中でクラスによって
同じメソッド名でも挙動が異なるのは非常にトリッキーで
ハマりポイントですね

以上

参考記事

なし、ふと閃いた

コメントをどうぞ

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

CAPTCHA