通过请求获取安全令牌

Getting security token with requests

正在尝试使用请求从应用空间服务器获取令牌。 在 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']

给出了一个字典对象不可调用的错误

也得到与

相同的错误
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"}

谢谢

是的很蠢

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

希望这对其他人有帮助,这样他们就不会卡住一段时间