lxx
lxx

Reputation: 1346

Getting security token with requests

Trying to use requests to get a token from an appspace server . Works in curl, works from DHC chrome extension but can't get code to work. More than likely something dumb I've missed or doing.

https://knowledgecenter.appspace.com/5-4/other-resources/developer-guides/appspace-graph-api-examples https://knowledgecenter.appspace.com/5-4/other-resources/developer-guides/appspace-graph-api

I've tried

url = 'http://10.2.3.205/api/v1/token/request'
headers = {'Content-type': 'application/json', 'Accept': 'application/json'}

data = {"Authentication" :
 {"Username": "[email protected]",
  "Password": "password"
 }
}

r = requests.get(url, auth=(data), headers=headers)

print r.status_code
print r.headers['content-type']

which gives a dict object not callable error

also get the same error with

auth = {"Username": "[email protected]",
      "Password": "password"
     }

also tried

    auth = {"Authentication" :
 {"Username": "[email protected]",
  "Password": "password"
 }
}
    r = requests.post(url, params=auth, headers=headers)

which gives

200
application/json; charset=utf-8
{"Errors":[{"Code":"500","Message":"Token request object is null"}],"Status":2}

and more directly with

r = requests.get(url, auth=("[email protected]", "password" ), headers=headers)

which gives

405
text/html; charset=UTF-8

and a few other ways as well which give 405 errors as well.

curl gets the token

$ curl -i -X POST    -H "Accept:application/json"    -H "Content-Type:application/json"    -d '{"Authentication" :                 
 {"Username": "[email protected]",
  "Password": "password"
 }
}'  'http://10.2.3.205/api/v1/token/request'
HTTP/1.1 200 OK
Cache-Control: private
Content-Length: 67
Content-Type: application/json; charset=utf-8
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Tue, 03 Mar 2015 14:21:17 GMT

{"Status":1,"SecurityToken":"valid token string"}

Thank you

Upvotes: 0

Views: 2249

Answers (1)

lxx
lxx

Reputation: 1346

doh

yes very dumb

data=json.dumps(auth1)

so fixed code is

url = 'http://10.2.3.205/api/v1/token/request'
headers = {'Content-type': 'application/json', 'Accept': 'application/json'}

auth1 = {"Authentication" :
{"Username": "[email protected]",
"Password": "password"
 }
}

r = requests.get(url, data=json.dumps(auth1), headers=headers)
print r.status_code
print r.headers['content-type']
print r.text
print r.headers

which gives

200
application/json; charset=utf-8
{"Status":1,"SecurityToken":"23a4ef65-d5f1-4067-8797-cba03f69e5d9"}
{'content-length': '67', 'x-aspnet-version': '4.0.30319', 'x-powered-by': 'ASP.NET', 'server': 'Microsoft-IIS/7.5',  
'cache-control': 'private', 'date': 'Tue, 03 Mar 2015 15:50:02 GMT', 'content-type': 'application/json; charset=utf-8'}

Hope this helps someone else so they don't get stuck for a while

Upvotes: 2

Related Questions