我试图通过查询Service Communications API来获取所有Office 365服务的名称。
我已经能够使用PowerShell脚本完成任务,但是使用Python无法做到这一点。
使用Python时,我得到200响应代码,但无法解析返回的内容。任何帮助将不胜感激。
我将PowerShell脚本转换为Python的尝试如下。
import json
import requests
from requests.auth import HTTPBasicAuth
username = "username"
password = "password"
# Base Service Communications URI
baseuri = "https://api.admin.microsoftonline.com/shdtenantcommunications.svc"
headers = {"accept": "application/json;odata=verbose"}
auth = {"username": username, "password": password}
# URI Paths
serviceinfo = "/GetServiceInformation"
register = "/Register"
response = requests.options(baseuri+register, auth=HTTPBasicAuth(username, password))
print("Registration status code: %s" % response.status_code)
if (response is not None and 200 == response.status_code):
info = requests.options(baseuri+serviceinfo, auth=HTTPBasicAuth(username, password))
print("Info status code: %s" % info.status_code)
data = json.loads(info.text)
Python脚本返回错误。具体来说,它返回以下内容:
Registration status code: 200
Info status code: 200
Traceback (most recent call last):
File "o365_option.py", line 22, in <module>
data = json.loads(info.text)
File "/usr/local/lib/python2.7/json/__init__.py", line 326, in loads
return _default_decoder.decode(s)
File "/usr/local/lib/python2.7/json/decoder.py", line 366, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/local/lib/python2.7/json/decoder.py", line 384, in raw_decode
raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded
答案 0 :(得分:3)
你的python脚本有一些问题。这是正确的python脚本,用于复制您发布的powershell脚本的结果。
import json
import requests
from requests.auth import HTTPBasicAuth
username = "username"
password = "password"
# Base Service Communications URI
baseuri = "https://api.admin.microsoftonline.com/shdtenantcommunications.svc"
headers = {"accept": "application/json;odata=verbose"}
auth = {"username": username, "password": password}
# URI Paths
serviceinfo = "/GetServiceInformation"
register = "/Register"
payload = {'userName': username, 'password': password}
myheaders = {'Content-Type': 'application/json'}
data=json.dumps(payload)
response = requests.post(baseuri+register,data=json.dumps(payload),headers=myheaders)
responsedata = json.loads(response.text)
cookie = responsedata.get("RegistrationCookie")
payload1 = {'lastCookie':cookie,'locale':"en-US"}
response = requests.post(baseuri+serviceinfo,data=json.dumps(payload1),headers=myheaders)
responsedata = json.loads(response.text)
for myobject in responsedata:
print myobject.get("ServiceName")
这是您将得到的回复:
"Exchange Online"
"Office Subscription"
"Identity Service"
"Office 365 Portal"
"Skype for Business"
"SharePoint Online"
"Rights Management Service"
"Yammer Enterprise"
"OneDrive for Business"
"Mobile Device Management"
此外,请注意,公共预览中有新版本的Office 365 Service Communications API,可在此处获取: https://msdn.microsoft.com/en-us/library/office/dn707385.aspx
它有一些可能对您感兴趣的新方法,并且更容易开发。新API遵循其他Microsoft API正在使用的OAuth 2.0流程。如果您使用的是多个Microsoft API,那么您已经熟悉了该流程。
如果这回答了您的问题,或者是否有任何其他问题,请告诉我。