通过请求获取安全令牌

时间:2015-03-04 00:49:07

标签: python-requests

尝试使用请求从appspace服务器获取令牌。 在curl中工作,从DHC chrome扩展工作,但无法使代码工作。 很可能是我错过或做过的蠢事。

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

我试过

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

data = {"Authentication" :
 {"Username": "email@company.com",
  "Password": "password"
 }
}

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

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

给dict对象不可调用错误

也会出现与

相同的错误
auth = {"Username": "email@company.com",
      "Password": "password"
     }

也尝试了

    auth = {"Authentication" :
 {"Username": "email@company.com",
  "Password": "password"
 }
}
    r = requests.post(url, params=auth, headers=headers)

给出了

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

更直接与

r = requests.get(url, auth=("email@company.com", "password" ), headers=headers)

给出了

405
text/html; charset=UTF-8

以及其他一些方法也会产生405错误。

curl获取令牌

$ curl -i -X POST    -H "Accept:application/json"    -H "Content-Type:application/json"    -d '{"Authentication" :                 
 {"Username": "email@company.com",
  "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"}

谢谢

1 个答案:

答案 0 :(得分:1)

DOH

是的,非常愚蠢

data=json.dumps(auth1)

所以固定代码是

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

auth1 = {"Authentication" :
{"Username": "email@company.com",
"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

给出了

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'}

希望这可以帮助别人,让他们不会被困一段时间