简体   繁体   English

使用Python查询Office 365服务通信API

[英]Querying Office 365 Service Communications API with Python

I am trying to get the name of all Office 365 services by querying the Service Communications API. 我试图通过查询服务通信API来获取所有Office 365服务的名称。

I have been able to complete the task using a PowerShell script, but am unable to do the same using Python. 我已经能够使用PowerShell脚本完成任务,但是无法使用Python来完成相同的任务。

When using Python, I get a 200 response code, but have been unable to parse what is returned. 使用Python时,我收到200响应代码,但无法解析返回的内容。 Any help would be much appreciated. 任何帮助将非常感激。

My attempt to convert the PowerShell script to Python is below. 下面是我将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)

The Python script returns an error. Python脚本返回错误。 Specifically, it returns the following: 具体来说,它返回以下内容:

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

There are a few issues with your python script. python脚本存在一些问题。 Here is the correct python script to duplicate the results from the powershell script you posted. 这是正确的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")

This is the response you will get: 这是您将收到的响应:

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

Additionally, please note that there is a new version of the Office 365 Service Communications API in public preview which is available here: https://msdn.microsoft.com/en-us/library/office/dn707385.aspx 此外,请注意,在公共预览中有一个新版本的Office 365服务通信API,可从以下位置获取: https : //msdn.microsoft.com/zh-cn/library/office/dn707385.aspx

It has a few new methods that might be interesting to you, and is a bit easier to develop against. 它具有一些您可能感兴趣的新方法,并且较容易开发。 The new API follows the OAuth 2.0 flow that the other Microsoft APIs are using. 新的API遵循其他Microsoft API正在使用的OAuth 2.0流程。 If you are using multiple Microsoft APIs, than you will be familiar with the flow already. 如果您正在使用多个Microsoft API,那么您将已经熟悉该流程。

Let me know if this answers your question or if have any additional questions. 让我知道这是否回答了您的问题,或者还有其他问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM