Jump to content


Photo

Need help connecting to API

python api rest

  • Please log in to reply
1 reply to this topic

#1 jaan

jaan

    Newbie

  • Members
  • Pip
  • 1 posts

Posted 07 June 2014 - 04:41 PM

Hello,

I need some help getting started connecting to the Zurmo API. I'm evidently missing something.

 

When I query the (correct?) API URL I receive a reply of:

 

{u'status': u'FAILURE', u'message': u'Invalid username or password.', u'errors': None, u'data': None}

 

Here's my code:

from urllib2 import Request, urlopen, URLError, HTTPError
import urllib
import simplejson as json
import requests

#strUrl      = "http://trial1.zurmo.com/index.php/zurmo/api/login"                                      # returns 404
#strUrl      = "http://trial1.zurmo.com/05/e4..../app/index.php"                             # returns 400
#strUrl      = "http://trial1.zurmo.com/index.php/zurmo/api/login"                                      # returns 400
strUrl      = "http://trial1.zurmo.com/05/e4..../app/index.php/zurmo/api/login"      # returns invalid username/password. 

data = {
    'ZURMO_AUTH_USERNAME' : ' admin',
    'ZURMO_AUTH_PASSWORD' : ' mypassword',
    'ZURMO_API_REQUEST_TYPE' : ' REST'
}

headers = {'Accept' : 'application/json'}
encodedValues = urllib.urlencode(data)

try:
    r = requests.post(strUrl, data, auth=('admin', 'mypassword'), headers=headers)
    print "from json: "
    print r.json

except HTTPError, e:
    print 'The server couldn\'t fulfill the request.'                       
    print 'Error code: ', e.code

except URLError, e:
    print 'We failed to reach the server.'                                       
    print 'Reason: ', e.reason

    if e.code == 404:
        print "404: No page. Go fish "

    if e.code == 400:
        print "400: Bad request. Check your code"

else:                                                                       # returned some data
    print "ok so we got something back"

I checked  http://zurmo.org/wik...-authentication for the right URL. I’ve tried the admin account sent to us for the trial and an account I created ( sign in via web API = allow). Now I’m stuck.

 

I'm I missing something or can trial accounts not use the API?

 

Thank you!

 



#2 Sardinha

Sardinha

    Newbie

  • Members
  • Pip
  • 1 posts

Posted 16 October 2014 - 08:32 AM

Hello Jaan.

 

Hope you're ok.

I think i can help you because right now i can successfully authenticate into Zurmo with my Python script.

Maybe it is better if i share with you my piece of code regarding the authentication process:

def zurmoAuthenticate(zURL, zInstance, zUser, zPassword):
    print "Executing authentication ..."
    headers = {"Content-type": "application/x-www-form-urlencoded",
               "Accept": "application/json",
               "ZURMO_AUTH_USERNAME": zUser,
               "ZURMO_AUTH_PASSWORD": zPassword,
               "ZURMO_API_REQUEST_TYPE": "REST"}

    conn = httplib.HTTPSConnection(zURL, 443)
    conn.request("POST", zInstance, "", headers)
    response = conn.getresponse()
    print response.status, response.reason
    dataBack = response.read()
    conn.close()

    return (dataBack)


# This next function will treat the data response from authentication function. At the end we will have a dictionary containing the sessionID and the token provided by the authentication.

def returnTokens(responseInJSON):
    responseInJSON = json.loads(responseInJSON)
    if responseInJSON["status"] == "SUCCESS":
        token = responseInJSON["data"]["token"]
        sessID = responseInJSON["data"]["sessionId"]
        return {"sessionID": sessID,
                "token": token}
    else:
        print "Shafted"
        print responseInJSON
        return {}
    return {}


# This is how i call the functions:

tokenJSON = zurmoAuthenticate(zurmoURL, zurmoInstance, zurmoUser, zurmoPassword)
tokens = returnTokens(tokenJSON)

Hope this helps you.

By the way jaan, i have sent you a personal. If possible i would like to have an example of your python data format when you are making a request to Zurmo. In my message i gave you some more details. Could you help me with that? Many thanks in advance.

 

With regards,

Luis Sardinha







Also tagged with one or more of these keywords: python, api, rest

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users