Method: OAuth2::AccessToken#initialize
- Defined in:
- lib/oauth2/access_token.rb
#initialize(client, token, opts = {}) ⇒ AccessToken
Note:
For “soon-to-expire”/“clock-skew” functionality see the ‘:expires_latency` option.
Note:
If no token is provided, the AccessToken will be considered invalid. This is to prevent the possibility of a token being accidentally created with no token value. If you want to create an AccessToken with no token value, you can pass in an empty string or nil for the token value. If you want to create an AccessToken with no token value and no refresh token, you can pass in an empty string or nil for the token value and nil for the refresh token, and ‘raise_errors: false`.
Initialize an AccessToken
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/oauth2/access_token.rb', line 148 def initialize(client, token, opts = {}) @client = client @token = token.to_s opts = opts.dup i[refresh_token expires_in expires_at expires_latency].each do |arg| instance_variable_set("@#{arg}", opts.delete(arg) || opts.delete(arg.to_s)) end no_tokens = (@token.nil? || @token.empty?) && (@refresh_token.nil? || @refresh_token.empty?) if no_tokens if @client.[:raise_errors] raise Error.new({ error: "OAuth2::AccessToken has no token", error_description: "Options are: #{opts.inspect}", }) elsif !OAuth2.config.silence_no_tokens_warning warn("OAuth2::AccessToken has no token") end end # @option opts [Fixnum, String] :expires is deprecated @expires_in ||= opts.delete("expires") @expires_in &&= @expires_in.to_i @expires_at &&= convert_expires_at(@expires_at) @expires_latency &&= @expires_latency.to_i @expires_at ||= Time.now.to_i + @expires_in if @expires_in && !@expires_in.zero? @expires_at -= @expires_latency if @expires_latency = { mode: opts.delete(:mode) || :header, header_format: opts.delete(:header_format) || "Bearer %s", param_name: opts.delete(:param_name) || "access_token", } [:token_name] = opts.delete(:token_name) if opts.key?(:token_name) @params = opts end |