-
Notifications
You must be signed in to change notification settings - Fork 14
/
util.clj
37 lines (34 loc) · 1.3 KB
/
util.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
(ns clj-spotify.util
(:require [clj-http.client :as client]
[clojure.data.codec.base64 :as b64]
[clojure.java.io :as io])
(:import [org.apache.commons.io IOUtils]))
(defn get-access-token
"Requests an access token from Spotify's API via the Client Credentials flow.
The returned token cannot be used for endpoints which access private user information;
use the OAuth 2 Authorization Code flow for that."
[client-id client-secret]
(-> "https://accounts.spotify.com/api/token"
(client/post {:form-params {:grant_type "client_credentials"}
:basic-auth [client-id client-secret]
:as :json})
:body
:access_token))
(defn refresh-access-token
"Refreshes an access token using a refresh token that was generated
via the OAuth 2 Authorization Code flow."
[client-id client-secret refresh_token]
(-> "https://accounts.spotify.com/api/token"
(client/post {:form-params {:grant_type "refresh_token"
:refresh_token refresh_token}
:basic-auth [client-id client-secret]
:as :json})
:body
:access_token))
(defn encode-to-base64
"Encode file to base64."
[file-path]
(-> file-path
io/input-stream
IOUtils/toByteArray
b64/encode))