-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClientCredential.sh
More file actions
57 lines (44 loc) · 1.36 KB
/
ClientCredential.sh
File metadata and controls
57 lines (44 loc) · 1.36 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
myCookie=~/.`basename $0`
cookie_is_valid=0
client_id='<CLIENT_ID>'
client_secret='<SECRETE>'
scope='<SCOPE>'
urlToken='<URL_TOKEN>'
urlApi='<URL_API>'
dataJSON="{ }"
echo "Authenticating in:"
echo "$urlToken?client_id=$client_id&scope=$scope"
if [ -f $myCookie ]; then #Validate Cookie
. $myCookie
time_now=`date +%s`
if (( time_now < expires_at )); then cookie_is_valid=1; fi
fi
if !(( cookie_is_valid )); then
#Renew Access Token
auth_result=$(curl -k -s $urlToken \
-H "Content-Type: application/x-www-form-urlencoded" \
-d scope=$scope \
-d client_id=$client_id \
-d client_secret=$client_secret \
-d grant_type=client_credentials)
access_token=$(echo -e "$auth_result" | \
grep -Po '"access_token" *: *.*?[^\\]",' | \
awk -F'"' '{ print $4 }')
expires_in=$(echo -e "$auth_result" | \
grep -Po '"expires_in"*: *.*,' | \
awk -F'"' '{ print $3 }' | \
awk -F':' '{ print $2}' | \
awk -F',' '{ print $1}' )
time_now=`date +%s`
expires_at=$((time_now + expires_in - 60))
echo -e "access_token=$access_token\nexpires_at=$expires_at" > $myCookie
fi
if [ -n "$access_token" ]; then
data_result=$(curl -X POST -s $urlApi \
-H "Content-Type: application/json; charset=utf-8" \
-H "Accept: application/json" -H "Authorization: Bearer $access_token" \
-d "$dataJSON")
echo "Result: $data_result"
else
echo "Not Authenticated."
fi