echo-bot icon indicating copy to clipboard operation
echo-bot copied to clipboard

`curl -b .cookie` can't work

Open haskell-monad opened this issue 6 years ago • 0 comments

calling ./myprovider.sh auth will generate a ./.cookie file, then execute another command (eg get-self), response 401 Authorization Required, then I manually add the access_token to -H 'Authorization: Bearer $access_token ', request again, can work normally (Amazon server Linux).

my improvement is: extract access_token from the response 'cookie' and save it to the ./.access_token file. When executing other commands, read the access_token in the file and put it into -H "Authorization: Bearer $access_token", then send the request.

authenticate() {
   ### ..... Omit other code ....
   resp=$(curl -s -X POST "$zapi/provider/login" \
        -H 'Content-Type: application/json' \
        -d '{"email":"'"$auth_email"'"
            ,"password":"'"$auth_password"'"}' \
        -c ./.cookie)

    ### ..... save access_token ....
    awk 'END{print $7}' ./.cookie > ./.access_token
    cat ./.access_token
 
    echo "$auth_ident" > .current
    echo "Authenticated as $auth_email"
}
get_self() {
    check_auth

    ###  read access_token from ./.access_token file
    access_token=$(< "./.access_token")

    ###  put it to header
    ###  still pass the ./.cookie, double insurance
    resp=$(curl -s -X GET -H "Authorization: Bearer $access_token" "$zapi/provider" -b ./.cookie)
   
    if [[ "$resp" == \{* ]]; then
        echo "$resp" | jq .
    else
        echo "$resp"
    fi
}

haskell-monad avatar Jun 13 '19 11:06 haskell-monad