Python requests 通过请求获取安全令牌

Python requests 通过请求获取安全令牌,python-requests,Python Requests,尝试使用请求从appspace服务器获取令牌。 可以在curl中工作,可以从DHC chrome扩展中工作,但无法使代码正常工作。 很可能是我错过或做过的愚蠢的事情 我试过了 url = 'http://10.2.3.205/api/v1/token/request' headers = {'Content-type': 'application/json', 'Accept': 'application/json'} data = {"Authentication" : {"Usern

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

我试过了

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
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'}
还有一些其他的方法也给出了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"}
谢谢你

是的,非常愚蠢

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

405
text/html; charset=UTF-8
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'}
希望这对其他人有帮助,这样他们就不会陷入困境