查询 Office 365 服务通信 API 与 Python

Querying Office 365 Service Communications API with Python

我正在尝试通过查询服务通信来获取所有 Office 365 服务的名称API。

我已经能够使用 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脚本returns出错。具体来说,它 returns 如下:

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

您的 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"

此外,请注意 public 预览中有一个新版本的 Office 365 服务通信 API,可在此处获取: https://msdn.microsoft.com/en-us/library/office/dn707385.aspx

它有一些您可能会感兴趣的新方法,并且更容易开发。新的 API 遵循其他 Microsoft API 正在使用的 OAuth 2.0 流程。如果您使用多个 Microsoft API,那么您将已经熟悉该流程。

如果这回答了您的问题或有任何其他问题,请告诉我。