-
Notifications
You must be signed in to change notification settings - Fork 0
/
xoauth2-rtoken.sh
65 lines (53 loc) · 1.42 KB
/
xoauth2-rtoken.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
#!/bin/sh
# SPDX-License-Identifier: GPL-3.0-or-later
# Copyright (C) 2019-2022 Junjiro R. Okajima
tmp=/tmp/$$
set -eu
me=$(basename $0)
abort()
{
echo "$@" 1>&2
exit 1
}
usage()
{
abort Usage: $me browser_command
}
test $# -ne 1 &&
usage
browser="$1"
which $browser > /dev/null ||
abort could not found $browser
. /etc/xoauth2-client
url='https://accounts.google.com/o/oauth2/auth'
url="$url?client_id=$client_id"
url="$url&redirect_uri=urn:ietf:wg:oauth:2.0:oob"
url="$url&response_type=code"
url="$url&scope=https://mail.google.com/"
$browser $url &
sleep 1
cat << EOF
In your browser, allow xoauth2-pipe to access your account, and get
Authorisation code.
Now Copy/paste it and hit enter key.
EOF
read authcode
curl -s \
--data-urlencode client_id=$client_id \
--data-urlencode client_secret=$client_secret \
--data-urlencode code=$authcode \
--data-urlencode redirect_uri=urn:ietf:wg:oauth:2.0:oob \
--data-urlencode grant_type=authorization_code \
https://accounts.google.com/o/oauth2/token \
|
tee $tmp|
fgrep -w refresh_token || :
cat << EOF
In the browser's screen, allow "xoauth2-pipe" to use Gmail API for your
Gmail account, and the screen will show you a refresh-token.
Then manually copy the refresh-token and paste it into the file
XOAUTH2_USER (/etc/xoauth2-user by default). The format is simple, just
the account, colon, and the refresh-token in a line.
[email protected]:your_refresh-token
EOF
rm -f $tmp $tmp.*