-
Notifications
You must be signed in to change notification settings - Fork 1
/
email_authorization.bsh
executable file
·49 lines (40 loc) · 1.57 KB
/
email_authorization.bsh
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
#!/bin/bash
# Check internet connection
if ping -c 1 google.com >> /dev/null 2>&1; then
echo "Internet connection detected"
else
echo "Internet connection not detected, will exit"
exit 3
fi
# Check if the config file exists
if [ ! -f config.json ]; then
echo "Config file not found, please read README.md on github"
exit 1;
fi
auth_url="http://accounts.google.com/o/oauth2/v2/auth"
scope="https://www.googleapis.com/auth/gmail.readonly"
redirect_uri="urn:ietf:wg:oauth:2.0:oob"
token_url="https://www.googleapis.com/oauth2/v4/token"
client_id=$(cat config.json | jq -r '.client_id')
client_secret=$(cat config.json | jq -r '.client_secret')
echo "Loaded config with client_id: $client_id"
echo "client_secret: $client_secret"
# Open the browser for the authorization code
open "$auth_url?scope=$scope&redirect_uri=$redirect_uri&response_type=code&client_id=$client_id"
echo "Please paste the authorization code:"
# Read the authorization code
read auth_code
echo "Authorization code: $auth_code"
# Get the access token
form_data="code=$auth_code&client_id=$client_id&client_secret=$client_secret&redirect_uri=$redirect_uri&grant_type=authorization_code"
token_response=$(curl -s --data $form_data $token_url)
echo $token_response
# Read token from the json
access_token=$(echo $token_response | jq -r '.access_token')
refresh_token=$(echo $token_response | jq -r '.refresh_token')
echo "Access token: $access_token"
echo "Refresh token: $refresh_token"
# Save the token in a file
mkdir -p resources
echo $token_response > ./resources/token.json
echo "Authentication successfull."