This repository has been archived by the owner on Nov 21, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
gmail-setup.sh
executable file
·140 lines (105 loc) · 5.19 KB
/
gmail-setup.sh
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
58
59
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/bin/bash
# author: Munkh-Orgil Myagmarsuren
# Create and configure Google Cloud Project for Gmail API
RESET='\033[0m'
PURPLE='\033[0;35m'
GREEN='\033[0;32m'
BOLD='\033[1m'
PROJECT_ID="erxes-gmail-$(echo $RANDOM)"
TOPIC="erxes-gmail-topic-$(echo $RANDOM)"
SUBSCRIPTION="erxes-gmail-subscription"
SERVICE_ACCOUNT="erxes-service-account-$(echo $RANDOM)"
PUBSUB_SERVICE_ACCOUNT="service-${PROJECT_NUMBER}@gcp-sa-pubsub.iam.gserviceaccount.com"
DOMAIN=""
function log {
echo -e "${GREEN}${1}${RESET}"
}
(
set -e
echo "
/* _\|/_
(o o)
+----oOO-{_}-OOo----------------------------------+
| |
| |
| Thanks for using Erxes |
| Let's setup Gmail for you |
| Please install Google Cloud Platform SDK |
| https://cloud.google.com/sdk/docs/quickstarts |
| |
+-------------------------------------------------*/
"
read -p "Please enter your domain: " DOMAIN
if [ -z "$DOMAIN" ]
then
exit 'echo DOMAIN did not supplied'
fi
# Initialize
gcloud init --skip-diagnostics
# Login to account
gcloud auth login --quiet
log "Creating GCP Project ${PROJECT_ID}"
# Create Google Cloud Project
gcloud projects create ${PROJECT_ID} --name='erxes-gmail-project'
# Set project as default core/project
gcloud config set project ${PROJECT_ID}
log "Enabling Pub/Sub API"
# Enable Pub/Sub API
gcloud services enable pubsub.googleapis.com
log "Enabling Gmail API"
# Enable Gmail API
gcloud services enable gmail.googleapis.com
log "Creating Gmail Topic ${TOPIC}"
log "This may take a while...😬"
# If the topic's project was recently created, you may need to wait a few
# minutes for the project's organization policy to be properly initialized,
# and then retry this operation.
sleep 60
# Create Gmail Topic
gcloud pubsub topics create ${TOPIC}
log "Creating Gmail subscription ${SUBSCRIPTION}"
# configure the subscription push identity
gcloud pubsub subscriptions create ${SUBSCRIPTION} \
--topic=${TOPIC} \
--topic-project=${PROJECT_ID} \
--push-endpoint="https://${DOMAIN}/integrations/gmail/webhook"
# --push-auth-service-account=${SERVICE_ACCOUNT_EMAIL} \
log "Adding publish role to gmail-api-push service account"
# Add gmail api service account pub/sub - publish role
gcloud pubsub topics add-iam-policy-binding ${TOPIC} \
--member="serviceAccount:[email protected]" \
--role="roles/pubsub.publisher"
echo -e "
\033[33;7m${BOLD}ONE LAST STEP${RESET}
+==================================================================================================================+
${PURPLE}${BOLD}[ Create app in OAuth Consent screen ]${RESET}
1. Navigate to https://console.cloud.google.com/apis/credentials/consent?project=${PROJECT_ID}
2. Fill the form and create/edit app
3. Click on the [Add Scope] button
4. Search [Gmail API] and select https://mail.google.com/ scope and [Add]
${PURPLE}${BOLD}[ Create OAuth Client ]${RESET}
1. Navigate to https://console.cloud.google.com/apis/credentials?project=${PROJECT_ID}
2. Click on the +Credentials and select OAuth Client ID
3. Select [Web Application] type and write App name
4. Authorized redirect URIs add as http://${DOMAIN}/integrations/gmail/login
5. Click Create button and you will get your [Client ID] and [Client Secret] copy them
${PURPLE}${BOLD}[ Navigate to Erxes [System config] and fill them as follows ]${RESET}
PROJECT_ID: ${GREEN}${PROJECT_ID}${RESET}
TOPIC: ${GREEN}${TOPIC}${RESET}
┏━ GOOGLE_CLIENT_ID: // CLIENT_ID in OAuth Client
┣━ GOOGLE_CLIENT_SECRET: // CLIENT_SECRET in OAuth Client
┃
┗━ ${GREEN}Navigate to https://console.cloud.google.com/apis/credentials?project=${PROJECT_ID} and select [Web Client]${RESET}
+==================================================================================================================+
References
==========
Configure your Push Endpoint https://console.cloud.google.com/cloudpubsub/subscription/detail/erxes-gmail-subscription?project=${PROJECT_ID}
"
log "Gmail setup succesfully done 🛸🛸🛸"
log "ERXES Rocks!"
)
errorCode=$?
if [ $errorCode -ne 0 ]; then
log "Error occurred while setting up GCP - Gmail"
exit $errorCode
fi