Bonsoir,
Le code suivant fonctionne très bien pour le scope écrit
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
| #!/bin/bash
# A simple cURL OAuth2 authenticator
# depends on Python's built-in json module to prettify output
#
# Usage:
# ./google-oauth2.sh create - authenticates a user
# ./google-oauth2.sh refresh <token> - gets a new token
#
# Set CLIENT_ID and CLIENT_SECRET and SCOPE
CLIENT_ID="263317....."
CLIENT_SECRET="wcjd9EJdB....."
SCOPE=${SCOPE:-"https://docs.google.com/feeds"}
echo $SCOPE
set -e
if [ "$1" == "create" ]; then
RESPONSE=`curl --silent "https://accounts.google.com/o/oauth2/device/code" --data "client_id=$CLIENT_ID&scope=$SCOPE"`
DEVICE_CODE=`echo "$RESPONSE" | python -mjson.tool | grep -oP 'device_code"\s*:\s*"\K(.*)"' | sed 's/"//'`
USER_CODE=`echo "$RESPONSE" | python -mjson.tool | grep -oP 'user_code"\s*:\s*"\K(.*)"' | sed 's/"//'`
URL=`echo "$RESPONSE" | python -mjson.tool | grep -oP 'verification_url"\s*:\s*"\K(.*)"' | sed 's/"//'`
echo -n "Go to $URL and enter $USER_CODE to grant access to this application. Hit enter when done..."
read
RESPONSE=`curl --silent "https://accounts.google.com/o/oauth2/token" --data "client_id=$CLIENT_ID&client_secret=$CLIENT_SECRET&code=$DEVICE_CODE&grant_type=http://oauth.net/grant_type/device/1.0"`
ACCESS_TOKEN=`echo "$RESPONSE" | python -mjson.tool | grep -oP 'access_token"\s*:\s*"\K(.*)"' | sed 's/"//'`
REFRESH_TOKEN=`echo "$RESPONSE" | python -mjson.tool | grep -oP 'refresh_token"\s*:\s*"\K(.*)"' | sed 's/"//'`
elif [ "$1" == "refresh" ]; then
REFRESH_TOKEN=$2
RESPONSE=`curl --silent "https://accounts.google.com/o/oauth2/token" --data "client_id=$CLIENT_ID&client_secret=$CLIENT_SECRET&refresh_token=$REFRESH_TOKEN&grant_type=refresh_token"`
ACCESS_TOKEN=`echo $RESPONSE | python -mjson.tool | grep -oP 'access_token"\s*:\s*"\K(.*)"' | sed 's/"//'`
echo "Access Token: $ACCESS_TOKEN"
fi |
Par contre, dès que je le change (ce pourquoi le script m'intéresse) en le scope de Gmail , soit https://mail.google.com/ (https://developers.google.com/gmail/api/auth/scopes), j'obtiens le message suivant
Go to and enter to grant access to this application. Hit enter when done...
En gros, les variables sont vides, et je ne comprends pas pourquoi. Il me semble que les URL de Oauth sont bien générales et marchent quelque soit l'application de Google qui est elle identifiée par le scope ...
Aussi, si j'ai bien compris, après avoir rentré le code depuis le navigateur, on a plus à le refaire et pour palier à l'expiration d'un token, on utilise le script qui prend comme paramètres refresh $REFRESH_TOKEN ? Faudra donc recréer un nouveau $REFRESH_TOKEN (chose qui n'est pas faite dans le code) pour palier à la deuxième expiration etc. ?
Merci.
Partager