Import contacts: Difference between revisions

From Open-Xchange
m (Modify session call to remove "password" from URL.)
(Undo revision 20850 by Diego.giron (talk))
Line 22: Line 22:
  CURL='curl -b cookies -c cookies -H Expect: -s -L --location-trusted -k'
  CURL='curl -b cookies -c cookies -H Expect: -s -L --location-trusted -k'
   
   
# Because of security reasons each login variation will reject requests containing the parameter "password"
SESSION=`$CURL "$SERVER/ajax/login?action=login&name=$USER&password=$PASSWORD" \
# within the URL query (starting with 7.8.0).
SESSION=`$CURL --data "name=$USER&password=$PASSWORD" "$SERVER/ajax/login?action=login" \
         |sed 's/^.*session\":\"\([0-9A-Fa-f]*\)\".*$/\1/'`
         |sed 's/^.*session\":\"\([0-9A-Fa-f]*\)\".*$/\1/'`
  echo $SESSION | grep "error" && echo "got no session, login failed" && exit 1
  echo $SESSION | grep "error" && echo "got no session, login failed" && exit 1

Revision as of 08:11, 30 October 2015

This is an example shell script which allows importing contacts from a local file into a certain Open-Xchange contacts folder.

The folderid is a bit tricky to find out. One way is to discover the HTTP API requests from the UI for example by using Firebug.

#!/bin/bash

SERVER=http://your.ox.server
USER=user@context
PASSWORD=userspw

USAGE () {
        echo "usage $0 filename folderid"
        exit 1
}

test "$1" = "--help" && USAGE
test "$#" = "2" || USAGE

IMP_FILE="$1"
FOLDER="$2"

CURL='curl -b cookies -c cookies -H Expect: -s -L --location-trusted -k'

SESSION=`$CURL "$SERVER/ajax/login?action=login&name=$USER&password=$PASSWORD" \
        |sed 's/^.*session\":\"\([0-9A-Fa-f]*\)\".*$/\1/'`
echo $SESSION | grep "error" && echo "got no session, login failed" && exit 1

echo
FOLDER_NAME=`$CURL "$SERVER/ajax/folders?action=get&session=$SESSION&id=$FOLDER" | sed ' s/^.*title":"//' | sed 's/".*$//'`
echo "going to clear Folder $FOLDER name: $FOLDER_NAME. Proceed (y/n)?"

read a
if [ "$a" = y -o "$a" = Y ]
then
        echo clearing Folder: $FOLDER
        DELETEREQ=$($CURL -X PUT  --data-binary "[\"$FOLDER\"]" --header "Content-Type: text/javascript" "$SERVER/ajax/folders?action=clear&session=$SESSION")
        if [ ! "$DELETEREQ" = '{"data":[]}' ]
        then
                echo "could not clear folder, error:"
                echo $DELETEREQ
                echo aborted
                exit 1
        else
                echo
                echo importing file $IMP_FILE to Folder $FOLDER
                $CURL -X POST --form cs_import_type=CSV -F "file=@$IMP_FILE;type=text/csv;name=file" "$SERVER/ajax/import?action=CSV&session=$SESSION&folder=$FOLDER"
                echo
                echo done
        fi
else
        echo "aborted"
        exit 1
fi
exit 0