Method: PlaylyfeClient::Connection#get_access_token

Defined in:
lib/playlyfe_client/connection.rb

#get_access_tokenObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/playlyfe_client/connection.rb', line 60

def get_access_token
  begin
    if @type == 'client'
      access_token = RestClient::Request.execute(:method => :post, :url => 'https://playlyfe.com/auth/token',
        :payload => {
          :client_id => @id,
          :client_secret => @secret,
          :grant_type => 'client_credentials'
        }.to_json,
        :headers => {
          :content_type => :json,
          :accepts => :json
        },
        :ssl_version => 'SSLv23'
      )
    else
      access_token = RestClient::Request.execute(:method => :post, :url => "https://playlyfe.com/auth/token",
        :payload => {
          :client_id => @id,
          :client_secret => @secret,
          :grant_type => 'authorization_code',
          :code => @code,
          :redirect_uri => @redirect_uri
        }.to_json,
        :headers => {
          :content_type => :json,
          :accepts => :json
        },
        :ssl_version => 'SSLv23'
      )
    end
    access_token = JSON.parse(access_token)
    expires_at ||= Time.now.to_i + access_token['expires_in']
    access_token.delete('expires_in')
    access_token['expires_at'] = expires_at
    @store.call access_token
    if @load.nil?
      @load = lambda { return access_token }
    else
      old_token = @load.call
      if access_token != old_token
        @load = lambda { return access_token }
      end
    end
  rescue => e
    raise PlaylyfeClient::ConnectionError.new(e.response)
  end
end