#!/bin/bash URL="https://api.hubic.com" CLIENTID="api_hubic_1" CLIENTSECRET="gXfu3KU6" USEREMAIL="@gmail.com" USERPWD="" CREDENTIALS=$( printf "$CLIENTID:$CLIENTSECRET" | base64 -w 0 ) urlen() { # Thanks https://gist.github.com/cdown/1163649 local length="${#1}" for (( i = 0; i < length; i++ )); do local c="${1:i:1}" case $c in [a-zA-Z0-9.~_-]) printf "$c" ;; *) printf '%s' "$c" | xxd -p -c1 | while read c; do printf '%%%s' "$c"; done ;; esac done } function setup () { # Obtain request code OAUTH=$( curl -s --get "https://api.hubic.com/oauth/auth/" --data-urlencode "client_id=$CLIENTID" --data-urlencode "redirect_uri=$URL" --data-urlencode "scope=usage.r,account.r,getAllLinks.r,credentials.r,sponsorCode.r,activate.w,sponsored.r,links.drw" --data-urlencode "response_type=code" --data-urlencode "state=RandomString_mpOwM8gSJD" | grep "name=\"oauth\"" | cut -d" " -f4 | cut -c8-13 ) if [ $? -ne 0 ]; then echo "Error getting request code, verify credentials" exit 1 fi # Accepting the app itself for the request token REQUESTTOKEN=$( curl -s -i "https://api.hubic.com/oauth/auth/" --data-urlencode "oauth=$OAUTH" --data-urlencode "action=accepted" --data-urlencode "account=r" --data-urlencode "credentials=r" --data-urlencode "getAllLinks=r" --data-urlencode "links=r" --data-urlencode "links=w" --data-urlencode "usage=r" --data-urlencode "login=$USEREMAIL" --data-urlencode "user_pwd=$USERPWD" --data-urlencode "submit=Accept" | grep Location | cut -c11- | grep code | cut -d"=" -f2 | cut -d"&" -f1 ) if [ $? -ne 0 ]; then echo "Error getting Request Token, try again later" exit 1 fi # Obtaining the auth code AUTHCODE=$( curl -s "https://api.hubic.com/oauth/token/" -H "Authorization: Basic $CREDENTIALS" --data-urlencode "code=$REQUESTTOKEN" --data-urlencode "redirect_uri=$URL" --data-urlencode "grant_type=authorization_code" | cut -d"\"" -f10 ) if [ $? -ne 0 ] || [ -z "$AUTHCODE" ]; then echo "Error $? getting Auth Code, try again later." exit 1 fi #Obtaining endpoint and token curl -s -H "Authorization: Bearer $AUTHCODE" https://api.hubic.com/1.0/account/credentials > /tmp/paso1.txt if [ $? -ne 0 ] || [ $( cat /tmp/paso1.txt | grep invalid ) ] ; then echo "Error $? getting token, check your AuthCode" echo "Debug: " cat /tmp/paso1.txt echo "***********************************" rm /tmp/paso1.txt exit 1 fi TOKEN=$( cat /tmp/paso1.txt | cut -d"\"" -f4 ) ENDPOINT=$( cat /tmp/paso1.txt | cut -d"\"" -f8 ) #Deleting temp file rm /tmp/paso1.txt } if [ $# == 0 ]; then echo "hubiC cli manager - NetoMX v0.1" echo "Usage: $0 [-l / -d / -u] [FILE]" exit 1 fi if [ "$1" == "-d" ] || [ "$1" == "-u" ] && [ $# != 2 ]; then echo "hubiC cli manager - NetoMX v0.1" echo "Usage: $0 [-l / -d / -u] [FILE]" exit 1 fi case "$1" in "-d") echo "Scarico i files da questo elenco: $2" setup elencuccio=./$2 while read line || [[ -n "$line" ]]; do FILENAME=$( urlen "$line" ) echo "downloading del seguente file:" $line curl -s -g -H "X-Auth-Token: $TOKEN" "$ENDPOINT/default/documenti/$FILENAME" -X GET -o "$line" if [ $? -ne 0 ]; then echo "Error $? downloading file: $1" exit 1 fi done < "$elencuccio" exit 0 ;; "-u") echo "Uploading dei files elencati in: $2" setup elencuccio=./$2 while read line || [[ -n "$line" ]]; do curl -s -g -H "X-Auth-Token: $TOKEN" "$ENDPOINT/default/" -X PUT -T "$line" echo "Invio del seguente file:" $line if [ $? -ne 0 ]; then echo "Errore $? inviando archivo $line" exit 1 fi done < "$elencuccio" exit 0 ;; "-l") echo "Listing files:" setup #Uncomment this if you want pretty JSON #curl -s -H "X-Auth-Token: $TOKEN" $ENDPOINT/default?format=json -X GET | python -m json.tool curl -s -H "X-Auth-Token: $TOKEN" $ENDPOINT/default?format=json -X GET | grep -e "bytes" -e "name" if [ $? -ne 0 ]; then echo "Error $? listing files" exit 1 fi exit 0 ;; *) echo "Error: argument not recognized." echo "Usage: $0 [-l / -d / -u] [FILE]" exit ;; esac